-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
107 lines (85 loc) · 3.03 KB
/
middleware.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
package api
import (
"context"
"fmt"
"net/http"
"strings"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/gofrs/uuid"
"github.com/sirupsen/logrus"
"github.com/tomasen/realip"
logging "github.com/Decentr-net/logrus/context"
)
// RecovererMiddleware handles panics.
func RecovererMiddleware(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
defer func() {
if rvr := recover(); rvr != nil {
logging.GetLogger(r.Context()).Info("service recovered from panic")
WriteInternalError(r.Context(), w, spew.Sdump(rvr))
}
}()
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
// LoggerMiddleware puts logger with client's info into context.
func LoggerMiddleware(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
log := logrus.WithField("ip", realip.FromRequest(r))
next.ServeHTTP(w, r.WithContext(logging.WithLogger(r.Context(), log)))
}
return http.HandlerFunc(fn)
}
// RequestIDMiddleware puts request-id to headers and adds it into a logger.
func RequestIDMiddleware(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
id := uuid.Must(uuid.NewV4()).String()
w.Header().Set("X-Request-ID", id)
l := logging.GetLogger(r.Context()).WithField("request_id", id)
next.ServeHTTP(w, r.WithContext(logging.WithLogger(r.Context(), l)))
}
return http.HandlerFunc(fn)
}
// TimeoutMiddleware puts timeout context into request.
func TimeoutMiddleware(timeout time.Duration) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
logging.GetLogger(r.Context()).WithField("url", r.URL.String()).Debug("start processing")
ctx, _ := context.WithTimeout(r.Context(), timeout)
next.ServeHTTP(w, r.WithContext(ctx))
logging.GetLogger(r.Context()).WithField("elapsed_time", time.Since(start)).Debug("processed")
}
return http.HandlerFunc(fn)
}
}
// FileServerMiddleware serves requests with prefix into directory.
func FileServerMiddleware(prefix, dir string) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Shortcut helpers for swagger-ui
if r.URL.Path == prefix {
http.Redirect(w, r, fmt.Sprintf("%s/", prefix), http.StatusFound)
return
}
// Serving ./swagger-ui/
if strings.Index(r.URL.Path, fmt.Sprintf("%s/", prefix)) == 0 {
http.StripPrefix(fmt.Sprintf("%s/", prefix), http.FileServer(http.Dir(dir))).ServeHTTP(w, r)
return
}
next.ServeHTTP(w, r)
})
}
}
// BodyLimiterMiddleware returns middleware which limits size of data read from request's body.
func BodyLimiterMiddleware(maxBodySize int64) func(handler http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, maxBodySize)
next.ServeHTTP(w, r)
})
return fn
}
}