Add logging to app package

This commit is contained in:
2024-12-15 16:46:29 +01:00
parent ab00276f0d
commit a32e0957ed
4 changed files with 90 additions and 15 deletions

View File

@@ -4,6 +4,7 @@ import (
"novamd/internal/auth"
"novamd/internal/context"
"novamd/internal/handlers"
"novamd/internal/logging"
"time"
"github.com/go-chi/chi/v5"
@@ -19,6 +20,7 @@ import (
// setupRouter creates and configures the chi router with middleware and routes
func setupRouter(o Options) *chi.Mux {
logging.Debug("Setting up router")
r := chi.NewRouter()
// Basic middleware
@@ -29,6 +31,7 @@ func setupRouter(o Options) *chi.Mux {
r.Use(middleware.Timeout(30 * time.Second))
// Security headers
logging.Debug("Setting up security headers")
r.Use(secure.New(secure.Options{
SSLRedirect: false,
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
@@ -36,6 +39,7 @@ func setupRouter(o Options) *chi.Mux {
}).Handler)
// CORS if origins are configured
logging.Debug("Setting up CORS")
if len(o.Config.CORSOrigins) > 0 {
r.Use(cors.Handler(cors.Options{
AllowedOrigins: o.Config.CORSOrigins,
@@ -48,6 +52,7 @@ func setupRouter(o Options) *chi.Mux {
}
// Initialize auth middleware and handler
logging.Debug("Setting up authentication middleware")
authMiddleware := auth.NewMiddleware(o.JWTManager, o.SessionManager, o.CookieService)
handler := &handlers.Handler{
DB: o.Database,
@@ -55,12 +60,14 @@ func setupRouter(o Options) *chi.Mux {
}
if o.Config.IsDevelopment {
logging.Debug("Setting up Swagger docs")
r.Get("/swagger/*", httpSwagger.Handler(
httpSwagger.URL("/swagger/doc.json"), // The URL pointing to API definition
))
}
// API routes
logging.Debug("Setting up API routes")
r.Route("/api/v1", func(r chi.Router) {
// Rate limiting for API routes
if o.Config.RateLimitRequests > 0 {
@@ -147,6 +154,7 @@ func setupRouter(o Options) *chi.Mux {
})
// Handle all other routes with static file server
logging.Debug("Setting up static file server")
r.Get("/*", handlers.NewStaticHandler(o.Config.StaticPath).ServeHTTP)
return r