2020-10-30 10:12:33 +13:00
|
|
|
package http
|
2020-10-23 03:54:50 +13:00
|
|
|
|
|
|
|
import (
|
2021-09-17 10:24:33 +12:00
|
|
|
"context"
|
2020-10-23 03:54:50 +13:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi/middleware"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2021-09-17 10:24:33 +12:00
|
|
|
"demodesk/neko/internal/http/auth"
|
|
|
|
"demodesk/neko/internal/types"
|
|
|
|
"demodesk/neko/internal/utils"
|
|
|
|
)
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2021-09-17 10:24:33 +12:00
|
|
|
type logEntryKey int
|
2021-03-20 03:04:01 +13:00
|
|
|
|
2021-09-17 10:24:33 +12:00
|
|
|
const logEntryKeyCtx logEntryKey = iota
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2021-09-17 10:24:33 +12:00
|
|
|
func setLogEntry(r *http.Request, data logEntry) *http.Request {
|
|
|
|
ctx := context.WithValue(r.Context(), logEntryKeyCtx, data)
|
|
|
|
return r.WithContext(ctx)
|
|
|
|
}
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2021-09-17 10:24:33 +12:00
|
|
|
func getLogEntry(r *http.Request) logEntry {
|
|
|
|
return r.Context().Value(logEntryKeyCtx).(logEntry)
|
|
|
|
}
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2021-09-17 10:24:33 +12:00
|
|
|
func LoggerMiddleware(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
next.ServeHTTP(w, setLogEntry(r, newLogEntry(w, r)))
|
|
|
|
})
|
|
|
|
}
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2021-09-17 10:24:33 +12:00
|
|
|
type logEntry struct {
|
|
|
|
req struct {
|
|
|
|
time time.Time
|
|
|
|
id string
|
|
|
|
scheme string
|
|
|
|
proto string
|
|
|
|
method string
|
|
|
|
remote string
|
|
|
|
agent string
|
|
|
|
uri string
|
|
|
|
}
|
|
|
|
res struct {
|
|
|
|
time time.Time
|
|
|
|
code int
|
|
|
|
bytes int
|
|
|
|
}
|
|
|
|
err error
|
|
|
|
elapsed time.Duration
|
|
|
|
hasSession bool
|
|
|
|
session types.Session
|
|
|
|
}
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2021-09-17 10:24:33 +12:00
|
|
|
func newLogEntry(w http.ResponseWriter, r *http.Request) logEntry {
|
|
|
|
e := logEntry{}
|
|
|
|
e.req.time = time.Now()
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2021-09-17 10:24:33 +12:00
|
|
|
if reqID := middleware.GetReqID(r.Context()); reqID != "" {
|
|
|
|
e.req.id = reqID
|
|
|
|
}
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2021-09-17 10:24:33 +12:00
|
|
|
scheme := "http"
|
|
|
|
if r.TLS != nil {
|
|
|
|
scheme = "https"
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
2021-09-17 10:24:33 +12:00
|
|
|
|
|
|
|
e.req.scheme = scheme
|
|
|
|
e.req.proto = r.Proto
|
|
|
|
e.req.method = r.Method
|
|
|
|
e.req.remote = r.RemoteAddr
|
|
|
|
e.req.agent = r.UserAgent()
|
|
|
|
e.req.uri = fmt.Sprintf("%s://%s%s", scheme, r.Host, r.RequestURI)
|
|
|
|
return e
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2021-09-17 10:24:33 +12:00
|
|
|
func (e *logEntry) SetResponse(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
|
|
|
|
e.res.time = time.Now()
|
|
|
|
e.res.code = ww.Status()
|
|
|
|
e.res.bytes = ww.BytesWritten()
|
|
|
|
|
|
|
|
e.elapsed = e.res.time.Sub(e.req.time)
|
|
|
|
e.session, e.hasSession = auth.GetSession(r)
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2021-09-17 10:24:33 +12:00
|
|
|
func (e *logEntry) SetError(err error) {
|
|
|
|
e.err = err
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2021-09-17 10:24:33 +12:00
|
|
|
func (e *logEntry) Write() {
|
|
|
|
logger := log.With().
|
|
|
|
Str("module", "http").
|
|
|
|
Float64("elapsed", float64(e.elapsed.Nanoseconds())/1000000.0).
|
|
|
|
Interface("req", e.req).
|
|
|
|
Interface("res", e.res).
|
|
|
|
Logger()
|
|
|
|
|
|
|
|
if e.hasSession {
|
|
|
|
logger = logger.With().Str("session_id", e.session.ID()).Logger()
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.err != nil {
|
|
|
|
httpErr, ok := e.err.(*utils.HTTPError)
|
|
|
|
if !ok {
|
|
|
|
logger.Err(e.err).Msgf("request failed (%d)", e.res.code)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if httpErr.Message == "" {
|
|
|
|
httpErr.Message = http.StatusText(httpErr.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger := logger.Error().Err(httpErr.InternalErr)
|
|
|
|
|
|
|
|
message := httpErr.Message
|
|
|
|
if httpErr.InternalMsg != "" {
|
|
|
|
message = httpErr.InternalMsg
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Msgf("request failed (%d): %s", e.res.code, message)
|
|
|
|
return
|
|
|
|
}
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2021-09-17 10:24:33 +12:00
|
|
|
logger.Debug().Msgf("request complete (%d)", e.res.code)
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|