-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
49 lines (41 loc) · 1.19 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
package web
import (
"log"
"net/http"
"strings"
"github.com/gorilla/handlers"
)
type loggingResponseWriter struct {
http.ResponseWriter
statusCode int
}
func (lrw *loggingResponseWriter) WriteHeader(code int) {
lrw.statusCode = code
lrw.ResponseWriter.WriteHeader(code)
}
func (serv *Server) loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
lrw := &loggingResponseWriter{w, http.StatusOK}
next.ServeHTTP(lrw, r)
ip := strings.Split(r.RemoteAddr, ":")[0]
method := r.Method
uri := r.RequestURI
proto := r.Proto
log.Printf("%s \"%s %s %s\" %d\n", ip, method, uri, proto, lrw.statusCode)
})
}
func (serv *Server) rateLimitingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
serv.rl.Take()
next.ServeHTTP(w, r)
})
}
func serverHeaderMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Powered-By", "https://github.com/CHTJonas/go-lg")
next.ServeHTTP(w, r)
})
}
func proxyMiddleware(next http.Handler) http.Handler {
return handlers.ProxyHeaders(next)
}