35 lines
900 B
Go
35 lines
900 B
Go
package handler
|
|
|
|
import (
|
|
"me-fit/middleware"
|
|
"me-fit/service"
|
|
|
|
"database/sql"
|
|
"net/http"
|
|
)
|
|
|
|
func GetHandler(db *sql.DB) http.Handler {
|
|
var router = http.NewServeMux()
|
|
|
|
router.HandleFunc("/", service.HandleIndexAnd404(db))
|
|
|
|
// Serve static files (CSS, JS and images)
|
|
router.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
|
|
|
|
router.Handle("/auth/", authUi(db))
|
|
router.Handle("/api/auth/", authApi(db))
|
|
|
|
router.Handle("/workout", auth(db, workoutUi(db)))
|
|
router.Handle("/api/workout", auth(db, workoutApi(db)))
|
|
// Needed a second time with trailing slash, otherwise either /api/workout or /api/workout/{id} does not match
|
|
router.Handle("/api/workout/", auth(db, workoutApi(db)))
|
|
|
|
return middleware.Logging(
|
|
middleware.EnableCors(
|
|
router))
|
|
}
|
|
|
|
func auth(db *sql.DB, h http.Handler) http.Handler {
|
|
return middleware.EnsureValidSession(db, h)
|
|
}
|