From 691b72418947b1ce6ab529198296dde7e3810416 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Sat, 13 Apr 2024 19:15:42 +0200 Subject: [PATCH] set debug --- internal/server/server.go | 19 +++++++++++++------ main.go | 5 ++++- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/internal/server/server.go b/internal/server/server.go index 7e1d37d..3714bc2 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -8,7 +8,6 @@ import ( "net/http" "os" "os/signal" - "strings" "time" "github.com/a-h/templ" @@ -31,8 +30,9 @@ type Server struct { } type options struct { - port *int - fs *embed.FS + port *int + fs *embed.FS + debug *bool } type Option func(*options) error @@ -54,6 +54,13 @@ func WithFileSystem(fs embed.FS) Option { } } +func WithDebug(debug bool) Option { + return func(o *options) error { + o.debug = &debug + return nil + } +} + func New(bs *booking.Service, ps *pdf.PdfService, hc *config.Host, opts ...Option) (*Server, error) { option := new(options) for _, opt := range opts { @@ -64,7 +71,7 @@ func New(bs *booking.Service, ps *pdf.PdfService, hc *config.Host, opts ...Optio } s := &Server{ - Router: NewRouter(*option.fs), + Router: NewRouter(*option.fs, *option.debug), bs: bs, ps: ps, hc: hc, @@ -103,11 +110,11 @@ func renderTempl(c echo.Context, status int, t templ.Component) error { return nil } -func NewRouter(fs embed.FS) *echo.Echo { +func NewRouter(fs embed.FS, debug bool) *echo.Echo { e := echo.New() // config e.HideBanner = true - e.Debug = strings.ToLower(os.Getenv("DEBUG")) == "true" + e.Debug = debug e.HTTPErrorHandler = func(err error, c echo.Context) { if hub := sentryecho.GetHubFromContext(c); hub != nil { hub.WithScope(func(s *sentry.Scope) { diff --git a/main.go b/main.go index 6e679ee..279c4b6 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "os" "os/signal" "strconv" + "strings" "github.com/getsentry/sentry-go" "github.com/joho/godotenv" @@ -65,7 +66,9 @@ func run(ctx context.Context, getEnv func(string) string) error { if err != nil { return fmt.Errorf("error parsing PORT env %s\n", err) } - srv, err := server.New(booking.NewService(db), ps, config.NewHost(), server.WithPort(port), server.WithFileSystem(static)) + srv, err := server.New( + booking.NewService(db), ps, config.NewHost(), + server.WithPort(port), server.WithFileSystem(static), server.WithDebug(strings.ToLower(getEnv("DEBUG")) == "true")) if err != nil { return fmt.Errorf("error starting server %s\n", err) }