-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
56 lines (45 loc) · 1.14 KB
/
context.go
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package cms
import (
"context"
"net/http"
sancus "go.sancus.dev/web/context"
)
func DefaultGetUser(ctx context.Context) User {
// No user management
return nil
}
func DefaultGetRoutePath(r *http.Request) string {
if rctx := sancus.RouteContext(r.Context()); rctx != nil {
return rctx.RoutePath
}
return r.URL.Path
}
func DefaultSetResource(ctx context.Context, res Resource) context.Context {
return context.WithValue(ctx, ResourceCtxKey, res)
}
func DefaultGetResource(ctx context.Context) Resource {
if res, ok := ctx.Value(ResourceCtxKey).(Resource); ok {
return res
}
return nil
}
func DefaultSetDirectory(ctx context.Context, dir Directory) context.Context {
return context.WithValue(ctx, DirectoryCtxKey, dir)
}
func DefaultGetDirectory(ctx context.Context) Directory {
if dir, ok := ctx.Value(DirectoryCtxKey).(Directory); ok {
return dir
}
return nil
}
var (
ResourceCtxKey = &contextKey{"Resource"}
DirectoryCtxKey = &contextKey{"Directory"}
)
// contextKey is a value for use with context.WithValue
type contextKey struct {
name string
}
func (k *contextKey) String() string {
return "cms context value " + k.name
}