Files
wotra/internal/handler/router.go
Andreas Schneider d8366f5c25 Add sync redesign with offline fallback (M9)
- Migration 003: adds logged_at to sync_log for TTL pruning; migrates
  settings_history to UUID TEXT PK with updated_at column
- SyncStore: Prune() deletes rows older than 30d and writes a '_pruned'
  marker at the boundary version; Pull() calls Prune lazily and returns
  ErrSyncStale (410) when the client's since_version is behind the marker
- sync_handler.go: GET /api/sync/pull?since=N; POST /api/sync/push with
  last-updated_at-wins conflict resolution for entries, balance_adjustments,
  settings_history; closed_days/closed_weeks skipped (server-only mutations)
- router.go: passes entryStore, adjustmentStore, settingsStore to SyncHandler
- settings_store.go: UUID PK, updated_at column, Upsert() for push path
- settings_service.go: generates UUID on create, sets updated_at on update
- settings_handler.go: ID params changed from int64 to string
- domain.go: Settings.ID string, Settings.UpdatedAt added
- client.ts: all mutation methods catch TypeError (offline) and fall back
  to Dexie write + outbox enqueue; crypto.randomUUID() for offline creates;
  Settings.id type changed to string
- db.ts: Dexie v3 — settings_history key path changed to string UUID;
  upgrade handler clears table for repopulation via pull
- sync.ts: real pushOutbox to POST /api/sync/push; pullChanges uses GET
  with ?since=N; 410 triggers coldStart() + retry; coldStart() wipes all
  tables and resets last_version
- 4 new Go store tests covering normal pull, stale client, empty prune,
  client-ahead-of-marker; all tests pass (store + service, 19 Vitest)
2026-04-30 22:50:33 +02:00

80 lines
1.9 KiB
Go

package handler
import (
"io/fs"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/wotra/wotra/internal/service"
"github.com/wotra/wotra/internal/store"
)
// NewRouter builds the full HTTP router.
func NewRouter(
authToken string,
entrySvc *service.EntryService,
daySvc *service.DayService,
settingsSvc *service.SettingsService,
weekSvc *service.WeekService,
syncStore *store.SyncStore,
entryStore *store.EntryStore,
adjustmentStore *store.BalanceAdjustmentStore,
settingsStore *store.SettingsStore,
staticFiles fs.FS,
) http.Handler {
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
// Unauthenticated
r.Get("/healthz", func(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, map[string]string{"status": "ok"})
})
// Authenticated API
r.Route("/api", func(r chi.Router) {
r.Use(AuthMiddleware(authToken))
entryH := NewEntryHandler(entrySvc)
entryH.Routes(r)
dayH := NewDayHandler(daySvc)
dayH.Routes(r)
settingsH := NewSettingsHandler(settingsSvc)
settingsH.Routes(r)
weekH := NewWeekHandler(weekSvc)
weekH.Routes(r)
syncH := NewSyncHandler(syncStore, entryStore, adjustmentStore, settingsStore)
syncH.Routes(r)
exportH := NewExportHandler(entrySvc, daySvc, weekSvc)
exportH.Routes(r)
})
// Serve embedded SPA if available (production build)
if staticFiles != nil {
fileServer := http.FileServer(http.FS(staticFiles))
r.Get("/*", func(w http.ResponseWriter, r *http.Request) {
// Try to open the requested path; fall back to index.html for SPA routing
path := r.URL.Path
if path == "/" || path == "" {
path = "index.html"
} else {
path = path[1:] // strip leading slash
}
if _, err := staticFiles.Open(path); err != nil {
r.URL.Path = "/"
}
fileServer.ServeHTTP(w, r)
})
}
return r
}