-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
61 lines (48 loc) · 1.09 KB
/
build.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
60
61
package goserver
import (
"context"
"net/http"
)
type Builder struct {
config *Config
server *http.Server
handler http.Handler
options Options
ctx context.Context
cancel context.CancelFunc
}
func NewBuilder() *Builder {
return &Builder{}
}
func (b *Builder) WithCustomServer(server *http.Server) *Builder {
b.server = server
return b
}
func (b *Builder) WithConfig(config *Config) *Builder {
b.config = config
return b
}
func (b *Builder) WithOptions(options ...Option) *Builder {
b.options = append(b.options, options...)
return b
}
func (b *Builder) WithContext(ctx context.Context, cancel context.CancelFunc) *Builder {
b.cancel = cancel
b.ctx = ctx
return b
}
func (b *Builder) WithHandler(handler http.Handler) *Builder {
b.handler = handler
return b
}
func (b *Builder) Build(ctx context.Context) *Server {
if b.server == nil {
b.server = &http.Server{}
b.server.Handler = b.handler
}
b.options.Apply(b.server)
if b.ctx == nil || b.cancel == nil {
b.ctx, b.cancel = context.WithCancel(context.Background())
}
return NewServer(b.server, b.ctx, b.cancel)
}