-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
79 lines (66 loc) · 2.02 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
package main
import (
"log/slog"
"os"
"time"
"gabe565.com/relax-sounds/internal/config"
"gabe565.com/relax-sounds/internal/debug"
"gabe565.com/relax-sounds/internal/handlers"
"gabe565.com/relax-sounds/internal/hooks"
"gabe565.com/relax-sounds/internal/metrics"
_ "gabe565.com/relax-sounds/migrations"
"github.com/lmittmann/tint"
"github.com/mattn/go-isatty"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/plugins/migratecmd"
slogmulti "github.com/samber/slog-multi"
)
func main() {
slog.SetDefault(slog.New(tint.NewHandler(os.Stderr, &tint.Options{
Level: slog.LevelInfo,
TimeFormat: time.DateTime,
NoColor: !isatty.IsTerminal(os.Stderr.Fd()) && !isatty.IsCygwinTerminal(os.Stderr.Fd()),
})))
app := pocketbase.New()
conf := config.New(app).RegisterFlags()
migratecmd.MustRegister(app, app.RootCmd, migratecmd.Config{
Automigrate: automigrateEnabled(),
})
app.OnBootstrap().BindFunc(func(e *core.BootstrapEvent) error {
if err := conf.Load(app.RootCmd); err != nil {
return err
}
return e.Next()
})
app.OnServe().BindFunc(func(e *core.ServeEvent) error {
e.Router.BindFunc(func(e *core.RequestEvent) error {
if e.Request.URL.Path == "/api/health" {
return apis.SkipSuccessActivityLog().Func(e)
}
return e.Next()
})
e.Router.GET("/{path...}", handlers.StaticHandler(conf))
handlers.NewMix(conf).RegisterRoutes(e)
metrics.Serve(conf)
debug.Serve(conf)
slog.SetDefault(slog.New(slogmulti.Fanout(
app.Logger().Handler(),
slog.Default().Handler(),
)))
go func() {
if err := hooks.ConvertAll(app); err != nil {
slog.Error("Failed to convert sounds", "error", err)
}
}()
return e.Next()
})
convertHook := hooks.Convert(app)
app.OnModelAfterCreateSuccess("sounds").BindFunc(convertHook)
app.OnModelAfterUpdateSuccess("sounds").BindFunc(convertHook)
if err := app.Start(); err != nil {
slog.Error("Failed to start app", "error", err)
os.Exit(1)
}
}