commit e2509238d2b7566492e3f0a6b71563277487c98e Author: Alexander NeonXP Kiryukhin Date: Sat Apr 6 20:05:14 2024 +0300 initial diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..612a4b7 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module gitrepo.ru/neonxp/middleware + +go 1.22.1 + +require ( + github.com/google/uuid v1.6.0 + golang.org/x/exp v0.0.0-20240404231335-c0f41cb1a7a0 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..1e212a5 --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +golang.org/x/exp v0.0.0-20240404231335-c0f41cb1a7a0 h1:985EYyeCOxTpcgOTJpflJUwOeEz0CQOdPt73OzpE9F8= +golang.org/x/exp v0.0.0-20240404231335-c0f41cb1a7a0/go.mod h1:/lliqkxwWAhPjf5oSOIJup2XcqJaw8RGS6k3TGEc7GI= diff --git a/logger.go b/logger.go new file mode 100644 index 0000000..6a8ce7a --- /dev/null +++ b/logger.go @@ -0,0 +1,22 @@ +package middleware + +import ( + "net/http" + + "log/slog" +) + +func Logger(handler http.Handler, logger *slog.Logger) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + handler.ServeHTTP(w, r) + requestID := GetRequestID(r) + logger.InfoContext( + r.Context(), + "request", + slog.String("proto", r.Proto), + slog.String("method", r.Method), + slog.String("request_uri", r.RequestURI), + slog.String("request_id", requestID), + ) + }) +} diff --git a/recover.go b/recover.go new file mode 100644 index 0000000..cbe12ac --- /dev/null +++ b/recover.go @@ -0,0 +1,32 @@ +package middleware + +import ( + "net/http" + "runtime/debug" + + "log/slog" +) + +func Recover(handler http.Handler, logger *slog.Logger) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + defer func() { + err := recover() + if err == nil { + return + } + debug.PrintStack() + requestID := GetRequestID(r) + logger.ErrorContext( + r.Context(), + "panic", + slog.Any("panic", err), + slog.String("proto", r.Proto), + slog.String("method", r.Method), + slog.String("request_uri", r.RequestURI), + slog.String("request_id", requestID), + ) + }() + + handler.ServeHTTP(w, r) + }) +} diff --git a/request_id.go b/request_id.go new file mode 100644 index 0000000..67a0c82 --- /dev/null +++ b/request_id.go @@ -0,0 +1,39 @@ +package middleware + +import ( + "context" + "net/http" + + "github.com/google/uuid" +) + +type ctxKeyRequestID int + +const ( + RequestIDKey ctxKeyRequestID = 0 + RequestIDHeader string = "X-Request-ID" +) + +func RequestID(handler http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + requestID := r.Header.Get(RequestIDHeader) + if requestID == "" { + requestID = uuid.NewString() + } + + handler.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), RequestIDKey, requestID))) + }) +} + +func GetRequestID(r *http.Request) string { + rid := r.Context().Value(RequestIDKey) + if rid == nil { + return "" + } + srid, ok := rid.(string) + if !ok { + return "" + } + + return srid +}