-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.go
76 lines (59 loc) · 1.39 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
66
67
68
69
70
71
72
73
74
75
76
package debug
import (
"net/http"
"github.com/alexfalkowski/go-service/crypto/tls"
"github.com/alexfalkowski/go-service/errors"
sh "github.com/alexfalkowski/go-service/net/http"
"github.com/alexfalkowski/go-service/server"
"github.com/alexfalkowski/go-service/time"
"go.uber.org/fx"
"go.uber.org/zap"
)
// NewServeMux for debug.
func NewServeMux() *http.ServeMux {
return http.NewServeMux()
}
// ServerParams for debug.
type ServerParams struct {
fx.In
Shutdowner fx.Shutdowner
Mux *http.ServeMux
Config *Config
Logger *zap.Logger
}
// Server for debug.
type Server struct {
*server.Server
}
// NewServer for debug.
func NewServer(params ServerParams) (*Server, error) {
s := &http.Server{
Handler: params.Mux,
ReadTimeout: time.Timeout, WriteTimeout: time.Timeout,
IdleTimeout: time.Timeout, ReadHeaderTimeout: time.Timeout,
}
c, err := config(params.Config)
if err != nil {
return nil, err
}
sv, err := sh.NewServer(s, c)
if err != nil {
return nil, errors.Prefix("new debug server", err)
}
svr := server.NewServer("debug", sv, params.Logger, params.Shutdowner)
return &Server{Server: svr}, nil
}
//nolint:nilnil
func config(cfg *Config) (*sh.Config, error) {
if !IsEnabled(cfg) {
return nil, nil
}
c := &sh.Config{}
c.Port = cfg.Port
if !tls.IsEnabled(cfg.TLS) {
return c, nil
}
t, err := tls.NewConfig(cfg.TLS)
c.TLS = t
return c, err
}