From 48ec7b64ac8a261908865cea200e0f7533c77625 Mon Sep 17 00:00:00 2001 From: Tim Wundenberg Date: Sun, 1 Dec 2024 21:50:22 +0100 Subject: [PATCH] chore(deps): remove dependencies from handler package --- handler/auth.go | 4 ++-- handler/default.go | 46 ---------------------------------------- handler/index_and_404.go | 4 ++-- handler/workout.go | 6 +++--- main.go | 40 +++++++++++++++++++++++++++++++++- 5 files changed, 46 insertions(+), 54 deletions(-) delete mode 100644 handler/default.go diff --git a/handler/auth.go b/handler/auth.go index 1305ba4..4ea74d4 100644 --- a/handler/auth.go +++ b/handler/auth.go @@ -14,7 +14,7 @@ import ( ) type HandlerAuth interface { - handle(router *http.ServeMux) + Handle(router *http.ServeMux) } type HandlerAuthImpl struct { @@ -31,7 +31,7 @@ func NewHandlerAuth(db *sql.DB, service service.AuthService, serverSettings *typ } } -func (handler HandlerAuthImpl) handle(router *http.ServeMux) { +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", handler.handleSignInPage()) router.Handle("/auth/signup", handler.handleSignUpPage()) diff --git a/handler/default.go b/handler/default.go deleted file mode 100644 index 230d061..0000000 --- a/handler/default.go +++ /dev/null @@ -1,46 +0,0 @@ -package handler - -import ( - "me-fit/db" - "me-fit/middleware" - "me-fit/service" - "me-fit/types" - - "database/sql" - "net/http" -) - -func GetHandler(d *sql.DB, serverSettings *types.ServerSettings) http.Handler { - var router = http.NewServeMux() - - authDb := db.NewAuthDbSqlite(d) - workoutDb := db.NewWorkoutDbSqlite(d) - - randomService := service.NewRandomServiceImpl() - clockService := service.NewClockServiceImpl() - mailService := service.NewMailServiceImpl(serverSettings) - authService := service.NewAuthServiceImpl(authDb, randomService, clockService, mailService, serverSettings) - workoutService := service.NewWorkoutServiceImpl(workoutDb, randomService, clockService, mailService, serverSettings) - - indexHandler := NewIndexHandler(d, authService, serverSettings) - authHandler := NewHandlerAuth(d, authService, serverSettings) - workoutHandler := NewWorkoutHandler(d, workoutService, authService, serverSettings) - - indexHandler.handle(router) - - // Serve static files (CSS, JS and images) - router.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static/")))) - - workoutHandler.handle(router) - - authHandler.handle(router) - - return middleware.Wrapper( - router, - middleware.Log, - middleware.ContentSecurityPolicy, - middleware.Cors(serverSettings), - middleware.Corp, - middleware.Coop, - ) -} diff --git a/handler/index_and_404.go b/handler/index_and_404.go index 57e2229..dd832ab 100644 --- a/handler/index_and_404.go +++ b/handler/index_and_404.go @@ -13,7 +13,7 @@ import ( ) type IndexHandler interface { - handle(router *http.ServeMux) + Handle(router *http.ServeMux) } type IndexHandlerImpl struct { @@ -30,7 +30,7 @@ func NewIndexHandler(db *sql.DB, service service.AuthService, serverSettings *ty } } -func (handler IndexHandlerImpl) handle(router *http.ServeMux) { +func (handler IndexHandlerImpl) Handle(router *http.ServeMux) { router.Handle("/", handler.handleIndexAnd404()) } diff --git a/handler/workout.go b/handler/workout.go index 0139639..30a4379 100644 --- a/handler/workout.go +++ b/handler/workout.go @@ -15,7 +15,7 @@ import ( ) type WorkoutHandler interface { - handle(router *http.ServeMux) + Handle(router *http.ServeMux) } type WorkoutHandlerImpl struct { @@ -25,7 +25,7 @@ type WorkoutHandlerImpl struct { serverSettings *types.ServerSettings } -func NewWorkoutHandler(db *sql.DB, service service.WorkoutService, auth service.AuthService, serverSettings *types.ServerSettings) HandlerAuth { +func NewWorkoutHandler(db *sql.DB, service service.WorkoutService, auth service.AuthService, serverSettings *types.ServerSettings) WorkoutHandler { return WorkoutHandlerImpl{ db: db, service: service, @@ -34,7 +34,7 @@ func NewWorkoutHandler(db *sql.DB, service service.WorkoutService, auth service. } } -func (handler WorkoutHandlerImpl) handle(router *http.ServeMux) { +func (handler WorkoutHandlerImpl) Handle(router *http.ServeMux) { router.Handle("/workout", handler.handleWorkoutPage()) router.Handle("POST /api/workout", handler.handleAddWorkout()) router.Handle("GET /api/workout", handler.handleGetWorkout()) diff --git a/main.go b/main.go index 89fe4fd..b3b1b61 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,10 @@ package main import ( + "me-fit/db" "me-fit/handler" + "me-fit/middleware" + "me-fit/service" "me-fit/types" "me-fit/utils" @@ -64,7 +67,7 @@ func run(ctx context.Context, db *sql.DB, env func(string) string) { httpServer := &http.Server{ Addr: ":" + serverSettings.Port, - Handler: handler.GetHandler(db, serverSettings), + Handler: createHandler(db, serverSettings), } go startServer(httpServer) @@ -99,3 +102,38 @@ func shutdownServer(s *http.Server, ctx context.Context, wg *sync.WaitGroup) { slog.Info("Gracefully stopped http server on " + s.Addr) } } + +func createHandler(d *sql.DB, serverSettings *types.ServerSettings) http.Handler { + var router = http.NewServeMux() + + authDb := db.NewAuthDbSqlite(d) + workoutDb := db.NewWorkoutDbSqlite(d) + + randomService := service.NewRandomServiceImpl() + clockService := service.NewClockServiceImpl() + mailService := service.NewMailServiceImpl(serverSettings) + authService := service.NewAuthServiceImpl(authDb, randomService, clockService, mailService, serverSettings) + workoutService := service.NewWorkoutServiceImpl(workoutDb, randomService, clockService, mailService, serverSettings) + + indexHandler := handler.NewIndexHandler(d, authService, serverSettings) + authHandler := handler.NewHandlerAuth(d, authService, serverSettings) + workoutHandler := handler.NewWorkoutHandler(d, workoutService, authService, serverSettings) + + indexHandler.Handle(router) + + // Serve static files (CSS, JS and images) + router.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static/")))) + + workoutHandler.Handle(router) + + authHandler.Handle(router) + + return middleware.Wrapper( + router, + middleware.Log, + middleware.ContentSecurityPolicy, + middleware.Cors(serverSettings), + middleware.Corp, + middleware.Coop, + ) +}