initial commit
This commit is contained in:
commit
c9565782b4
5 changed files with 308 additions and 0 deletions
49
log.go
Normal file
49
log.go
Normal file
|
@ -0,0 +1,49 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type responseWriter struct {
|
||||
http.ResponseWriter
|
||||
status int
|
||||
wroteHeader bool
|
||||
}
|
||||
|
||||
func (rw *responseWriter) WriteHeader(code int) {
|
||||
if rw.wroteHeader {
|
||||
return
|
||||
}
|
||||
|
||||
rw.status = code
|
||||
rw.ResponseWriter.WriteHeader(code)
|
||||
rw.wroteHeader = true
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func loghttpreq(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
method := r.Method
|
||||
path := r.URL.EscapedPath()
|
||||
endpoint := strings.Split(r.RemoteAddr, ":")
|
||||
ip := endpoint[0]
|
||||
wrapped := &responseWriter{
|
||||
ResponseWriter: w,
|
||||
}
|
||||
start := time.Now()
|
||||
next.ServeHTTP(wrapped, r)
|
||||
responseTime := time.Since(start)
|
||||
log.Printf(
|
||||
"%-15v %14v %v %-7v %v",
|
||||
ip,
|
||||
responseTime,
|
||||
wrapped.status,
|
||||
method,
|
||||
path,
|
||||
)
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue