Initial commit: smartrun MVP
Garmin run classification and progression tracker. Go backend (MCP client to mcp-garmin, SQLite store, deterministic rule engine, REST API) and React/TS frontend (Dashboard, Review Queue, Workout Kinds). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
91
backend/cmd/smartrund/main.go
Normal file
91
backend/cmd/smartrund/main.go
Normal file
@@ -0,0 +1,91 @@
|
||||
// Command smartrund is smartrun'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"
|
||||
|
||||
"smartrun/backend/internal/api"
|
||||
"smartrun/backend/internal/config"
|
||||
"smartrun/backend/internal/garmin"
|
||||
"smartrun/backend/internal/store"
|
||||
appsync "smartrun/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()
|
||||
|
||||
garminClient := garmin.NewClient(garmin.Config{
|
||||
PythonPath: cfg.GarminPythonPath,
|
||||
ServerPath: cfg.GarminServerPath,
|
||||
GarminEmail: cfg.GarminEmail,
|
||||
GarminPassword: cfg.GarminPassword,
|
||||
TokenStorePath: cfg.GarminTokenStore,
|
||||
})
|
||||
defer garminClient.Close()
|
||||
|
||||
syncSvc := appsync.NewService(garminClient, db, appsync.Config{
|
||||
BackfillHorizonDays: cfg.BackfillHorizonDays,
|
||||
MinConfidence: cfg.MinConfidence,
|
||||
MaxHR: cfg.MaxHR,
|
||||
}, nil)
|
||||
|
||||
server := api.NewServer(db, garminClient, syncSvc)
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
go runIncrementalSyncLoop(ctx, syncSvc, cfg.IncrementalSyncEvery)
|
||||
|
||||
httpServer := &http.Server{Addr: cfg.Addr, Handler: server.Router()}
|
||||
go func() {
|
||||
log.Printf("smartrund 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 in the
|
||||
// background so the frontend doesn't need to trigger every sync manually.
|
||||
func runIncrementalSyncLoop(ctx context.Context, svc *appsync.Service, every time.Duration) {
|
||||
ticker := time.NewTicker(every)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
if err := svc.IncrementalSync(ctx); err != nil {
|
||||
log.Printf("incremental sync: %v", err)
|
||||
continue
|
||||
}
|
||||
if err := svc.FillPendingDetails(ctx, 50); err != nil {
|
||||
log.Printf("fill pending details: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user