From b25650cd20fdbb59dc96fc31aa19ce2c849d576a Mon Sep 17 00:00:00 2001 From: Kirill Sysoev Date: Thu, 18 Jan 2024 21:16:05 +0800 Subject: [PATCH] Adds connection stash middleware --- middleware/http/clientip.go | 6 ------ middleware/http/context.go | 9 +++++++++ middleware/http/stash.go | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 middleware/http/context.go create mode 100644 middleware/http/stash.go diff --git a/middleware/http/clientip.go b/middleware/http/clientip.go index c2062d4..f71ae87 100644 --- a/middleware/http/clientip.go +++ b/middleware/http/clientip.go @@ -7,12 +7,6 @@ import ( "strings" ) -type ContextKey uint - -const ( - ClientIP ContextKey = iota -) - type Provider uint8 const ( diff --git a/middleware/http/context.go b/middleware/http/context.go new file mode 100644 index 0000000..bd919bc --- /dev/null +++ b/middleware/http/context.go @@ -0,0 +1,9 @@ +package http + +type ContextKey uint + +const ( + NoKey ContextKey = iota + ClientIP + Stash +) diff --git a/middleware/http/stash.go b/middleware/http/stash.go new file mode 100644 index 0000000..859fec7 --- /dev/null +++ b/middleware/http/stash.go @@ -0,0 +1,19 @@ +package http + +import ( + "context" + "net/http" + "sync" +) + +func NewStashMiddleware() func(next http.Handler) http.Handler { + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + ctx = context.WithValue(ctx, Stash, &sync.Map{}) + r = r.WithContext(ctx) + + next.ServeHTTP(w, r) + }) + } +}