-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
95 lines (74 loc) · 2.44 KB
/
main.go
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
package main
import (
"log/slog"
"net/http"
"os"
glc "github.com/golobby/config/v3"
"github.com/golobby/config/v3/pkg/feeder"
"github.com/jmoiron/sqlx"
"github.com/pressly/goose/v3"
"github.com/marcus-crane/gunslinger/config"
gdb "github.com/marcus-crane/gunslinger/db"
"github.com/marcus-crane/gunslinger/events"
"github.com/marcus-crane/gunslinger/migrations"
"github.com/marcus-crane/gunslinger/playback"
)
func main() {
cfg := config.Config{}
envFeeder := feeder.Env{}
feeders := []glc.Feeder{envFeeder}
if _, err := os.Stat(".env"); err == nil {
// We only load in .env if one is present or else we crash with file not exists error
feeders = append(feeders, feeder.DotEnv{Path: ".env"})
}
err := glc.
New().
AddFeeder(feeders...).
AddStruct(&cfg).
Feed()
if err != nil {
panic(err)
}
logLevel := cfg.GetLogLevel()
h := slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: logLevel})
slog.SetDefault(slog.New(h))
slog.With(slog.String("log_level", logLevel.Level().String())).Debug("Initialised logger")
db, err := sqlx.Connect("sqlite", cfg.Gunslinger.DbPath)
if err != nil {
slog.Error("Failed to create connection to DB", slog.String("stack", err.Error()))
os.Exit(1)
}
// See https://blog.pecar.me/sqlite-prod
db.Exec("PRAGMA foreign_keys = ON")
db.Exec("PRAGMA journal_mode = WAL")
db.Exec("PRAGMA synchronous = NORMAL")
db.Exec("PRAGMA mmap_size = 134217728")
db.Exec("PRAGMA journal_size_limit = 27103364")
db.Exec("PRAGMA cache_size = 2000")
ps := playback.NewPlaybackSystem(db)
store := gdb.SqliteStore{DB: db}
goose.SetBaseFS(migrations.GetMigrations())
if err := goose.SetDialect(string(goose.DialectSQLite3)); err != nil {
slog.Error("Failed to set goose dialect", slog.String("stack", err.Error()))
os.Exit(1)
}
if err := goose.Up(db.DB, "."); err != nil {
slog.Error("Failed to create connection to DB", slog.String("stack", err.Error()))
os.Exit(1)
}
jobScheduler := SetupInBackground(cfg, ps, &store)
if cfg.Gunslinger.BackgroundJobsEnabled {
jobScheduler.StartAsync()
slog.Debug("Background jobs have started up in the background.")
} else {
slog.Debug("Background jobs are disabled.")
}
events.Init()
router := RegisterRoutes(http.NewServeMux(), cfg, ps)
slog.Info("Gunslinger is running at http://localhost:8080")
if err := http.ListenAndServe(":8080", router); err != nil {
slog.Error("", slog.String("stack", err.Error()))
jobScheduler.Stop()
os.Exit(1)
}
}