fix: restructure env handling for better testing capabillities #181
All checks were successful
Build Docker Image / Explore-Gitea-Actions (push) Successful in 47s
Build and Push Docker Image / Explore-Gitea-Actions (push) Successful in 55s

This commit was merged in pull request #195.
This commit is contained in:
2024-09-29 23:51:23 +02:00
parent 25e82be339
commit a70138f2f7
15 changed files with 191 additions and 152 deletions

View File

@@ -2,11 +2,12 @@ package handler
import (
"me-fit/service"
"me-fit/types"
"me-fit/utils"
"time"
"database/sql"
"net/http"
"time"
)
type HandlerAuth interface {
@@ -14,33 +15,35 @@ type HandlerAuth interface {
}
type HandlerAuthImpl struct {
db *sql.DB
service service.ServiceAuth
db *sql.DB
service service.ServiceAuth
serverSettings *types.ServerSettings
}
func NewHandlerAuth(db *sql.DB, service service.ServiceAuth) HandlerAuth {
func NewHandlerAuth(db *sql.DB, service service.ServiceAuth, serverSettings *types.ServerSettings) HandlerAuth {
return HandlerAuthImpl{
db: db,
service: service,
db: db,
service: service,
serverSettings: serverSettings,
}
}
func (handler HandlerAuthImpl) handle(router *http.ServeMux) {
// 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(handler.db))
router.Handle("/auth/signup", service.HandleSignUpPage(handler.db))
router.Handle("/auth/verify", service.HandleSignUpVerifyPage(handler.db)) // Hint for the user to verify their email
router.Handle("/auth/delete-account", service.HandleDeleteAccountPage(handler.db))
router.Handle("/auth/signin", service.HandleSignInPage(handler.db, handler.serverSettings))
router.Handle("/auth/signup", service.HandleSignUpPage(handler.db, handler.serverSettings))
router.Handle("/auth/verify", service.HandleSignUpVerifyPage(handler.db, handler.serverSettings)) // Hint for the user to verify their email
router.Handle("/auth/delete-account", service.HandleDeleteAccountPage(handler.db, handler.serverSettings))
router.Handle("/auth/verify-email", service.HandleSignUpVerifyResponsePage(handler.db)) // The link contained in the email
router.Handle("/auth/change-password", service.HandleChangePasswordPage(handler.db))
router.Handle("/auth/reset-password", service.HandleResetPasswordPage(handler.db))
router.Handle("/api/auth/signup", service.HandleSignUpComp(handler.db))
router.Handle("/auth/change-password", service.HandleChangePasswordPage(handler.db, handler.serverSettings))
router.Handle("/auth/reset-password", service.HandleResetPasswordPage(handler.db, handler.serverSettings))
router.Handle("/api/auth/signup", service.HandleSignUpComp(handler.db, handler.serverSettings))
router.Handle("/api/auth/signin", handler.handleSignIn())
router.Handle("/api/auth/signout", service.HandleSignOutComp(handler.db))
router.Handle("/api/auth/delete-account", service.HandleDeleteAccountComp(handler.db))
router.Handle("/api/auth/verify-resend", service.HandleVerifyResendComp(handler.db))
router.Handle("/api/auth/delete-account", service.HandleDeleteAccountComp(handler.db, handler.serverSettings))
router.Handle("/api/auth/verify-resend", service.HandleVerifyResendComp(handler.db, handler.serverSettings))
router.Handle("/api/auth/change-password", service.HandleChangePasswordComp(handler.db))
router.Handle("/api/auth/reset-password", service.HandleResetPasswordComp(handler.db))
router.Handle("/api/auth/reset-password", service.HandleResetPasswordComp(handler.db, handler.serverSettings))
router.Handle("/api/auth/reset-password-actual", service.HandleActualResetPasswordComp(handler.db))
}

View File

@@ -4,26 +4,27 @@ import (
"me-fit/db"
"me-fit/middleware"
"me-fit/service"
"me-fit/types"
"database/sql"
"net/http"
)
func GetHandler(d *sql.DB) http.Handler {
func GetHandler(d *sql.DB, serverSettings *types.ServerSettings) http.Handler {
var router = http.NewServeMux()
router.HandleFunc("/", service.HandleIndexAnd404(d))
router.HandleFunc("/", service.HandleIndexAnd404(d, serverSettings))
handlerAuth := NewHandlerAuth(d, service.NewServiceAuthImpl(db.NewDbAuthSqlite(d)))
handlerAuth := NewHandlerAuth(d, service.NewServiceAuthImpl(db.NewDbAuthSqlite(d)), serverSettings)
// Serve static files (CSS, JS and images)
router.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
handleWorkout(d, router)
handleWorkout(d, router, serverSettings)
handlerAuth.handle(router)
return middleware.Logging(middleware.EnableCors(router))
return middleware.Logging(middleware.EnableCors(serverSettings, router))
}
func auth(db *sql.DB, h http.Handler) http.Handler {

View File

@@ -2,13 +2,14 @@ package handler
import (
"me-fit/service"
"me-fit/types"
"database/sql"
"net/http"
)
func handleWorkout(db *sql.DB, router *http.ServeMux) {
router.Handle("/workout", auth(db, service.HandleWorkoutPage(db)))
func handleWorkout(db *sql.DB, router *http.ServeMux, serverSettings *types.ServerSettings) {
router.Handle("/workout", auth(db, service.HandleWorkoutPage(db, serverSettings)))
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)))