Implement structured logging and enhance middleware with context-aware logging
This commit is contained in:
38
cmd/start.go
38
cmd/start.go
@ -1,11 +1,14 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"log"
|
||||
"context"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.coopcloud.tech/wiki-cafe/member-console/internal/auth"
|
||||
"git.coopcloud.tech/wiki-cafe/member-console/internal/logging"
|
||||
"git.coopcloud.tech/wiki-cafe/member-console/internal/middleware"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
@ -20,8 +23,18 @@ var startCmd = &cobra.Command{
|
||||
The server listens on port 8080 by default, unless a different port is specified using the --port flag.`,
|
||||
Args: cobra.NoArgs,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Retrieve the port value from Viper
|
||||
// Create base context for the application
|
||||
ctx := context.Background()
|
||||
|
||||
// Retrieve the configuration values from Viper
|
||||
port := viper.GetString("port")
|
||||
env := viper.GetString("env")
|
||||
|
||||
// Set up structured logging
|
||||
logger := logging.SetupLogger(env)
|
||||
|
||||
// Store logger in context
|
||||
ctx = logging.WithContext(ctx, logger)
|
||||
|
||||
// Create a new HTTP request router
|
||||
httpRequestRouter := http.NewServeMux()
|
||||
@ -29,7 +42,8 @@ var startCmd = &cobra.Command{
|
||||
// Set up authentication
|
||||
authConfig, err := auth.Setup()
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to set up authentication: %v", err)
|
||||
logger.Error("failed to set up authentication", slog.Any("error", err))
|
||||
return
|
||||
}
|
||||
|
||||
// Register auth handlers
|
||||
@ -42,7 +56,7 @@ var startCmd = &cobra.Command{
|
||||
middleware.RequestID(), // Generate a unique request ID
|
||||
middleware.MaxBodySize(1024*1024), // 1MB size limit
|
||||
middleware.SecureHeaders, // Set secure headers
|
||||
middleware.Logging, // Log requests
|
||||
middleware.Logging, // Log requests with structured logging
|
||||
authConfig.Middleware(), // OIDC authentication middleware
|
||||
)
|
||||
|
||||
@ -53,13 +67,23 @@ var startCmd = &cobra.Command{
|
||||
ReadTimeout: 2 * time.Second,
|
||||
WriteTimeout: 4 * time.Second,
|
||||
IdleTimeout: 8 * time.Second,
|
||||
MaxHeaderBytes: 1024 * 1024, // 1MB
|
||||
MaxHeaderBytes: 1024 * 1024, // 1MB
|
||||
BaseContext: func(_ net.Listener) context.Context { return ctx }, // Pass base context to all requests
|
||||
}
|
||||
|
||||
// Serve the components directory
|
||||
httpRequestRouter.Handle("/", http.FileServer(http.Dir("./components")))
|
||||
log.Println("Starting server on port", port)
|
||||
log.Fatal(server.ListenAndServe())
|
||||
|
||||
// Log server startup with structured logging
|
||||
logger.Info("starting server",
|
||||
slog.String("port", port),
|
||||
slog.String("environment", env),
|
||||
slog.String("address", "http://localhost:"+port))
|
||||
|
||||
// Start server and log any errors
|
||||
if err := server.ListenAndServe(); err != nil {
|
||||
logger.Error("server error", slog.Any("error", err))
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user