-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
58 lines (50 loc) · 1.36 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
package server
import (
"fmt"
"log"
"net/http"
"net/url"
"os"
"runtime/debug"
"github.com/gorilla/handlers"
)
func DefaultLogMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
scheme := r.URL.Scheme
if len(scheme) == 0 {
scheme = "http"
}
u := &url.URL{
Scheme: scheme,
Host: r.Host,
Path: r.URL.Path,
}
httpAction := fmt.Sprintf("\"%s %s %s\"", r.Method, u.String(), r.Proto)
log.Println(r.RemoteAddr, httpAction)
next.ServeHTTP(w, r)
})
}
func ApacheLogMiddleware(next http.Handler) http.Handler {
return handlers.LoggingHandler(os.Stdout, next)
}
func ProxyMiddleware(next http.Handler) http.Handler {
return handlers.ProxyHeaders(next)
}
func ServerHeaderMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Server", "https://github.com/CHTJonas/httkey-server")
next.ServeHTTP(w, r)
})
}
func recoveryMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
http.Error(w, "500 Internal Server Error", http.StatusInternalServerError)
log.Println("An internal server error occurred:", err)
log.Println(string(debug.Stack()))
}
}()
next.ServeHTTP(w, r)
})
}