refactor: simplify main entry and improve logging

Refactors main.go to streamline context and logger initialization.
Moves signal handling directly into main, sets up a base logger for
early errors, and updates error handling to use structured logging.
No functional changes to application logic.
This commit is contained in:
Ruidy 2025-10-03 20:09:27 +02:00
parent ac94faedb0
commit 5d42a5aefe
No known key found for this signature in database
GPG key ID: 705C24D202990805
2 changed files with 23 additions and 23 deletions

View file

@ -25,18 +25,18 @@ func NewBookingAgentParser() *BookingAgentParser {
{ {
"data": { "data": {
"arrival_date": "YYYY-MM-DD", "arrival_date": "YYYY-MM-DD",
"booking_fees": "number as string", "booking_fees": "number as string",
"booking_id": "string", "booking_id": "string",
"departure_date": "YYYY-MM-DD", "departure_date": "YYYY-MM-DD",
"stay_length": int, "stay_length": int,
"guest_email": "string", "guest_email": "string",
"guest_name": "string", "guest_name": "string",
"guest_number": int, "guest_number": int,
"guest_phone": "string", "guest_phone": "string",
"room_booked": "string", "room_booked": "string",
"standard_rate": "number as string", "standard_rate": "number as string",
"special_requests": "string" "special_requests": "string"
} }
} }

22
main.go
View file

@ -22,26 +22,26 @@ import (
) )
func main() { func main() {
ctx := context.Background() ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
if err := run(ctx); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}
func run(c context.Context) error {
ctx, cancel := signal.NotifyContext(c, os.Interrupt)
defer cancel() defer cancel()
baseLogger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{AddSource: true}))
appConfig, err := config.New(ctx) appConfig, err := config.New(ctx)
if err != nil { if err != nil {
return err baseLogger.Error("configuration error", slog.Any("error", err))
} }
appLogger := logger.New(appConfig.LogLevel) appLogger := logger.New(appConfig.LogLevel)
slog.SetDefault(appLogger) slog.SetDefault(appLogger)
if err := run(appConfig, appLogger); err != nil {
appLogger.Error("server exited", slog.Any("error", err))
os.Exit(1)
}
}
func run(appConfig *config.Config, appLogger *slog.Logger) error {
// init sentry // init sentry
if err := sentry.Init(sentry.ClientOptions{ if err := sentry.Init(sentry.ClientOptions{
Dsn: appConfig.SentryDsn, Dsn: appConfig.SentryDsn,