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