Pre-production app, no need to preserve incremental migration history: replace the 26 migration files with one current-state schema.sql (the schema.sql comments are now the living documentation), simplify db.go to apply it once instead of tracking/rebuilding through schema_migrations, and make user_id NOT NULL everywhere now that there's no staged migration to accommodate a nullable backfill window. This removes the reason ClaimLegacyOwner/GENIUSRUN_LEGACY_OWNER_OIDC_SUB existed (binding a pre-existing singleton-schema database to one account across a staged migration), so that whole path is gone too -- the existing dev database was wiped and reseeded fresh under the new schema. Add cmd/dumpschema, which regenerates docs/DATABASE.md straight from the live schema (via store.Open + sqlite_master introspection) so the database documentation can never drift out of sync with reality. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
95 lines
2.5 KiB
Go
95 lines
2.5 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"
|
|
"net/http"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"geniusrun/backend/internal/api"
|
|
"geniusrun/backend/internal/auth"
|
|
"geniusrun/backend/internal/config"
|
|
"geniusrun/backend/internal/garmin"
|
|
"geniusrun/backend/internal/store"
|
|
appsync "geniusrun/backend/internal/sync"
|
|
)
|
|
|
|
func main() {
|
|
cfg, err := config.Load()
|
|
if err != nil {
|
|
log.Fatalf("config: %v", err)
|
|
}
|
|
|
|
db, err := store.Open(cfg.DBPath)
|
|
if err != nil {
|
|
log.Fatalf("open database: %v", err)
|
|
}
|
|
defer db.Close()
|
|
|
|
authVerifier, err := auth.NewOIDCVerifier(context.Background(), auth.OIDCConfig{
|
|
IssuerURL: cfg.OIDCIssuerURL,
|
|
ClientID: cfg.OIDCClientID,
|
|
ClientSecret: cfg.OIDCClientSecret,
|
|
RedirectURL: cfg.OIDCRedirectURL,
|
|
RequiredRole: cfg.OIDCRequiredRole,
|
|
})
|
|
if err != nil {
|
|
log.Fatalf("oidc: %v", err)
|
|
}
|
|
|
|
server := api.NewServer(db, garmin.NewClient, garmin.Config{
|
|
PythonPath: cfg.GarminPythonPath,
|
|
ServerPath: cfg.GarminServerPath,
|
|
TokenStorePath: cfg.GarminTokenStoreRoot,
|
|
}, appsync.Config{
|
|
MinConfidence: cfg.MinConfidence,
|
|
}, authVerifier, api.SessionConfig{
|
|
Secret: cfg.SessionSecret,
|
|
Duration: cfg.SessionDuration,
|
|
Secure: cfg.SessionSecure,
|
|
PublicBaseURL: cfg.PublicBaseURL,
|
|
})
|
|
|
|
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
|
defer stop()
|
|
|
|
go runIncrementalSyncLoop(ctx, server, cfg.IncrementalSyncEvery)
|
|
|
|
httpServer := &http.Server{Addr: cfg.Addr, Handler: server.Router()}
|
|
go func() {
|
|
log.Printf("geniusrund listening on %s", cfg.Addr)
|
|
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
log.Fatalf("http server: %v", err)
|
|
}
|
|
}()
|
|
|
|
<-ctx.Done()
|
|
log.Println("shutting down...")
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
if err := httpServer.Shutdown(shutdownCtx); err != nil {
|
|
log.Printf("http server shutdown: %v", err)
|
|
}
|
|
}
|
|
|
|
// runIncrementalSyncLoop periodically syncs new activities for every
|
|
// provisioned user in the background so the frontend doesn't need to
|
|
// trigger every sync manually.
|
|
func runIncrementalSyncLoop(ctx context.Context, server *api.Server, every time.Duration) {
|
|
ticker := time.NewTicker(every)
|
|
defer ticker.Stop()
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-ticker.C:
|
|
server.RunIncrementalSyncForAllUsers(ctx)
|
|
}
|
|
}
|
|
}
|