2025-04-15 23:49:28 +03:00

27 lines
582 B
Go

package middleware
import (
"log/slog"
"net/http"
)
func AuthMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
if token == "foobar" {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
next.ServeHTTP(w, r)
})
}
func LoggingMiddleware(next http.Handler, logger *slog.Logger) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logger.Info("🚀 handle %s %s", r.Method, r.URL.Path)
next.ServeHTTP(w, r)
})
}