-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
121 lines (97 loc) · 2.59 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package config
/**
The config module provides access to currently available configuration.
A repository is used as the source of configuration.
Configuration at a high level is:
1. Bootstrap (can be dynamically updated)
2. Contextual (depends on current context data)
*/
import (
"fmt"
"os"
config_api "github.com/safedep/gateway/services/gen"
)
type configHolder struct {
ctx any // TODO: Standardize domain context
configRepository ConfigRepository
}
var (
globalConfig *configHolder = nil
)
func Bootstrap(file string, reloadOnChange bool) {
if globalConfig != nil {
panic("config is already bootstrapped")
}
if file == "" {
file = os.Getenv("GLOBAL_CONFIG_PATH")
if file == "" {
panic("no config source available")
}
}
repository, err := NewConfigFileRepository(file, false, reloadOnChange)
if err != nil {
panic(err)
}
globalConfig = &configHolder{
configRepository: repository,
}
}
// Get a contextual config
func Contextual(ctx any) *configHolder {
return current().withContext(ctx)
}
// Returns a wrapped configuration data with the wrapper
// providing some convenient utility method
func current() *configHolder {
if globalConfig == nil {
panic("config is used without bootstrap")
}
return globalConfig
}
func (cfg *configHolder) withContext(ctx any) *configHolder {
return &configHolder{
configRepository: cfg.configRepository,
ctx: ctx,
}
}
// Returns the configuration data as per spec
func g() *config_api.GatewayConfiguration {
cfg, err := current().configRepository.LoadGatewayConfiguration()
if err != nil {
panic(err)
}
return cfg
}
func GetMessagingConfigByName(name string) (*config_api.MessagingAdapter, error) {
if mc, ok := g().Messaging[name]; ok {
return mc, nil
} else {
return nil, fmt.Errorf("messaging adapter not found with name: %s", name)
}
}
func GetAuthenticatorByName(name string) (*config_api.GatewayAuthenticator, error) {
if a, ok := g().Authenticators[name]; ok {
return a, nil
} else {
return nil, fmt.Errorf("authenticator not found with name: %s", name)
}
}
func PdpServiceConfig() *config_api.PdpServiceConfig {
return g().Services.GetPdp()
}
func DcsServiceConfig() *config_api.DcsServiceConfig {
return g().Services.GetDcs()
}
func TapServiceConfig() *config_api.TapServiceConfig {
return g().Services.GetTap()
}
func Upstreams() []*config_api.GatewayUpstream {
return g().GetUpstreams()
}
func GetSecret(name string) (*config_api.GatewaySecret, error) {
if s, ok := g().Secrets[name]; ok {
return s, nil
} else {
return nil, fmt.Errorf("no secret found with name: %s", name)
}
}