set debug

This commit is contained in:
Ruidy 2024-04-13 19:15:42 +02:00
parent efccace761
commit 691b724189
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
2 changed files with 17 additions and 7 deletions

View file

@ -8,7 +8,6 @@ import (
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
"strings"
"time" "time"
"github.com/a-h/templ" "github.com/a-h/templ"
@ -31,8 +30,9 @@ type Server struct {
} }
type options struct { type options struct {
port *int port *int
fs *embed.FS fs *embed.FS
debug *bool
} }
type Option func(*options) error 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) { func New(bs *booking.Service, ps *pdf.PdfService, hc *config.Host, opts ...Option) (*Server, error) {
option := new(options) option := new(options)
for _, opt := range opts { for _, opt := range opts {
@ -64,7 +71,7 @@ func New(bs *booking.Service, ps *pdf.PdfService, hc *config.Host, opts ...Optio
} }
s := &Server{ s := &Server{
Router: NewRouter(*option.fs), Router: NewRouter(*option.fs, *option.debug),
bs: bs, bs: bs,
ps: ps, ps: ps,
hc: hc, hc: hc,
@ -103,11 +110,11 @@ func renderTempl(c echo.Context, status int, t templ.Component) error {
return nil return nil
} }
func NewRouter(fs embed.FS) *echo.Echo { func NewRouter(fs embed.FS, debug bool) *echo.Echo {
e := echo.New() e := echo.New()
// config // config
e.HideBanner = true e.HideBanner = true
e.Debug = strings.ToLower(os.Getenv("DEBUG")) == "true" e.Debug = debug
e.HTTPErrorHandler = func(err error, c echo.Context) { e.HTTPErrorHandler = func(err error, c echo.Context) {
if hub := sentryecho.GetHubFromContext(c); hub != nil { if hub := sentryecho.GetHubFromContext(c); hub != nil {
hub.WithScope(func(s *sentry.Scope) { hub.WithScope(func(s *sentry.Scope) {

View file

@ -7,6 +7,7 @@ import (
"os" "os"
"os/signal" "os/signal"
"strconv" "strconv"
"strings"
"github.com/getsentry/sentry-go" "github.com/getsentry/sentry-go"
"github.com/joho/godotenv" "github.com/joho/godotenv"
@ -65,7 +66,9 @@ func run(ctx context.Context, getEnv func(string) string) error {
if err != nil { if err != nil {
return fmt.Errorf("error parsing PORT env %s\n", err) 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 { if err != nil {
return fmt.Errorf("error starting server %s\n", err) return fmt.Errorf("error starting server %s\n", err)
} }