feat(middlewares): use closures instead of structs for middleware constructors

This commit is contained in:
Guz
2024-12-13 16:21:47 -03:00
parent 23ac6d6220
commit c2bbd80dce
4 changed files with 38 additions and 54 deletions

View File

@@ -68,11 +68,9 @@ func (a *App) setLogger() {
}
func (a *App) setServer() {
mlogger := middleware.NewLoggerMiddleware(a.logger)
r := router.NewRouter()
r.Use(mlogger.Wrap)
r.Use(middleware.NewLoggerMiddleware(a.logger))
if configs.DEVELOPMENT {
a.logger.Info("RUNNING IN DEVELOPMENT MODE")

View File

@@ -11,8 +11,7 @@ import (
func Routes(logger *slog.Logger) router.Router {
r := router.NewRouter()
mErrors := rerrors.NewErrorMiddleware(ErrorPage{}.Component, logger)
r.Use(mErrors.Wrap)
r.Use(rerrors.NewErrorMiddleware(ErrorPage{}.Component, logger))
r.Handle("/dashboard", &Dashboard{})

View File

@@ -13,10 +13,6 @@ func DevMiddleware(next http.Handler) http.Handler {
})
}
type LoggerMiddleware struct {
log *slog.Logger
}
type loggerReponse struct {
http.ResponseWriter
status int
@@ -27,42 +23,40 @@ func (lr *loggerReponse) WriteHeader(s int) {
lr.ResponseWriter.WriteHeader(s)
}
func NewLoggerMiddleware(l *slog.Logger) *LoggerMiddleware {
func NewLoggerMiddleware(l *slog.Logger) Middleware {
l = l.WithGroup("logger_middleware")
return &LoggerMiddleware{l}
}
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
id := randHash(5)
func (l *LoggerMiddleware) Wrap(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
id := randHash(5)
l.Info("NEW REQUEST",
slog.String("id", id),
slog.String("status", "xxx"),
slog.String("method", r.Method),
slog.String("path", r.URL.Path),
)
l.log.Info("NEW REQUEST",
slog.String("id", id),
slog.String("status", "xxx"),
slog.String("method", r.Method),
slog.String("path", r.URL.Path),
)
lw := &loggerReponse{w, http.StatusOK}
next.ServeHTTP(lw, r)
lw := &loggerReponse{w, http.StatusOK}
next.ServeHTTP(lw, r)
if lw.status >= 400 {
l.Warn("ERR REQUEST",
slog.String("id", id),
slog.Int("status", lw.status),
slog.String("method", r.Method),
slog.String("path", r.URL.Path),
)
return
}
if lw.status >= 400 {
l.log.Warn("ERR REQUEST",
l.Info("END REQUEST",
slog.String("id", id),
slog.Int("status", lw.status),
slog.String("method", r.Method),
slog.String("path", r.URL.Path),
)
return
}
l.log.Info("END REQUEST",
slog.String("id", id),
slog.Int("status", lw.status),
slog.String("method", r.Method),
slog.String("path", r.URL.Path),
)
})
})
}
}
const HASH_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

View File

@@ -8,6 +8,7 @@ import (
"net/http"
"strings"
"forge.capytal.company/capytalcode/project-comicverse/lib/middleware"
"github.com/a-h/templ"
)
@@ -127,17 +128,11 @@ func (h ErrorDisplayer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}
type ErrorMiddleware struct {
page ErrorMiddlewarePage
notfound ErrorMiddlewarePage
log *slog.Logger
}
func NewErrorMiddleware(
p ErrorMiddlewarePage,
l *slog.Logger,
notfound ...ErrorMiddlewarePage,
) *ErrorMiddleware {
) middleware.Middleware {
var nf ErrorMiddlewarePage
if len(notfound) > 0 {
nf = notfound[0]
@@ -147,20 +142,18 @@ func NewErrorMiddleware(
l = l.WithGroup("error_middleware")
return &ErrorMiddleware{p, nf, l}
}
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Header.Set(ERROR_MIDDLEWARE_HEADER, "enable")
func (m *ErrorMiddleware) Wrap(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Header.Set(ERROR_MIDDLEWARE_HEADER, "enable")
if uerr := r.URL.Query().Get("error"); uerr != "" && prefersHtml(r.Header) {
ErrorDisplayer{l, nf}.ServeHTTP(w, r)
return
}
if uerr := r.URL.Query().Get("error"); uerr != "" && prefersHtml(r.Header) {
ErrorDisplayer{m.log, m.page}.ServeHTTP(w, r)
return
}
next.ServeHTTP(w, r)
})
next.ServeHTTP(w, r)
})
}
}
func prefersHtml(h http.Header) bool {