~bigbes/core-go

ref: cd87849358edbf15f9bbe6b1c3fbea6e99f49a86 core-go/s3/middleware.go -rw-r--r-- 751 bytes
cd878493 — Conrad Hoffmann webhooks: skip webhooks with expired credentials 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package s3

import (
	"context"
	"net/http"

	"github.com/minio/minio-go/v7"
)

var minioCtxKey = &contextKey{"minio"}

type contextKey struct {
	name string
}

func Middleware(client *minio.Client) func(next http.Handler) http.Handler {
	return func(next http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			ctx := Context(r.Context(), client)
			r = r.WithContext(ctx)
			next.ServeHTTP(w, r)
		})
	}
}

func Context(ctx context.Context, client *minio.Client) context.Context {
	return context.WithValue(ctx, minioCtxKey, client)
}

func ForContext(ctx context.Context) *minio.Client {
	raw, ok := ctx.Value(minioCtxKey).(*minio.Client)
	if !ok {
		panic("Invalid minio context")
	}
	return raw
}