mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-06 02:36:49 +00:00
refactor: server option to own filed
This commit is contained in:
parent
4fc580c93e
commit
6a15d1b32a
2 changed files with 55 additions and 49 deletions
54
internal/server/option.go
Normal file
54
internal/server/option.go
Normal 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
|
||||
}
|
||||
}
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in a new issue