MCPcopy Create free account
hub / github.com/ericls/imgdd / LoggerMiddleware

Function LoggerMiddleware

httpserver/common_middleware.go:109–152  ·  view source on GitHub ↗
(next http.Handler)

Source from the content-addressed store, hash-verified

107}
108
109func LoggerMiddleware(next http.Handler) http.Handler {
110 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
111 metrics := httpsnoop.CaptureMetrics(
112 next,
113 w,
114 r,
115 )
116
117 remoteHost := ExtractIP(r)
118
119 uri := r.RequestURI
120
121 if r.ProtoMajor == 2 && r.Method == "CONNECT" {
122 uri = r.Host
123 }
124 if uri == "" {
125 uri = r.URL.RequestURI()
126 }
127
128 host, port, err := net.SplitHostPort(r.Host)
129 if err != nil {
130 host = r.Host
131 if r.TLS != nil {
132 port = "443"
133 } else {
134 port = "80"
135 }
136 }
137
138 entry := &loggingEntry{
139 Proto: r.Proto,
140 Method: r.Method,
141 URL: uri,
142 StatusCode: metrics.Code,
143 Size: metrics.Written,
144 Duration: metrics.Duration,
145 Referer: r.Referer(),
146 RemoteIP: remoteHost,
147 HostName: host,
148 Port: port,
149 }
150 entry.Log()
151 })
152}

Callers 1

MakeServerFunction · 0.85

Calls 2

LogMethod · 0.95
ExtractIPFunction · 0.85

Tested by

no test coverage detected