refactor: server option to own filed

This commit is contained in:
Ruidy 2024-08-15 23:48:35 +02:00
parent 4fc580c93e
commit 6a15d1b32a
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
2 changed files with 55 additions and 49 deletions

54
internal/server/option.go Normal file
View file

@ -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
}
}

View file

@ -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 {