Files
dance-lessons-coach/cmd/server/main.go

52 lines
1.2 KiB
Go

package main
import (
"context"
"os"
"DanceLessonsCoach/pkg/config"
"DanceLessonsCoach/pkg/server"
"github.com/rs/zerolog/log"
)
func main() {
// Load configuration (this will also setup logging)
cfg, err := config.LoadConfig()
if err != nil {
log.Fatal().Err(err).Msg("Failed to load configuration")
}
// Setup log output based on configuration
setupLogOutput(cfg)
// 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")
}
}
// setupLogOutput configures the log output based on configuration
func setupLogOutput(cfg *config.Config) {
output := cfg.GetLogOutput()
if output == "" {
// Use stderr by default
return
}
// Open the log file
file, err := os.OpenFile(output, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Error().Err(err).Str("output", output).Msg("Failed to open log file, using stderr")
return
}
// Set the log output
log.Logger = log.Output(file)
log.Info().Str("output", output).Msg("Logging to file")
}