mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-05-01 20:53:05 -04:00
* add example generator infrastructure, with some examples for pkg/jmap and pkg/groupware, with more needing to be done * alter the apidoc Makefile to stop using go-swagger but, instead, use the openapi.yml file that must be dropped into that directory using groupware-apidocs (will improve the integration there later) * add Makefile target to generate examples * bump redocly from 2.4.0 to 2.14.5 * introduce Request.PathParam() and .PathParamDoc() to improve API documentation, as well as future-proofing * improve X-Request-ID and Trace-Id header handling in the middleware by logging it safely when an error occurs in the middleware
61 lines
1.2 KiB
Go
61 lines
1.2 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
|
"github.com/opencloud-eu/opencloud/pkg/log"
|
|
)
|
|
|
|
var (
|
|
LogTraceID = "traceId"
|
|
)
|
|
|
|
func GroupwareLogger(logger log.Logger) func(http.Handler) http.Handler {
|
|
return func(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
start := time.Now()
|
|
wrap := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
|
|
next.ServeHTTP(wrap, r)
|
|
|
|
level := logger.Debug()
|
|
err := recover()
|
|
if err != nil {
|
|
level = logger.Error()
|
|
}
|
|
|
|
if !level.Enabled() {
|
|
return
|
|
}
|
|
|
|
if err != nil {
|
|
switch e := err.(type) {
|
|
case error:
|
|
level = level.Err(e)
|
|
default:
|
|
level = level.Any("panic", e)
|
|
}
|
|
}
|
|
|
|
ctx := r.Context()
|
|
|
|
requestID := middleware.GetReqID(ctx)
|
|
level = level.Str(log.RequestIDString, log.SafeString(requestID))
|
|
traceID := GetTraceID(ctx)
|
|
if traceID != "" {
|
|
level = level.Str(LogTraceID, log.SafeString(traceID))
|
|
}
|
|
|
|
level.
|
|
Str("proto", r.Proto).
|
|
Str("method", r.Method).
|
|
Int("status", wrap.Status()).
|
|
Str("path", r.URL.Path).
|
|
Dur("duration", time.Since(start)).
|
|
Int("bytes", wrap.BytesWritten()).
|
|
Msg("")
|
|
})
|
|
}
|
|
}
|