-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
52 lines (40 loc) · 1.09 KB
/
config.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
// Package config provides access to run-time configuration.
package config
import (
"context"
"cloud.google.com/go/datastore"
)
type credentials struct {
SecretKey string `datastore:",noindex"`
AccessToken string `datastore:",noindex"`
}
var cachedCreds *credentials
func loadCreds(ctx context.Context) error {
if cachedCreds != nil {
return nil
}
client, err := datastore.NewClient(ctx, datastore.DetectProjectID)
if err != nil {
return err
}
var c credentials
if err := client.Get(ctx, datastore.NameKey("credentials", "singleton", nil), &c); err != nil {
return err
}
cachedCreds = &c
return nil
}
// SecretKey returns the shared secret used to verify the signature on Github events.
func SecretKey(ctx context.Context) ([]byte, error) {
if err := loadCreds(ctx); err != nil {
return nil, err
}
return []byte(cachedCreds.SecretKey), nil
}
// AccessToken returns the access token used to authenticate requests to Github.
func AccessToken(ctx context.Context) (string, error) {
if err := loadCreds(ctx); err != nil {
return "", err
}
return cachedCreds.AccessToken, nil
}