src/api/routes.go (view raw)
1package api
2
3import (
4 "net/http"
5
6 "github.com/birabittoh/go-lift/src/database"
7 "github.com/birabittoh/go-lift/src/ui"
8)
9
10const uiDir = "ui"
11
12var fileServer = http.FileServer(http.Dir(uiDir))
13
14func GetServeMux(dbStruct *database.Database) *http.ServeMux {
15 mux := http.NewServeMux()
16 db := dbStruct.DB
17
18 mux.HandleFunc("GET /authelia/api/user/info", mockAutheliaHandler)
19
20 mux.HandleFunc("GET /api/ping", pingHandler)
21 mux.HandleFunc("GET /api/connection", connectionHandler(db))
22
23 // Profile routes
24 mux.HandleFunc("GET /api/users/{id}", getUserHandler(db))
25 mux.HandleFunc("PUT /api/users/{id}", updateUserHandler(db))
26
27 // Exercises routes (read-only)
28 mux.HandleFunc("GET /api/exercises", getExercisesHandler(db))
29 mux.HandleFunc("GET /api/exercises/{id}", getExerciseHandler(db))
30
31 // Routines routes
32 mux.HandleFunc("GET /api/routines", getRoutinesHandler(db))
33 mux.HandleFunc("GET /api/routines/{id}", getRoutineHandler(db))
34 mux.HandleFunc("POST /api/routines", createRoutineHandler(db))
35 mux.HandleFunc("PUT /api/routines/{id}", updateRoutineHandler(db))
36 mux.HandleFunc("DELETE /api/routines/{id}", deleteRoutineHandler(db))
37
38 // Record routines routes (workout sessions)
39 mux.HandleFunc("GET /api/records", getRecordRoutinesHandler(db))
40 mux.HandleFunc("GET /api/records/{id}", getRecordRoutineHandler(db))
41 mux.HandleFunc("POST /api/records", createRecordRoutineHandler(db))
42 mux.HandleFunc("PUT /api/records/{id}", updateRecordRoutineHandler(db))
43 mux.HandleFunc("DELETE /api/records/{id}", deleteRecordRoutineHandler(db))
44
45 // Stats routes
46 mux.HandleFunc("GET /api/stats", getStatsHandler(db))
47
48 /*
49 // Static UI route
50 mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
51 requestedFile := filepath.Join(uiDir, r.URL.Path)
52 _, err := os.Stat(requestedFile)
53
54 if err == nil || r.URL.Path == "/" {
55 fileServer.ServeHTTP(w, r)
56 return
57 }
58
59 http.ServeFile(w, r, filepath.Join(uiDir, "index.html"))
60 })
61 */
62 ui.InitServeMux(mux)
63
64 return mux
65}
66
67type WorkoutStats struct {
68 TotalWorkouts int64 `json:"totalWorkouts"`
69 TotalMinutes int `json:"totalMinutes"`
70 TotalExercises int64 `json:"totalExercises"`
71 MostFrequentExercise *struct {
72 Name string `json:"name"`
73 Count int `json:"count"`
74 } `json:"mostFrequentExercise,omitempty"`
75 MostFrequentRoutine *struct {
76 Name string `json:"name"`
77 Count int `json:"count"`
78 } `json:"mostFrequentRoutine,omitempty"`
79 RecentWorkouts []database.RecordRoutine `json:"recentWorkouts"`
80}