-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
67 lines (56 loc) · 2 KB
/
handlers.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
package main
import (
"fmt"
"io"
"log"
"net/http"
)
const (
httpHeaderAppName string = "X-App-Name"
httpHeaderAppVersion string = "X-App-Version"
defaultHttpTimeout int = 30 // in seconds
)
type customResponseWriter struct {
writer http.ResponseWriter
status int
lenght int
}
func (crw *customResponseWriter) Header() http.Header {
return crw.writer.Header()
}
func (crw *customResponseWriter) WriteHeader(statusCode int) {
crw.status = statusCode
crw.writer.WriteHeader(statusCode)
}
func (crw *customResponseWriter) Write(b []byte) (int, error) {
if crw.status == 0 {
crw.status = http.StatusOK
}
crw.lenght = len(b)
return crw.writer.Write(b)
}
func httpLog(out io.Writer, nextHandlerFunc http.HandlerFunc) http.HandlerFunc {
return func(responseWriter http.ResponseWriter, request *http.Request) {
crw := &customResponseWriter{writer: responseWriter}
defer func() { // defer is very important here, status and lenght in crw have not been populated yet, you get 0 0 in logs if you remove defer which is expected. Making it defer delays the execuation until the surrounding function completes and by that time these get populated as we are passing &customResponseWriter{writer: responseWriter} i.e. crw to nextHandlerFunc
status := crw.status
length := crw.lenght
log.Println("[INFO]", request.Host, request.RemoteAddr, request.Method, request.URL.Path, request.Proto,
status, length, request.UserAgent())
}()
nextHandlerFunc(crw, request)
}
}
func addAppHeaders(status int, nextHandlerFunc http.HandlerFunc) http.HandlerFunc {
return func(responseWriter http.ResponseWriter, request *http.Request) {
responseWriter.Header().Set(httpHeaderAppName, "http_harbour")
responseWriter.Header().Set(httpHeaderAppVersion, "0.1.2")
responseWriter.WriteHeader(status)
nextHandlerFunc(responseWriter, request)
}
}
func httpDump(dumpString string) http.HandlerFunc {
return func(responseWriter http.ResponseWriter, request *http.Request) {
fmt.Fprintln(responseWriter, dumpString)
}
}