-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.go
86 lines (68 loc) · 1.57 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
77
78
79
80
81
82
83
84
85
86
package debug
import (
"net/http"
"time"
"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"
t "github.com/alexfalkowski/go-service/time"
"go.uber.org/fx"
"go.uber.org/zap"
)
// ServerParams for debug.
type ServerParams struct {
fx.In
Shutdowner fx.Shutdowner
Config *Config
Logger *zap.Logger
}
// Server for debug.
type Server struct {
mux *http.ServeMux
*server.Server
}
// NewServer for debug.
func NewServer(params ServerParams) (*Server, error) {
mux := http.NewServeMux()
timeout := timeout(params.Config)
s := &http.Server{
Handler: mux,
ReadTimeout: timeout, WriteTimeout: timeout,
IdleTimeout: timeout, ReadHeaderTimeout: 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, mux: mux}, nil
}
// ServeMux for debug.
func (s *Server) ServeMux() *http.ServeMux {
return s.mux
}
//nolint:nilnil
func config(cfg *Config) (*sh.Config, error) {
if !IsEnabled(cfg) {
return nil, nil
}
c := &sh.Config{}
c.Port = cfg.GetPort("6060")
if !tls.IsEnabled(cfg.TLS) {
return c, nil
}
t, err := tls.NewConfig(cfg.TLS)
c.TLS = t
return c, err
}
func timeout(cfg *Config) time.Duration {
if !IsEnabled(cfg) {
return time.Minute
}
return t.MustParseDuration(cfg.Timeout)
}