mirror of
https://git.sr.ht/~sircmpwn/core-go
synced 2024-09-09 11:42:36 +02:00
redis: Update style
To match the s3 middleware.
This commit is contained in:
parent
156c5e2649
commit
083cb1606a
1 changed files with 8 additions and 8 deletions
|
@ -2,10 +2,9 @@ package redis
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
goRedis "github.com/go-redis/redis/v8"
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
var redisCtxKey = &contextKey{"redis"}
|
||||
|
@ -14,23 +13,24 @@ type contextKey struct {
|
|||
name string
|
||||
}
|
||||
|
||||
func Middleware(client *goRedis.Client) func(http.Handler) http.Handler {
|
||||
func Middleware(client *redis.Client) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r = r.WithContext(Context(r.Context(), client))
|
||||
ctx := Context(r.Context(), client)
|
||||
r = r.WithContext(ctx)
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Context(ctx context.Context, client *goRedis.Client) context.Context {
|
||||
func Context(ctx context.Context, client *redis.Client) context.Context {
|
||||
return context.WithValue(ctx, redisCtxKey, client)
|
||||
}
|
||||
|
||||
func ForContext(ctx context.Context) *goRedis.Client {
|
||||
raw, ok := ctx.Value(redisCtxKey).(*goRedis.Client)
|
||||
func ForContext(ctx context.Context) *redis.Client {
|
||||
raw, ok := ctx.Value(redisCtxKey).(*redis.Client)
|
||||
if !ok {
|
||||
panic(errors.New("Invalid redis context"))
|
||||
panic("Invalid redis context")
|
||||
}
|
||||
return raw
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue