-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.go
112 lines (90 loc) · 3 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package http
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"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/alexfalkowski/go-service/version"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"go.uber.org/fx"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// ServerParams for HTTP.
type ServerParams struct {
fx.In
Lifecycle fx.Lifecycle
Shutdowner fx.Shutdowner
Config *Config
Logger *zap.Logger
Tracer opentracing.Tracer
Version version.Version
Metrics *prometheus.ServerMetrics
}
// Server for HTTP.
type Server struct {
Mux *runtime.ServeMux
server *http.Server
}
// 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, Version: params.Version, Handler: handler})
handler = meta.NewHandler(params.Version, handler)
addr := fmt.Sprintf(":%s", params.Config.Port)
server := &Server{Mux: mux, server: &http.Server{Addr: addr, Handler: handler}}
params.Lifecycle.Append(fx.Hook{
OnStart: func(context.Context) error {
listener, err := net.Listen("tcp", addr)
if err != nil {
return err
}
go startServer(server.server, listener, params)
return nil
},
OnStop: func(ctx context.Context) error {
return stopServer(ctx, server.server, params)
},
})
return server
}
func startServer(server *http.Server, listener net.Listener, params ServerParams) {
params.Logger.Info("starting http server", zap.String("port", params.Config.Port))
if err := server.Serve(listener); err != nil && !errors.Is(err, http.ErrServerClosed) {
fields := []zapcore.Field{zap.String("port", params.Config.Port), zap.Error(err)}
if err := params.Shutdowner.Shutdown(); err != nil {
fields = append(fields, zap.NamedError("shutdown_error", err))
}
params.Logger.Error("could not start http server", fields...)
}
}
func stopServer(ctx context.Context, server *http.Server, params ServerParams) error {
params.Logger.Info("stopping http server", zap.String("port", params.Config.Port))
if err := server.Shutdown(ctx); err != nil {
params.Logger.Error("could not stop http server", zap.String("port", params.Config.Port), zap.Error(err))
return err
}
return nil
}
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)
}
}