chore(deps): remove dependencies from handler package
All checks were successful
Build Docker Image / Build-Docker-Image (push) Successful in 42s
Build and Push Docker Image / Build-And-Push-Docker-Image (push) Successful in 48s

This commit was merged in pull request #287.
This commit is contained in:
2024-12-01 21:50:22 +01:00
parent e201ac7b2c
commit 48ec7b64ac
5 changed files with 46 additions and 54 deletions

View File

@@ -14,7 +14,7 @@ import (
) )
type HandlerAuth interface { type HandlerAuth interface {
handle(router *http.ServeMux) Handle(router *http.ServeMux)
} }
type HandlerAuthImpl struct { 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 // 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/signin", handler.handleSignInPage())
router.Handle("/auth/signup", handler.handleSignUpPage()) 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 { type IndexHandler interface {
handle(router *http.ServeMux) Handle(router *http.ServeMux)
} }
type IndexHandlerImpl struct { 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()) router.Handle("/", handler.handleIndexAnd404())
} }

View File

@@ -15,7 +15,7 @@ import (
) )
type WorkoutHandler interface { type WorkoutHandler interface {
handle(router *http.ServeMux) Handle(router *http.ServeMux)
} }
type WorkoutHandlerImpl struct { type WorkoutHandlerImpl struct {
@@ -25,7 +25,7 @@ type WorkoutHandlerImpl struct {
serverSettings *types.ServerSettings 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{ return WorkoutHandlerImpl{
db: db, db: db,
service: service, 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("/workout", handler.handleWorkoutPage())
router.Handle("POST /api/workout", handler.handleAddWorkout()) router.Handle("POST /api/workout", handler.handleAddWorkout())
router.Handle("GET /api/workout", handler.handleGetWorkout()) router.Handle("GET /api/workout", handler.handleGetWorkout())

40
main.go
View File

@@ -1,7 +1,10 @@
package main package main
import ( import (
"me-fit/db"
"me-fit/handler" "me-fit/handler"
"me-fit/middleware"
"me-fit/service"
"me-fit/types" "me-fit/types"
"me-fit/utils" "me-fit/utils"
@@ -64,7 +67,7 @@ func run(ctx context.Context, db *sql.DB, env func(string) string) {
httpServer := &http.Server{ httpServer := &http.Server{
Addr: ":" + serverSettings.Port, Addr: ":" + serverSettings.Port,
Handler: handler.GetHandler(db, serverSettings), Handler: createHandler(db, serverSettings),
} }
go startServer(httpServer) 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) 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,
)
}