-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.go
129 lines (107 loc) · 3.06 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package http
import (
"net/http"
st "github.com/alexfalkowski/go-service/crypto/tls"
"github.com/alexfalkowski/go-service/errors"
"github.com/alexfalkowski/go-service/limiter"
sh "github.com/alexfalkowski/go-service/net/http"
"github.com/alexfalkowski/go-service/server"
"github.com/alexfalkowski/go-service/time"
"github.com/alexfalkowski/go-service/transport/http/cors"
hl "github.com/alexfalkowski/go-service/transport/http/limiter"
"github.com/alexfalkowski/go-service/transport/http/meta"
logger "github.com/alexfalkowski/go-service/transport/http/telemetry/logger/zap"
"github.com/alexfalkowski/go-service/transport/http/telemetry/metrics"
"github.com/alexfalkowski/go-service/transport/http/telemetry/tracer"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
v3 "github.com/ulule/limiter/v3"
"github.com/urfave/negroni/v3"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
"go.uber.org/fx"
"go.uber.org/zap"
"google.golang.org/protobuf/encoding/protojson"
)
// NewServeMux for HTTP.
func NewServeMux() *runtime.ServeMux {
opts := []runtime.ServeMuxOption{
runtime.WithIncomingHeaderMatcher(customMatcher),
runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{
MarshalOptions: protojson.MarshalOptions{
UseProtoNames: true,
},
UnmarshalOptions: protojson.UnmarshalOptions{
DiscardUnknown: true,
},
}),
}
return runtime.NewServeMux(opts...)
}
// ServerParams for HTTP.
type ServerParams struct {
fx.In
Shutdowner fx.Shutdowner
Mux *runtime.ServeMux
Config *Config
Logger *zap.Logger
Tracer trace.Tracer
Meter metric.Meter
Limiter *v3.Limiter
Key limiter.KeyFunc
Handlers []negroni.Handler `optional:"true"`
}
// Server for HTTP.
type Server struct {
*server.Server
}
// NewServer for HTTP.
func NewServer(params ServerParams) (*Server, error) {
n := negroni.New()
n.Use(meta.NewHandler(UserAgent(params.Config)))
n.Use(tracer.NewHandler(params.Tracer))
n.Use(logger.NewHandler(params.Logger))
n.Use(metrics.NewHandler(params.Meter))
for _, hd := range params.Handlers {
n.Use(hd)
}
n.Use(cors.New())
n.Use(hl.NewHandler(params.Limiter, params.Key))
n.UseHandler(params.Mux)
s := &http.Server{
Handler: n,
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 http server", err)
}
svr := server.NewServer("http", 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 !st.IsEnabled(cfg.TLS) {
return c, nil
}
tls, err := st.NewConfig(cfg.TLS)
c.TLS = tls
return c, err
}
func customMatcher(key string) (string, bool) {
switch key {
case "Request-Id", "Geolocation", "X-Forwarded-For":
return key, true
default:
return runtime.DefaultHeaderMatcher(key)
}
}