package web import ( "database/sql" "net/http" "sync" "time" "github.com/martinlehoux/biking_home/strava" "github.com/martinlehoux/kagamigo/kcore" ) const dateFormat = "2006-01-02" type Server struct { db *sql.DB configPath string oauthMu sync.Mutex oauthState string returnToURL string syncMu sync.Mutex syncActive bool } type syncClient interface { List(from, to time.Time) ([]strava.Activity, error) Get(id int64) (strava.Activity, []byte, error) } func NewServer(db *sql.DB, configPath string) *Server { return &Server{db: db, configPath: configPath} } func (s *Server) Handler() http.Handler { mux := http.NewServeMux() mux.Handle("GET /static/", http.FileServer(http.FS(staticFiles))) mux.HandleFunc("GET /", s.handleRides) mux.HandleFunc("GET /rides/{id}", s.handleRide) mux.HandleFunc("GET /analytics", s.handleAnalytics) mux.HandleFunc("POST /rides/{id}/official-climbs", s.handleCreateOfficialClimb) mux.HandleFunc("GET /sync", s.handleSyncForm) mux.HandleFunc("POST /sync", s.handleSync) mux.HandleFunc("GET /strava/login", s.handleStravaLogin) mux.HandleFunc("GET /strava/callback", s.handleStravaCallback) return kcore.RecoverMiddleware(mux) } func (s *Server) ListenAndServe(addr string) error { return http.ListenAndServe(addr, s.Handler()) }