/
http.go
157 lines (136 loc) · 4.57 KB
/
http.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package kitty
import (
"context"
"fmt"
"net/http"
"net/http/pprof"
"github.com/go-kit/kit/endpoint"
kithttp "github.com/go-kit/kit/transport/http"
)
// HTTPTransport defines a HTTP transport for a kitty Server.
type HTTPTransport struct {
cfg Config
opts []kithttp.ServerOption
endpoints []*httpendpoint
httpmiddleware func(http.Handler) http.Handler
mux Router
svr *http.Server
liveness http.HandlerFunc
readiness http.HandlerFunc
}
var _ Transport = &HTTPTransport{}
// nopHTTPMiddleWare is the default HTTP middleware, and does nothing.
func nopHTTPMiddleWare(h http.Handler) http.Handler {
return h
}
// HTTPMiddlewares defines the list of HTTP middlewares to be added to all HTTP handlers.
func (t *HTTPTransport) HTTPMiddlewares(m ...func(http.Handler) http.Handler) *HTTPTransport {
t.httpmiddleware = func(next http.Handler) http.Handler {
for i := len(m) - 1; i >= 0; i-- {
next = m[i](next)
}
return next
}
return t
}
// NewHTTPTransport creates a new HTTP transport, based on the specified config.
func NewHTTPTransport(cfg Config) *HTTPTransport {
t := &HTTPTransport{
cfg: DefaultConfig,
httpmiddleware: nopHTTPMiddleWare,
mux: StdlibRouter(),
liveness: defaultHealthcheck,
readiness: defaultHealthcheck,
}
if cfg.HTTPPort > 0 {
t.cfg.HTTPPort = cfg.HTTPPort
}
if cfg.LivenessCheckPath != "" {
t.cfg.LivenessCheckPath = cfg.LivenessCheckPath
}
if cfg.ReadinessCheckPath != "" {
t.cfg.ReadinessCheckPath = cfg.ReadinessCheckPath
}
t.cfg.EnablePProf = cfg.EnablePProf
if cfg.EncodeResponse != nil {
t.cfg.EncodeResponse = cfg.EncodeResponse
}
return t
}
// ServeHTTP implements http.Handler.
func (t *HTTPTransport) ServeHTTP(w http.ResponseWriter, r *http.Request) {
t.httpmiddleware(t.mux).ServeHTTP(w, r)
}
// RegisterEndpoints registers all configured endpoints, wraps them with the m middleware.
func (t *HTTPTransport) RegisterEndpoints(m endpoint.Middleware) error {
opts := []kithttp.ServerOption{
kithttp.ServerBefore(kithttp.PopulateRequestContext),
}
opts = append(opts, t.opts...)
// register endpoints
for _, ep := range t.endpoints {
encoder := t.cfg.EncodeResponse
if ep.encoder != nil {
encoder = ep.encoder
}
t.mux.Handle(ep.method, ep.path,
kithttp.NewServer(
m(ep.endpoint),
ep.decoder,
encoder,
append(opts, ep.options...)...))
}
// register health handlers
t.mux.Handle("GET", t.cfg.LivenessCheckPath, t.liveness)
t.mux.Handle("GET", t.cfg.ReadinessCheckPath, t.readiness)
// register pprof handlers
registerPProf(t.cfg, t.mux)
return nil
}
var httpLogkeys = map[string]interface{}{
"http-method": kithttp.ContextKeyRequestMethod,
"http-uri": kithttp.ContextKeyRequestURI,
"http-path": kithttp.ContextKeyRequestPath,
"http-proto": kithttp.ContextKeyRequestProto,
"http-requesthost": kithttp.ContextKeyRequestHost,
"http-remote-addr": kithttp.ContextKeyRequestRemoteAddr,
"http-x-forwarded-for": kithttp.ContextKeyRequestXForwardedFor,
"http-x-forwarded-proto": kithttp.ContextKeyRequestXForwardedProto,
"http-user-agent": kithttp.ContextKeyRequestUserAgent,
"http-x-request-id": kithttp.ContextKeyRequestXRequestID,
}
// LogKeys returns the list of name key to context key mappings
func (t *HTTPTransport) LogKeys() map[string]interface{} {
return httpLogkeys
}
// Start starts the HTTP server.
func (t *HTTPTransport) Start(ctx context.Context) error {
t.svr = &http.Server{
Handler: t,
Addr: fmt.Sprintf(":%d", t.cfg.HTTPPort),
}
_ = LogMessage(ctx, fmt.Sprintf("Listening on port: %d", t.cfg.HTTPPort))
err := t.svr.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
return err
}
return nil
}
// Shutdown shutdowns the HTTP server
func (t *HTTPTransport) Shutdown(ctx context.Context) error {
return t.svr.Shutdown(ctx)
}
func registerPProf(cfg Config, mux Router) {
if !cfg.EnablePProf {
return
}
mux.Handle("GET", "/debug/pprof/", http.HandlerFunc(pprof.Index))
mux.Handle("GET", "/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
mux.Handle("GET", "/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
mux.Handle("GET", "/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
mux.Handle("GET", "/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
mux.Handle("GET", "/debug/pprof/goroutine", pprof.Handler("goroutine"))
mux.Handle("GET", "/debug/pprof/heap", pprof.Handler("heap"))
mux.Handle("GET", "/debug/pprof/threadcreate", pprof.Handler("threadcreate"))
mux.Handle("GET", "/debug/pprof/block", pprof.Handler("block"))
}