-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
65 lines (53 loc) · 1.66 KB
/
server.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
62
63
64
65
package scenarios
import "net/http"
// CreateServerFunc creates a http.Server instance.
type CreateServerFunc func(addr string, handler http.Handler) (*http.Server, error)
// CreateServerWith creates a CreateServerFunc with the given ServerConfig.
func CreateServerWith(
configures ...ServerConfig,
) CreateServerFunc {
apply := ApplyServerWith(configures...)
return func(addr string, handler http.Handler) (*http.Server, error) {
server := new(http.Server)
var err error
server, err = apply(server)
if err != nil {
return nil, err
}
server.Addr = addr
server.Handler = handler
return server, nil
}
}
// ApplyServerFunc applies configurations to a http.Server instance.
type ApplyServerFunc func(*http.Server) (*http.Server, error)
// ApplyServerWith creates an ApplyServerFunc with the given ServerConfig.
func ApplyServerWith(
configures ...ServerConfig,
) ApplyServerFunc {
return func(s *http.Server) (*http.Server, error) {
for _, configure := range configures {
var err error
s, err = configure.configureServer(s)
if err != nil {
return nil, err
}
}
return s, nil
}
}
// ListenAndServeFunc listens on the TCP network address addr and then calls Serve to handle requests on incoming connections.
type ListenAndServeFunc func(addr string, handler http.Handler) error
// ListenAndServeFunc creates a ListenAndServeFunc with the given ServerConfig.
func ListenAndServeFuncWith(
configures ...ServerConfig,
) ListenAndServeFunc {
create := CreateServerWith(configures...)
return func(addr string, handler http.Handler) error {
server, err := create(addr, handler)
if err != nil {
return err
}
return server.ListenAndServe()
}
}