- Simplify cmd/server/main.go from 158 to 57 lines (64% reduction) - Move HTTP server creation, graceful shutdown, and context management to pkg/server - Add Run() method to encapsulate server lifecycle management - Maintain all existing functionality and OpenTelemetry integration - Improve separation of concerns and code organization This refactoring makes cmd/server/main.go a thin entrypoint while moving all server implementation details to the pkg/server package, following Go best practices for package organization. Add server and greet binaries to .gitignore
58 lines
1.5 KiB
Go
58 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
|
|
"DanceLessonsCoach/pkg/config"
|
|
"DanceLessonsCoach/pkg/server"
|
|
"github.com/rs/zerolog"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
func main() {
|
|
// Initialize Zerolog with default console format first
|
|
zerolog.SetGlobalLevel(zerolog.TraceLevel)
|
|
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
|
|
consoleWriter := zerolog.ConsoleWriter{Out: os.Stderr}
|
|
|
|
// Check if JSON logging is requested via environment variable
|
|
// This allows JSON logging even during config loading
|
|
jsonLogging := os.Getenv("DLC_LOGGING_JSON") == "true"
|
|
|
|
if jsonLogging {
|
|
// JSON output for structured logging
|
|
log.Logger = log.Output(os.Stderr)
|
|
} else {
|
|
// Console output for initial logging
|
|
log.Logger = log.Output(consoleWriter)
|
|
}
|
|
|
|
// Load configuration
|
|
cfg, err := config.LoadConfig()
|
|
if err != nil {
|
|
log.Fatal().Err(err).Msg("Failed to load configuration")
|
|
}
|
|
|
|
// Reconfigure logging based on loaded configuration (overrides env var)
|
|
if cfg.Logging.JSON {
|
|
// JSON output for structured logging
|
|
log.Logger = log.Output(os.Stderr)
|
|
} else {
|
|
// Keep console output
|
|
log.Logger = log.Output(consoleWriter)
|
|
}
|
|
|
|
log.Info().Bool("json_logging", cfg.Logging.JSON).Msg("Logging configured")
|
|
|
|
// Create readiness context to control readiness state
|
|
readyCtx, readyCancel := context.WithCancel(context.Background())
|
|
defer readyCancel()
|
|
|
|
// Create and run server
|
|
server := server.NewServer(cfg, readyCtx)
|
|
if err := server.Run(); err != nil {
|
|
log.Fatal().Err(err).Msg("Server failed")
|
|
}
|
|
}
|