-
Notifications
You must be signed in to change notification settings - Fork 76
/
env_config_factory.go
66 lines (57 loc) · 2.33 KB
/
env_config_factory.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
package envfactory
import (
"strings"
"github.com/launchdarkly/ld-relay/v7/config"
ct "github.com/launchdarkly/go-configtypes"
)
// EnvConfigFactory is an abstraction of the logic for generating environment configurations that
// are partly parameterized, instead of each environment being manually configured. This is used
// in both auto-configuration mode and offline mode.
type EnvConfigFactory struct {
// DataStorePrefix is the configured data store prefix, which may contain a per-environment placeholder.
DataStorePrefix string
// DataStorePrefix is the configured data store table name, which may contain a per-environment placeholder.
TableName string
//
AllowedOrigin ct.OptStringList
AllowedHeader ct.OptStringList
}
// NewEnvConfigFactoryForAutoConfig creates an EnvConfigFactory based on the auto-configuration mode settings.
func NewEnvConfigFactoryForAutoConfig(c config.AutoConfigConfig) EnvConfigFactory {
return EnvConfigFactory{
DataStorePrefix: c.EnvDatastorePrefix,
TableName: c.EnvDatastoreTableName,
AllowedOrigin: c.EnvAllowedOrigin,
AllowedHeader: c.EnvAllowedHeader,
}
}
// NewEnvConfigFactoryForOfflineMode creates an EnvConfigFactory based on the offline mode settings.
func NewEnvConfigFactoryForOfflineMode(c config.OfflineModeConfig) EnvConfigFactory {
return EnvConfigFactory{
DataStorePrefix: c.EnvDatastorePrefix,
TableName: c.EnvDatastoreTableName,
AllowedOrigin: c.EnvAllowedOrigin,
AllowedHeader: c.EnvAllowedHeader,
}
}
// MakeEnvironmentConfig creates an EnvConfig based on both the individual EnvironmentParams and the
// properties of the EnvConfigFactory.
func (f EnvConfigFactory) MakeEnvironmentConfig(params EnvironmentParams) config.EnvConfig {
ret := config.EnvConfig{
SDKKey: params.SDKKey,
MobileKey: params.MobileKey,
EnvID: params.EnvID,
Prefix: maybeSubstituteEnvironmentID(f.DataStorePrefix, params.EnvID),
TableName: maybeSubstituteEnvironmentID(f.TableName, params.EnvID),
AllowedOrigin: f.AllowedOrigin,
AllowedHeader: f.AllowedHeader,
SecureMode: params.SecureMode,
}
if params.TTL != 0 {
ret.TTL = ct.NewOptDuration(params.TTL)
}
return ret
}
func maybeSubstituteEnvironmentID(s string, envID config.EnvironmentID) string {
return strings.ReplaceAll(s, config.AutoConfigEnvironmentIDPlaceholder, string(envID))
}