Monorepo for Tangled
tangled.org
1package knotacl
2
3import (
4 "context"
5 "net/http"
6 "sync"
7)
8
9type lister interface {
10 GetKnotMembers(ctx context.Context, host string) ([]string, error)
11 GetRepoCollaborators(ctx context.Context, host, repoDid string) ([]string, error)
12}
13
14type requestMemo struct {
15 mu sync.Mutex
16 entries map[string][]string
17}
18
19type memoCtxKey struct{}
20
21func WithMemo(ctx context.Context) context.Context {
22 return context.WithValue(ctx, memoCtxKey{}, &requestMemo{entries: map[string][]string{}})
23}
24
25func memoFrom(ctx context.Context) *requestMemo {
26 memo, _ := ctx.Value(memoCtxKey{}).(*requestMemo)
27 return memo
28}
29
30func (m *requestMemo) get(key string) ([]string, bool) {
31 m.mu.Lock()
32 defer m.mu.Unlock()
33 v, ok := m.entries[key]
34 return v, ok
35}
36
37func (m *requestMemo) put(key string, subjects []string) {
38 m.mu.Lock()
39 defer m.mu.Unlock()
40 m.entries[key] = subjects
41}
42
43func MemoMiddleware(next http.Handler) http.Handler {
44 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
45 next.ServeHTTP(w, r.WithContext(WithMemo(r.Context())))
46 })
47}