oficina / base Public

Clone
git clone https://amadan.net/oficina/base

Plain git — no account needed to clone.

Download

Download this file

1package equipo
2
3import (
4 "context"
5 "net/http"
6 "strings"
7)
8
9// The values of Base-Capabilities: what Base, and every client the
10// member uses, can draw. CapLookupAnswers: a reply's lookup answers and
11// their sources (Message.Lookups).
12const CapLookupAnswers = "lookup_answers"
13
14// KnownCapabilities are the values Base may send, in order. A value is
15// sent for a member only while each of their clients can draw it.
16var KnownCapabilities = []string{CapLookupAnswers}
17
18// AbilityKeys are what the Base owner can turn off for a member, in the
19// order the person page lists them. Equipo's internal/wire/abilities.go
20// has the same list; Base can't import it.
21var AbilityKeys = []string{"web", "apps_read", "apps_write", "build", "go_live", "services", "services_add"}
22
23// CapabilitiesHeader is the header that carries them on every call.
24const CapabilitiesHeader = "Base-Capabilities"
25
26type capsKey struct{}
27
28// CapabilitiesFunc works out the capabilities to send, as a call is sent.
29type CapabilitiesFunc func(ctx context.Context) []string
30
31// WithCapabilities is ctx with caps to send as Base-Capabilities on every
32// call made with it (none: the header is left out).
33func WithCapabilities(ctx context.Context, caps []string) context.Context {
34 caps = append([]string(nil), caps...)
35 return WithCapabilitiesFunc(ctx, func(context.Context) []string { return caps })
36}
37
38// WithCapabilitiesFunc is ctx with f, asked for the capabilities as each
39// call made with it is sent: a call sent late (an upload, a goroutine)
40// says what holds then, not when its request began.
41func WithCapabilitiesFunc(ctx context.Context, f CapabilitiesFunc) context.Context {
42 return context.WithValue(ctx, capsKey{}, f)
43}
44
45// CapabilitiesFuncOf is ctx's CapabilitiesFunc, or nil.
46func CapabilitiesFuncOf(ctx context.Context) CapabilitiesFunc {
47 f, _ := ctx.Value(capsKey{}).(CapabilitiesFunc)
48 return f
49}
50
51// setCapabilities puts ctx's capabilities, as they are now, on req.
52func setCapabilities(ctx context.Context, req *http.Request) {
53 if f := CapabilitiesFuncOf(ctx); f != nil {
54 if caps := f(ctx); len(caps) > 0 {
55 req.Header.Set(CapabilitiesHeader, strings.Join(caps, ","))
56 }
57 }
58}
59