summaryrefslogtreecommitdiff
path: root/main.go
blob: 813ac412ff5ba7ee8b79e3baa0ad7bd844be3731 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main

import (
	"context"
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"os"
	"os/signal"
	"path/filepath"
	"syscall"
	"time"

	"wikiapiserver/api"
	"wikiapiserver/db"
)

// Config is loaded from config.json at startup.
type Config struct {
	Database struct {
		Host     string `json:"host"`
		Username string `json:"username"`
		Password string `json:"password"`
		Name     string `json:"name"`
	} `json:"database"`
	Server struct {
		Port int `json:"port"`
	} `json:"server"`
}

func loadConfig() (*Config, error) {
	// Try CWD first (works for go run), then executable dir (works for built binary)
	for _, dir := range []string{".", ""} {
		if dir == "" {
			if exe, err := os.Executable(); err == nil {
				dir = filepath.Dir(exe)
			} else {
				continue
			}
		}

		path := filepath.Join(dir, "config.json")
		f, err := os.Open(path)
		if err != nil {
			continue
		}

		var cfg Config
		if err := json.NewDecoder(f).Decode(&cfg); err != nil {
			f.Close()
			return nil, fmt.Errorf("decode %s: %w", path, err)
		}
		f.Close()
		return &cfg, nil
	}

	return nil, fmt.Errorf("config.json not found in . or next to executable")
}

func buildDSN(cfg *Config) string {
	return fmt.Sprintf("%s:%s@tcp(%s)/%s",
		cfg.Database.Username,
		cfg.Database.Password,
		cfg.Database.Host,
		cfg.Database.Name,
	)
}

func main() {
	cfg, err := loadConfig()
	if err != nil {
		log.Fatalf("config: %v", err)
	}

	database, err := db.Connect(buildDSN(cfg))
	if err != nil {
		log.Fatalf("db: %v", err)
	}
	defer database.Close()

	handler := api.NewHandler(database)

	mux := http.NewServeMux()
	mux.HandleFunc("POST /register", handler.Register)
	mux.HandleFunc("POST /login", handler.Login)
	mux.HandleFunc("POST /refresh", handler.Refresh)
	mux.HandleFunc("GET /health", handler.Health)

	addr := fmt.Sprintf(":%d", cfg.Server.Port)
	srv := &http.Server{
		Addr:         addr,
		Handler:      mux,
		ReadTimeout:  5 * time.Second,
		WriteTimeout: 10 * time.Second,
		IdleTimeout:  120 * time.Second,
	}

	// Graceful shutdown on SIGINT / SIGTERM
	ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
	defer stop()

	go func() {
		<-ctx.Done()
		log.Println("shutting down...")
		shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
		defer cancel()
		if err := srv.Shutdown(shutdownCtx); err != nil {
			log.Printf("shutdown error: %v", err)
		}
	}()

	log.Printf("listening on %s", addr)
	if err := srv.ListenAndServe(); err != http.ErrServerClosed {
		log.Fatalf("server: %v", err)
	}

	log.Println("stopped")
}