summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander NeonXP Kiryukhin <i@neonxp.ru>2024-04-06 20:39:09 +0300
committerAlexander NeonXP Kiryukhin <i@neonxp.ru>2024-04-06 20:39:09 +0300
commit2e050160b5535e91fda411d7994a8a293410b716 (patch)
tree87fe5c5dd91ba90e257b5b3665c53601bd85d38a
parente2509238d2b7566492e3f0a6b71563277487c98e (diff)
Правки миддлвейров
-rw-r--r--logger.go33
-rw-r--r--recover.go44
-rw-r--r--request_id.go4
-rw-r--r--use.go13
4 files changed, 58 insertions, 36 deletions
diff --git a/logger.go b/logger.go
index 6a8ce7a..039bd19 100644
--- a/logger.go
+++ b/logger.go
@@ -6,17 +6,24 @@ import (
"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),
- )
- })
+func Logger(logger *slog.Logger) Middleware {
+ return func(next http.Handler) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ next.ServeHTTP(w, r)
+ requestID := GetRequestID(r)
+ args := []any{
+ slog.String("proto", r.Proto),
+ slog.String("method", r.Method),
+ slog.String("request_uri", r.RequestURI),
+ }
+ if requestID != "" {
+ args = append(args, slog.String("request_id", requestID))
+ }
+ logger.InfoContext(
+ r.Context(),
+ "request",
+ args...,
+ )
+ })
+ }
}
diff --git a/recover.go b/recover.go
index cbe12ac..6b5f2cb 100644
--- a/recover.go
+++ b/recover.go
@@ -7,26 +7,28 @@ import (
"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),
- )
- }()
+func Recover(logger *slog.Logger) Middleware {
+ return func(next http.Handler) 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)
- })
+ next.ServeHTTP(w, r)
+ })
+ }
}
diff --git a/request_id.go b/request_id.go
index 67a0c82..b3650ef 100644
--- a/request_id.go
+++ b/request_id.go
@@ -14,14 +14,14 @@ const (
RequestIDHeader string = "X-Request-ID"
)
-func RequestID(handler http.Handler) http.Handler {
+func RequestID(next 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)))
+ next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), RequestIDKey, requestID)))
})
}
diff --git a/use.go b/use.go
new file mode 100644
index 0000000..6610e2f
--- /dev/null
+++ b/use.go
@@ -0,0 +1,13 @@
+package middleware
+
+import "net/http"
+
+type Middleware func(http.Handler) http.Handler
+
+func Use(handler http.Handler, middlewares ...Middleware) http.Handler {
+ for _, h := range middlewares {
+ handler = h(handler)
+ }
+
+ return handler
+}