Files
geniusrun/backend/cmd/geniusrund/main.go
Christophe Vila 0e6cf00dba refactor(config): mandatory app-config rows seeded in DB; rename to session.setup_timeout
All registry keys must exist as rows in the config table: main seeds
missing keys with their defaults at startup, LoadApp fails fast on a
missing key, and the code-side fallback const/helper for the onboarding
setup timeout is gone -- the value rides in SessionConfig.SetupTimeout.
The key is renamed session.idle_timeout -> session.setup_timeout, and
the /config page's 'overridden' now means 'differs from the default'.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-04 18:17:24 +02:00

120 lines
3.3 KiB
Go

// Command geniusrund is geniusrun's backend server: syncs runs from Garmin,
// classifies them into workout kinds, and serves the REST API the frontend
// talks to.
package main
import (
"context"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"geniusrun/backend/internal/api"
"geniusrun/backend/internal/auth"
"geniusrun/backend/internal/config"
"geniusrun/backend/internal/garmin"
applog "geniusrun/backend/internal/log"
"geniusrun/backend/internal/store"
)
// fatal logs through the application JSON logger and exits -- the
// structured replacement for log.Fatalf, so even startup failures come
// out as JSON lines.
func fatal(msg string, err error) {
slog.Error(msg, "error", err)
os.Exit(1)
}
func main() {
// Bootstrap logger at info so failures loading the env config itself
// (which carries the real log level) are still emitted as JSON.
slog.SetDefault(applog.NewLogger("info", os.Stdout))
envCfg, err := config.LoadEnv()
if err != nil {
fatal("load env config", err)
}
slog.SetDefault(applog.NewLogger(envCfg.LogLevel, os.Stdout))
db, err := store.Open(envCfg.DBPath)
if err != nil {
fatal("open database", err)
}
defer db.Close()
values, err := db.ConfigValues(context.Background())
if err != nil {
fatal("read app config", err)
}
// Every registry key is mandatory in the DB: seed missing ones with
// their defaults so the config table is always fully populated (no
// code-side fallbacks anywhere downstream).
for _, k := range config.AppRegistry() {
if _, ok := values[k.Key]; !ok {
if err := db.SetConfigValue(context.Background(), k.Key, k.Default); err != nil {
fatal("seed app config default", err)
}
values[k.Key] = k.Default
}
}
appCfg, err := config.LoadApp(values)
if err != nil {
fatal("load app config", err)
}
authVerifier, err := auth.NewOIDCVerifier(context.Background(), auth.OIDCConfig{
IssuerURL: envCfg.OIDCIssuerURL,
ClientID: envCfg.OIDCClientID,
ClientSecret: envCfg.OIDCClientSecret,
RedirectURL: envCfg.OIDCRedirectURL,
RequiredRole: envCfg.OIDCRequiredRole,
})
if err != nil {
fatal("oidc verifier setup", err)
}
server := api.NewServer(
db,
garmin.NewClient,
garmin.ClientConfig{
PythonPath: envCfg.PythonPath,
TokenStorePath: envCfg.TokenStoreRoot,
},
garmin.SyncConfig{},
authVerifier,
api.SessionConfig{
Secret: envCfg.SessionSecret,
Duration: appCfg.SessionDuration,
SetupTimeout: appCfg.SetupTimeout,
Secure: envCfg.SessionSecure,
BackendURL: envCfg.BackendURL,
FrontendURL: envCfg.FrontendURL,
})
for _, e := range envCfg.DisplayEnv() {
server.EnvVars = append(server.EnvVars, api.EnvVar{Name: e.Name, Value: e.Value})
}
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
httpServer := &http.Server{Addr: envCfg.BackendAddr, Handler: server.Router()}
go func() {
slog.Info("geniusrund listening", "addr", envCfg.BackendAddr)
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
fatal("http server", err)
}
}()
<-ctx.Done()
slog.Info("shutting down")
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := httpServer.Shutdown(shutdownCtx); err != nil {
slog.Error("http server shutdown", "error", err)
}
}