From 5d42a5aefe13b3cb20165ada66f90f7c9b9e8957 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Fri, 3 Oct 2025 20:09:27 +0200 Subject: [PATCH] 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. --- internal/driver/parser/client.go | 24 ++++++++++++------------ main.go | 22 +++++++++++----------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/internal/driver/parser/client.go b/internal/driver/parser/client.go index ca3e299..e398f7b 100644 --- a/internal/driver/parser/client.go +++ b/internal/driver/parser/client.go @@ -25,18 +25,18 @@ func NewBookingAgentParser() *BookingAgentParser { { "data": { - "arrival_date": "YYYY-MM-DD", - "booking_fees": "number as string", - "booking_id": "string", - "departure_date": "YYYY-MM-DD", - "stay_length": int, - "guest_email": "string", - "guest_name": "string", - "guest_number": int, - "guest_phone": "string", - "room_booked": "string", - "standard_rate": "number as string", - "special_requests": "string" + "arrival_date": "YYYY-MM-DD", + "booking_fees": "number as string", + "booking_id": "string", + "departure_date": "YYYY-MM-DD", + "stay_length": int, + "guest_email": "string", + "guest_name": "string", + "guest_number": int, + "guest_phone": "string", + "room_booked": "string", + "standard_rate": "number as string", + "special_requests": "string" } } diff --git a/main.go b/main.go index ed17a3b..2b96a70 100644 --- a/main.go +++ b/main.go @@ -22,26 +22,26 @@ import ( ) func main() { - ctx := context.Background() - - 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) + ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) defer cancel() + baseLogger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{AddSource: true})) + appConfig, err := config.New(ctx) if err != nil { - return err + baseLogger.Error("configuration error", slog.Any("error", err)) } appLogger := logger.New(appConfig.LogLevel) 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 if err := sentry.Init(sentry.ClientOptions{ Dsn: appConfig.SentryDsn,