From 6a15d1b32a1b289faebda050af0e1bdc2791ba27 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Thu, 15 Aug 2024 23:48:35 +0200 Subject: [PATCH] refactor: server option to own filed --- internal/server/option.go | 54 +++++++++++++++++++++++++++++++++++++++ internal/server/server.go | 50 +----------------------------------- 2 files changed, 55 insertions(+), 49 deletions(-) create mode 100644 internal/server/option.go diff --git a/internal/server/option.go b/internal/server/option.go new file mode 100644 index 0000000..a983bbf --- /dev/null +++ b/internal/server/option.go @@ -0,0 +1,54 @@ +package server + +import ( + "embed" + "errors" +) + +type options struct { + port *int + fs *embed.FS + debug *bool + secretKey *string + origins []string +} + +type Option func(*options) error + +func WithPort(port int) Option { + return func(o *options) error { + if port <= 0 { + return errors.New("port should be positive") + } + o.port = &port + return nil + } +} + +func WithFileSystem(fs embed.FS) Option { + return func(o *options) error { + o.fs = &fs + return nil + } +} + +func WithDebug(debug bool) Option { + return func(o *options) error { + o.debug = &debug + return nil + } +} + +func WithSecretKey(secretKey string) Option { + return func(o *options) error { + o.secretKey = &secretKey + return nil + } +} + +func WithOrigins(origins []string) Option { + return func(o *options) error { + o.origins = origins + return nil + } +} diff --git a/internal/server/server.go b/internal/server/server.go index 9de24cd..3186b78 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -34,54 +34,6 @@ type Server struct { addr string } -type options struct { - port *int - fs *embed.FS - debug *bool - secretKey *string - origins []string -} - -type Option func(*options) error - -func WithPort(port int) Option { - return func(o *options) error { - if port <= 0 { - return errors.New("port should be positive") - } - o.port = &port - return nil - } -} - -func WithFileSystem(fs embed.FS) Option { - return func(o *options) error { - o.fs = &fs - return nil - } -} - -func WithDebug(debug bool) Option { - return func(o *options) error { - o.debug = &debug - return nil - } -} - -func WithSecretKey(secretKey string) Option { - return func(o *options) error { - o.secretKey = &secretKey - return nil - } -} - -func WithOrigins(origins []string) Option { - return func(o *options) error { - o.origins = origins - return nil - } -} - func New(bs *booking.Service, as *auth.Service, ps *pdf.PdfService, cs *calendar.Service, hc *config.Host, opts ...Option) (*Server, error) { option := new(options) for _, opt := range opts { @@ -125,7 +77,7 @@ func (s Server) Start(c context.Context) { func NewRouter(fs embed.FS, debug bool, secret string, origins []string) *echo.Echo { e := echo.New() // config - e.HideBanner = true + e.HideBanner = !debug e.Debug = debug e.HTTPErrorHandler = func(err error, c echo.Context) { if hub := sentryecho.GetHubFromContext(c); hub != nil {