This commit is contained in:
Alexander NeonXP Kiryukhin 2024-04-06 20:05:14 +03:00
commit e2509238d2
Signed by: NeonXP
GPG key ID: 35E33E1AB7776B39
5 changed files with 105 additions and 0 deletions

8
go.mod Normal file
View file

@ -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
)

4
go.sum Normal file
View file

@ -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=

22
logger.go Normal file
View file

@ -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),
)
})
}

32
recover.go Normal file
View file

@ -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)
})
}

39
request_id.go Normal file
View file

@ -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
}