-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmiddleware.go
151 lines (125 loc) · 3.75 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
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
package httplog
import (
"fmt"
"io"
"net/http"
"os"
"regexp"
"time"
"github.com/mattn/go-isatty"
)
// Copyright 2022 Jack Rudenko. MadAppGang. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
// LoggerWithName instance a Logger middleware with the specified name prefix.
func LoggerWithName(routerName string) func(next http.Handler) http.Handler {
return LoggerWithConfig(LoggerConfig{
ProxyHandler: NewProxy(),
RouterName: routerName,
})
}
// LoggerWithFormatter instance a Logger middleware with the specified log format function.
func LoggerWithFormatter(f LogFormatter) func(next http.Handler) http.Handler {
return LoggerWithConfig(LoggerConfig{
Formatter: f,
ProxyHandler: NewProxy(),
})
}
// LoggerWithFormatterAndName instance a Logger middleware with the specified log format function.
func LoggerWithFormatterAndName(routerName string, f LogFormatter) func(next http.Handler) http.Handler {
return LoggerWithConfig(LoggerConfig{
Formatter: f,
ProxyHandler: NewProxy(),
RouterName: routerName,
})
}
// LoggerWithWriter instance a Logger middleware with the specified writer buffer.
// Example: os.Stdout, a file opened in write mode, a socket...
func LoggerWithWriter(out io.Writer, notlogged ...string) func(next http.Handler) http.Handler {
return LoggerWithConfig(LoggerConfig{
Output: out,
SkipPaths: notlogged,
ProxyHandler: NewProxy(),
})
}
// LoggerWithConfig instance a Logger middleware with config.
func LoggerWithConfig(conf LoggerConfig) func(next http.Handler) http.Handler {
formatter := conf.Formatter
if formatter == nil {
formatter = DefaultLogFormatter
}
if conf.ProxyHandler == nil {
conf.ProxyHandler = NewProxy()
}
out := conf.Output
if out == nil {
out = DefaultWriter
}
isTerm := true
if w, ok := out.(*os.File); !ok || os.Getenv("TERM") == "dumb" ||
(!isatty.IsTerminal(w.Fd()) && !isatty.IsCygwinTerminal(w.Fd())) {
isTerm = false
}
var skipPath []*regexp.Regexp
for _, p := range conf.SkipPaths {
re, err := regexp.Compile(p)
if err == nil {
skipPath = append(skipPath, re)
} else {
fmt.Fprint(out, fmt.Sprintf("error parsing skip path regex, ignoring: %s", p))
}
}
var hideHeaderKeys []*regexp.Regexp
for _, p := range conf.HideHeaderKeys {
re, err := regexp.Compile(p)
if err == nil {
hideHeaderKeys = append(hideHeaderKeys, re)
} else {
fmt.Fprint(out, fmt.Sprintf("error parsing header key regexp to hide, ignoring: %s", p))
}
}
middleware := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Start timer
start := time.Now()
path := r.URL.Path
raw := r.URL.RawQuery
// Process request
// Wrap response writer with Recorded response writer
wr := NewWriter(w, conf.CaptureBody)
next.ServeHTTP(wr, r)
var skip bool
// check path for skip regexp set
for _, r := range skipPath {
if r.MatchString(path) {
skip = true
break
}
}
// Log only when path is not being skipped
if skip == false {
r.Header = maskHeaderKeys(r.Header.Clone(), hideHeaderKeys)
param := LogFormatterParams{
Request: r,
isTerm: isTerm,
}
// Stop timer
param.TimeStamp = time.Now()
param.Latency = param.TimeStamp.Sub(start)
param.ClientIP = conf.ProxyHandler.ClientIP(r)
param.Method = r.Method
param.StatusCode = wr.Status()
param.BodySize = wr.Size()
param.Body = wr.Body()
param.ResponseHeader = maskHeaderKeys(wr.Header().Clone(), hideHeaderKeys)
param.RouterName = conf.RouterName
if raw != "" {
path = path + "?" + raw
}
param.Path = path
fmt.Fprint(out, formatter(param))
}
})
}
return middleware
}