-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
87 lines (76 loc) · 2.11 KB
/
Copy pathmain.go
File metadata and controls
87 lines (76 loc) · 2.11 KB
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
package main
import (
"context"
"database/sql"
"errors"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"
"time"
_ "github.com/lib/pq"
)
func main() {
cfg, err := LoadConfig()
if err != nil {
panic(err)
}
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: logLevel(cfg.Environment)}))
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
db, err := sql.Open("postgres", cfg.DatabaseURL)
if err != nil {
logger.Error("open database", "error", err)
os.Exit(1)
}
defer db.Close()
db.SetMaxOpenConns(20)
db.SetMaxIdleConns(10)
db.SetConnMaxLifetime(30 * time.Minute)
store, err := NewStore(cfg, db, logger)
if err != nil {
logger.Error("create store", "error", err)
os.Exit(1)
}
if err := store.Migrate(ctx); err != nil {
logger.Error("migrate database", "error", err)
os.Exit(1)
}
if cfg.SeedOnStart {
if summary, err := store.SeedDemo(ctx, "startup"); err != nil {
logger.Warn("seed demo data", "error", err)
} else {
logger.Info("seeded demo data", "asset_id", summary.AssetID, "run_id", summary.RunID)
}
}
if store.ElasticEnabled() {
go store.ListenAndIndex(ctx)
logger.Info("elasticsearch indexing enabled", "index", cfg.ElasticIndex)
} else {
logger.Info("elasticsearch disabled; search uses postgres fallback")
}
server := &http.Server{
Addr: cfg.Address,
Handler: NewRouter(store, cfg, logger),
ReadHeaderTimeout: 10 * time.Second,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 120 * time.Second,
}
go func() {
logger.Info("searchAPI listening", "address", cfg.Address, "site_id", cfg.SiteID)
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.Error("http server failed", "error", err)
stop()
}
}()
<-ctx.Done()
shutdownCtx, cancel := context.WithTimeout(context.Background(), cfg.ShutdownTimeout)
defer cancel()
if err := server.Shutdown(shutdownCtx); err != nil {
logger.Error("http shutdown", "error", err)
os.Exit(1)
}
logger.Info("searchAPI stopped")
}