-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathfactory.go
59 lines (48 loc) · 1.99 KB
/
factory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package httpserver
import (
"github.com/labstack/echo/v4"
)
// HttpServerFactory is the interface for [echo.Echo] factories.
type HttpServerFactory interface {
Create(options ...HttpServerOption) (*echo.Echo, error)
}
// DefaultHttpServerFactory is the default [HttpServerFactory] implementation.
type DefaultHttpServerFactory struct{}
// NewDefaultHttpServerFactory returns a [DefaultHttpServerFactory], implementing [HttpServerFactory].
func NewDefaultHttpServerFactory() HttpServerFactory {
return &DefaultHttpServerFactory{}
}
// Create returns a new [echo.Echo], and accepts a list of [HttpServerOption].
// For example:
//
// var server, _ = httpserver.NewDefaultHttpServerFactory().Create()
//
// is equivalent to:
//
// var server, _ = httpserver.NewDefaultHttpServerFactory().Create(
// httpserver.WithDebug(false), // debug disabled by default
// httpserver.WithBanner(false), // banner disabled by default
// httpserver.WithLogger(log.New("default")), // echo default logger
// httpserver.WithBinder(&echo.DefaultBinder{}), // echo default binder
// httpserver.WithJsonSerializer(&echo.DefaultJSONSerializer{}), // echo default json serializer
// httpserver.WithHttpErrorHandler(nil), // echo default error handler
// )
func (f *DefaultHttpServerFactory) Create(options ...HttpServerOption) (*echo.Echo, error) {
appliedOpts := DefaultHttpServerOptions()
for _, applyOpt := range options {
applyOpt(&appliedOpts)
}
httpServer := echo.New()
httpServer.Debug = appliedOpts.Debug
httpServer.HideBanner = !appliedOpts.Banner
httpServer.Logger = appliedOpts.Logger
httpServer.Binder = appliedOpts.Binder
httpServer.JSONSerializer = appliedOpts.JsonSerializer
if appliedOpts.HttpErrorHandler != nil {
httpServer.HTTPErrorHandler = appliedOpts.HttpErrorHandler
}
if appliedOpts.Renderer != nil {
httpServer.Renderer = appliedOpts.Renderer
}
return httpServer, nil
}