forked from sev-2/raiden
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
92 lines (75 loc) · 2.99 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
package raiden
import (
"path/filepath"
"github.com/ory/viper"
)
// ----- main configuration functionality -----
type DeploymentTarget string
const (
DeploymentTargetCloud DeploymentTarget = "cloud"
DeploymentTargetSelfHosted DeploymentTarget = "self_hosted"
)
type Config struct {
AccessToken string `mapstructure:"ACCESS_TOKEN"`
AnonKey string `mapstructure:"ANON_KEY"`
BreakerEnable bool `mapstructure:"BREAKER_ENABLE"`
CorsAllowedOrigins string `mapstructure:"CORS_ALLOWED_ORIGINS"`
CorsAllowedMethods string `mapstructure:"CORS_ALLOWED_METHODS"`
CorsAllowedHeaders string `mapstructure:"CORS_ALLOWED_HEADERS"`
CorsAllowCredentials bool `mapstructure:"CORS_ALLOWED_CREDENTIALS"`
DeploymentTarget DeploymentTarget `mapstructure:"DEPLOYMENT_TARGET"`
Environment string `mapstructure:"ENVIRONMENT"`
ProjectId string `mapstructure:"PROJECT_ID"`
ProjectName string `mapstructure:"PROJECT_NAME"`
ServiceKey string `mapstructure:"SERVICE_KEY"`
ServerHost string `mapstructure:"SERVER_HOST"`
ServerPort string `mapstructure:"SERVER_PORT"`
SupabaseApiUrl string `mapstructure:"SUPABASE_API_URL"`
SupabaseApiBasePath string `mapstructure:"SUPABASE_API_BASE_PATH"`
SupabasePublicUrl string `mapstructure:"SUPABASE_PUBLIC_URL"`
TraceEnable bool `mapstructure:"TRACE_ENABLE"`
TraceCollector string `mapstructure:"TRACE_COLLECTOR"`
TraceCollectorEndpoint string `mapstructure:"TRACE_COLLECTOR_ENDPOINT"`
Version string `mapstructure:"VERSION"`
}
// The function `LoadConfig` loads a configuration file based on the provided path or uses default
// values if no path is provided.
func LoadConfig(path *string) (*Config, error) {
if path != nil && *path != "" {
folderPath := filepath.Dir(*path)
file := filepath.Base(*path)
fileExtension := filepath.Ext(file)[1:]
fileName := file[:len(file)-len(fileExtension)-1]
viper.SetConfigName(fileName)
viper.SetConfigType(fileExtension)
viper.AddConfigPath(folderPath)
} else {
viper.SetConfigName("app")
viper.SetConfigType("yaml")
viper.AddConfigPath("./configs")
}
if err := viper.ReadInConfig(); err != nil {
return nil, err
}
var config Config
if err := viper.Unmarshal(&config); err != nil {
return nil, err
}
// set default value
if config.ServerHost == "" {
config.ServerHost = "127.0.0.1"
}
if config.ServerPort != "" {
config.ServerPort = "8002"
}
if config.Version != "" {
config.Version = "1.0.0"
}
if config.Environment == "" {
config.Environment = "development"
}
if len(config.SupabaseApiBasePath) > 0 && config.SupabaseApiBasePath[0] != '/' {
config.SupabaseApiBasePath = "/" + config.SupabaseApiBasePath
}
return &config, nil
}