-
Notifications
You must be signed in to change notification settings - Fork 17
/
echo.go
304 lines (243 loc) · 7.61 KB
/
echo.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Copyright 2023 The Infratographer Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package echox
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/labstack/echo-contrib/prometheus"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho"
"go.uber.org/zap"
"go.infratographer.com/x/echox/echozap"
"go.infratographer.com/x/versionx"
)
const (
ipv4SingleHostCIDR = 32
ipv4BitLength = 8 * net.IPv4len
ipv6SingleHostCIDR = 128
ipv6BitLength = 8 * net.IPv6len
)
var (
// ErrInvalidTrustedProxyIP is returned when an invalid ip is provided as a trusted proxy.
ErrInvalidTrustedProxyIP = errors.New("invalid trusted proxy ip")
)
// CheckFunc is a function that can be used to check the status of a service.
type CheckFunc func(ctx context.Context) error
// Server implements the HTTP Server
type Server struct {
debug bool
listen string
logger *zap.Logger
handlers []handler
middleware []echo.MiddlewareFunc
echozapOpts []echozap.MiddlewareOption
readinessChecks map[string]CheckFunc
shutdownTimeout time.Duration
trustedProxies []*net.IPNet
version *versionx.Details
}
// NewServer will return an opinionated echo server for processing API requests.
func NewServer(logger *zap.Logger, cfg Config, version *versionx.Details, options ...Option) (*Server, error) {
cfg = cfg.withDefaults()
trustedProxies, err := parseIPNets(cfg.TrustedProxies)
if err != nil {
return nil, err
}
s := &Server{
debug: cfg.Debug,
listen: cfg.Listen,
logger: logger.Named("echox"),
middleware: cfg.Middleware,
readinessChecks: map[string]CheckFunc{},
shutdownTimeout: cfg.ShutdownGracePeriod,
trustedProxies: trustedProxies,
version: version,
}
for _, opt := range options {
opt(s)
}
return s, nil
}
func parseIPNets(sNets []string) ([]*net.IPNet, error) {
var nets []*net.IPNet
for _, entry := range sNets {
var (
ipnet *net.IPNet
err error
)
if strings.Contains(entry, "/") {
_, ipnet, err = net.ParseCIDR(entry)
if err != nil {
return nil, err
}
} else {
ip := net.ParseIP(entry)
if ip == nil {
return nil, ErrInvalidTrustedProxyIP
}
if ipv4 := ip.To4(); ipv4 != nil {
ipnet = &net.IPNet{
IP: ipv4,
Mask: net.CIDRMask(ipv4SingleHostCIDR, ipv4BitLength),
}
} else {
ipnet = &net.IPNet{
IP: ip,
Mask: net.CIDRMask(ipv6SingleHostCIDR, ipv6BitLength),
}
}
}
nets = append(nets, ipnet)
}
return nets, nil
}
type handler interface {
Routes(*echo.Group)
}
// AddHandler provides the ability to add additional HTTP handlers that process
// requests. The handler that is provided should have a Routes(*echo.Group)
// function, which allows the routes to be added to the server.
func (s *Server) AddHandler(h handler) *Server {
s.handlers = append(s.handlers, h)
return s
}
// AddReadinessCheck will accept a function to be ran during calls to /readyx.
// These functions should accept a context and only return an error. When adding
// a readiness check a name is also provided, this name will be used when returning
// the state of all the checks
func (s *Server) AddReadinessCheck(name string, f CheckFunc) *Server {
s.readinessChecks[name] = f
return s
}
// Handler returns a new http.Handler for serving requests.
func (s *Server) Handler() http.Handler {
hostname, err := os.Hostname()
if err != nil {
hostname = "unknown"
}
engine := echo.New()
engine.Use(middleware.RequestID())
engine.Use(echozap.Middleware(s.logger, s.echozapOpts...))
engine.Use(middleware.Recover())
engine.Use(otelecho.Middleware(hostname, otelecho.WithSkipper(SkipDefaultEndpoints)))
engine.Use(s.middleware...)
engine.HideBanner = true
engine.HidePort = true
engine.Debug = s.debug
if s.trustedProxies != nil {
ranges := make([]echo.TrustOption, len(s.trustedProxies))
for i, trust := range s.trustedProxies {
ranges[i] = echo.TrustIPRange(trust)
}
engine.IPExtractor = echo.ExtractIPFromXFFHeader(ranges...)
} else {
engine.IPExtractor = echo.ExtractIPDirect()
}
p := prometheus.NewPrometheus("echo", nil)
p.Use(engine)
if s.version != nil {
// Version endpoint returns build information
engine.GET("/version", s.versionHandler)
}
// Health endpoints
engine.GET("/livez", s.livenessCheckHandler)
engine.GET("/readyz", s.readinessCheckHandler)
for _, handler := range s.handlers {
handler.Routes(engine.Group(""))
}
return engine
}
// Serve serves an http server on the provided listener.
// See ServeWithContext for more details.
func (s *Server) Serve(listener net.Listener) error {
return s.ServeWithContext(context.Background(), listener)
}
// ServeWithContext serves an http server on the provided listener.
// Serve blocks until SIGINT or SIGTERM are signalled,
// or if the http serve fails.
// A graceful shutdown will be attempted
func (s *Server) ServeWithContext(ctx context.Context, listener net.Listener) error {
logger := s.logger.With(zap.String("address", listener.Addr().String()))
logger.Info("starting server")
srv := &http.Server{
Handler: s.Handler(),
}
var (
exit = make(chan error, 1)
quit = make(chan os.Signal, 2) //nolint:gomnd
)
// Serve in a go routine.
// If serve returns an error, capture the error to return later.
go func() {
if err := srv.Serve(listener); err != nil {
exit <- err
return
}
exit <- nil
}()
// close server to kill active connections.
defer srv.Close() //nolint:errcheck // server is being closed, we'll ignore this.
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
var err error
select {
case err = <-exit:
return err
case sig := <-quit:
logger.Warn(fmt.Sprintf("%s received, server shutting down", sig.String()))
case <-ctx.Done():
logger.Warn("context done, server shutting down")
// Since the context has already been canceled, the server would immediately shutdown.
// We'll reset the context to allow for the proper grace period to be given.
ctx = context.Background()
}
ctx, cancel := context.WithTimeout(ctx, s.shutdownTimeout)
defer cancel()
if err = srv.Shutdown(ctx); err != nil {
logger.Error("server shutdown timed out", zap.Error(err))
return err
}
return nil
}
// Run listens and serves the echo server on the configured address.
func (s *Server) Run() error {
return s.RunWithContext(context.Background())
}
// RunWithContext listens and serves the echo server on the configured address.
// See ServeWithContext for more details.
func (s *Server) RunWithContext(ctx context.Context) error {
listener, err := net.Listen("tcp", s.listen)
if err != nil {
return err
}
defer listener.Close() //nolint:errcheck // No need to check error.
return s.ServeWithContext(ctx, listener)
}
// SkipDefaultEndpoints returns true when the provided context request is for /version /livez /readyz or /metrics
func SkipDefaultEndpoints(c echo.Context) bool {
switch c.Request().URL.Path {
case "/version", "/livez", "/readyz", "/metrics":
return true
default:
return false
}
}