chore(deps): remove dependencies from handler package #287

Merged
tim merged 1 commits from update-dependencies into prod 2024-12-01 21:29:47 +00:00
5 changed files with 46 additions and 54 deletions

View File

@@ -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())

View File

@@ -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,
)
}

View File

@@ -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())
}

View File

@@ -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())

40
main.go
View File

@@ -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,
)
}