| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | |
| 6 | "github.com/stevedylandev/andromeda/pkg/auth" |
| 7 | "github.com/stevedylandev/andromeda/pkg/darkmatter" |
| 8 | "github.com/stevedylandev/andromeda/pkg/web" |
| 9 | ) |
| 10 | |
| 11 | func (a *App) routes() *http.ServeMux { |
| 12 | mux := http.NewServeMux() |
| 13 | |
| 14 | requireSession := func(next http.HandlerFunc) http.HandlerFunc { |
| 15 | return a.Sessions.RequireSession("/login", next) |
| 16 | } |
| 17 | requireAPIKey := func(next http.HandlerFunc) http.HandlerFunc { |
| 18 | return auth.RequireAPIKey(a.APIKey, next) |
| 19 | } |
| 20 | |
| 21 | // Public: only the login flow and static assets. |
| 22 | mux.HandleFunc("GET /login", a.loginGetHandler) |
| 23 | mux.HandleFunc("POST /login", a.loginPostHandler) |
| 24 | mux.HandleFunc("GET /logout", a.logoutHandler) |
| 25 | mux.HandleFunc("GET /static/", web.EmbeddedHandler(appFS, "static")) |
| 26 | darkmatter.Mount(mux, "/assets") |
| 27 | |
| 28 | // Everything else requires a session (fully gated admin app). |
| 29 | mux.HandleFunc("GET /", requireSession(a.dashboardHandler)) |
| 30 | mux.HandleFunc("GET /new", requireSession(a.newHabitHandler)) |
| 31 | mux.HandleFunc("GET /settings", requireSession(a.settingsHandler)) |
| 32 | mux.HandleFunc("GET /export.csv", requireSession(a.exportHandler)) |
| 33 | |
| 34 | mux.HandleFunc("POST /habits", requireSession(a.createHabitHandler)) |
| 35 | mux.HandleFunc("GET /habits/{short_id}", requireSession(a.habitDetailHandler)) |
| 36 | mux.HandleFunc("POST /habits/{short_id}", requireSession(a.updateHabitHandler)) |
| 37 | mux.HandleFunc("POST /habits/{short_id}/delete", requireSession(a.deleteHabitHandler)) |
| 38 | |
| 39 | mux.HandleFunc("POST /records", requireSession(a.createRecordHandler)) |
| 40 | mux.HandleFunc("POST /records/{short_id}", requireSession(a.updateRecordHandler)) |
| 41 | mux.HandleFunc("POST /records/{short_id}/delete", requireSession(a.deleteRecordHandler)) |
| 42 | |
| 43 | // Read-only JSON API, gated by X-API-Key (disabled when key is empty). |
| 44 | mux.HandleFunc("GET /api/habits", requireAPIKey(a.apiListHabits)) |
| 45 | mux.HandleFunc("GET /api/records", requireAPIKey(a.apiListRecords)) |
| 46 | |
| 47 | return mux |
| 48 | } |