-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
api.go
43 lines (37 loc) · 1.03 KB
/
api.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
package api
import (
"encoding/base64"
"fmt"
"beryju.io/gravity/api"
"beryju.io/gravity/pkg/instance"
roleAPI "beryju.io/gravity/pkg/roles/api"
"beryju.io/gravity/pkg/roles/api/auth"
"beryju.io/gravity/pkg/roles/api/types"
"beryju.io/gravity/pkg/tests"
"github.com/gorilla/securecookie"
)
func APIClient(rootInst *instance.Instance) (*api.APIClient, func()) {
inst := rootInst.ForRole("api")
role := roleAPI.New(inst)
ctx := tests.Context()
role.Start(ctx, []byte(tests.MustJSON(roleAPI.RoleConfig{
ListenOverride: "localhost:8008",
})))
token := base64.RawStdEncoding.EncodeToString(securecookie.GenerateRandomKey(64))
inst.KV().Put(ctx, inst.KV().Key(
types.KeyRole,
types.KeyTokens,
token,
).String(), tests.MustJSON(auth.Token{
Key: token,
Username: "foo",
}))
config := api.NewConfiguration()
config.Debug = true
config.Scheme = "http"
config.Host = "localhost:8008"
config.AddDefaultHeader("Authorization", fmt.Sprintf("Bearer %s", token))
return api.NewAPIClient(config), func() {
role.Stop()
}
}