fix: restructure handler.go #181
This commit is contained in:
@@ -1,32 +1,13 @@
|
|||||||
package main
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"me-fit/middleware"
|
|
||||||
"me-fit/service"
|
"me-fit/service"
|
||||||
"me-fit/template/mail"
|
|
||||||
"me-fit/utils"
|
|
||||||
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getHandler(db *sql.DB) http.Handler {
|
func handleAuth(db *sql.DB, router *http.ServeMux) {
|
||||||
var router = http.NewServeMux()
|
|
||||||
|
|
||||||
if utils.Environment == "dev" {
|
|
||||||
router.HandleFunc("/mail/", handleMails)
|
|
||||||
}
|
|
||||||
|
|
||||||
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("/workout", auth(db, service.HandleWorkoutPage(db)))
|
|
||||||
router.Handle("POST /api/workout", auth(db, service.HandleWorkoutNewComp(db)))
|
|
||||||
router.Handle("GET /api/workout", auth(db, service.HandleWorkoutGetComp(db)))
|
|
||||||
router.Handle("DELETE /api/workout/{id}", auth(db, service.HandleWorkoutDeleteComp(db)))
|
|
||||||
|
|
||||||
// Don't use auth middleware for these routes, as it makes redirecting very difficult, if the mail is not yet verified
|
// Don't use auth middleware for these routes, as it makes redirecting very difficult, if the mail is not yet verified
|
||||||
router.Handle("/auth/signin", service.HandleSignInPage(db))
|
router.Handle("/auth/signin", service.HandleSignInPage(db))
|
||||||
router.Handle("/auth/signup", service.HandleSignUpPage(db))
|
router.Handle("/auth/signup", service.HandleSignUpPage(db))
|
||||||
@@ -43,16 +24,4 @@ func getHandler(db *sql.DB) http.Handler {
|
|||||||
router.Handle("/api/auth/change-password", service.HandleChangePasswordComp(db))
|
router.Handle("/api/auth/change-password", service.HandleChangePasswordComp(db))
|
||||||
router.Handle("/api/auth/reset-password", service.HandleResetPasswordComp(db))
|
router.Handle("/api/auth/reset-password", service.HandleResetPasswordComp(db))
|
||||||
router.Handle("/api/auth/reset-password-actual", service.HandleActualResetPasswordComp(db))
|
router.Handle("/api/auth/reset-password-actual", service.HandleActualResetPasswordComp(db))
|
||||||
|
|
||||||
return middleware.Logging(middleware.EnableCors(router))
|
|
||||||
}
|
|
||||||
|
|
||||||
func auth(db *sql.DB, h http.Handler) http.Handler {
|
|
||||||
return middleware.EnsureValidSession(db, h)
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleMails(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if r.URL.Path == "/mail/register" {
|
|
||||||
mail.Register("test-code").Render(r.Context(), w)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
28
handler/default.go
Normal file
28
handler/default.go
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
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/"))))
|
||||||
|
|
||||||
|
handleWorkout(db, router)
|
||||||
|
|
||||||
|
handleAuth(db, router)
|
||||||
|
|
||||||
|
return middleware.Logging(middleware.EnableCors(router))
|
||||||
|
}
|
||||||
|
|
||||||
|
func auth(db *sql.DB, h http.Handler) http.Handler {
|
||||||
|
return middleware.EnsureValidSession(db, h)
|
||||||
|
}
|
||||||
15
handler/workout.go
Normal file
15
handler/workout.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"me-fit/service"
|
||||||
|
|
||||||
|
"database/sql"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func handleWorkout(db *sql.DB, router *http.ServeMux) {
|
||||||
|
router.Handle("/workout", auth(db, service.HandleWorkoutPage(db)))
|
||||||
|
router.Handle("POST /api/workout", auth(db, service.HandleWorkoutNewComp(db)))
|
||||||
|
router.Handle("GET /api/workout", auth(db, service.HandleWorkoutGetComp(db)))
|
||||||
|
router.Handle("DELETE /api/workout/{id}", auth(db, service.HandleWorkoutDeleteComp(db)))
|
||||||
|
}
|
||||||
5
main.go
5
main.go
@@ -1,10 +1,11 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"me-fit/handler"
|
||||||
"me-fit/utils"
|
"me-fit/utils"
|
||||||
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"log"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -34,7 +35,7 @@ func main() {
|
|||||||
|
|
||||||
var server = http.Server{
|
var server = http.Server{
|
||||||
Addr: ":8080",
|
Addr: ":8080",
|
||||||
Handler: getHandler(db),
|
Handler: handler.GetHandler(db),
|
||||||
}
|
}
|
||||||
slog.Info("Starting server on " + server.Addr)
|
slog.Info("Starting server on " + server.Addr)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user