-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.go
96 lines (79 loc) · 2.58 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
87
88
89
90
91
92
93
94
95
96
package http
import (
"context"
"errors"
"net"
"net/http"
"github.com/alexfalkowski/go-service/time"
"github.com/alexfalkowski/go-service/transport/http/cors"
szap "github.com/alexfalkowski/go-service/transport/http/logger/zap"
"github.com/alexfalkowski/go-service/transport/http/meta"
"github.com/alexfalkowski/go-service/transport/http/metrics/prometheus"
"github.com/alexfalkowski/go-service/transport/http/trace/opentracing"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/soheilhy/cmux"
"go.uber.org/fx"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// ServerParams for HTTP.
type ServerParams struct {
fx.In
Shutdowner fx.Shutdowner
Config *Config
Logger *zap.Logger
Tracer opentracing.Tracer
Metrics *prometheus.ServerMetrics
}
// Server for HTTP.
type Server struct {
Mux *runtime.ServeMux
server *http.Server
params ServerParams
}
// NewServer for HTTP.
func NewServer(params ServerParams) *Server {
mux := runtime.NewServeMux(runtime.WithIncomingHeaderMatcher(customMatcher))
var handler http.Handler = mux
handler = cors.New().Handler(handler)
handler = params.Metrics.Handler(handler)
handler = opentracing.NewHandler(params.Tracer, handler)
handler = szap.NewHandler(szap.HandlerParams{Logger: params.Logger, Handler: handler})
handler = meta.NewHandler(handler)
server := &Server{
Mux: mux,
server: &http.Server{Handler: handler, ReadHeaderTimeout: time.Timeout},
params: params,
}
return server
}
// Start the server.
func (s *Server) Start(listener net.Listener) {
s.params.Logger.Info("starting http server", zap.String("addr", listener.Addr().String()))
if err := s.server.Serve(listener); err != nil && !s.ignoreError(err) {
fields := []zapcore.Field{zap.String("addr", listener.Addr().String()), zap.Error(err)}
if err := s.params.Shutdowner.Shutdown(); err != nil {
fields = append(fields, zap.NamedError("shutdown_error", err))
}
s.params.Logger.Error("could not start http server", fields...)
}
}
// Stop the server.
func (s *Server) Stop(ctx context.Context) {
s.params.Logger.Info("stopping http server", zap.Error(s.server.Shutdown(ctx)))
}
func (s *Server) ignoreError(err error) bool {
return errors.Is(err, http.ErrServerClosed) || errors.Is(err, cmux.ErrListenerClosed) || errors.Is(err, cmux.ErrServerClosed)
}
func customMatcher(key string) (string, bool) {
switch key {
case "Request-Id", "Authorization", "Geolocation":
return key, true
case "User-Agent":
return "ua", true
case "X-Forwarded-For":
return "forwarded-for", true
default:
return runtime.DefaultHeaderMatcher(key)
}
}