-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
79 lines (64 loc) · 2.21 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
package config
import (
"embed"
"os"
"gopkg.in/yaml.v2"
libconfig "github.com/kujilabo/cocotola/lib/config"
lib "github.com/kujilabo/cocotola/lib/domain"
)
type AppConfig struct {
Name string `yaml:"name" validate:"required"`
HTTPPort int `yaml:"httpPort" validate:"required"`
GRPCPort int `yaml:"grpcPort" validate:"required"`
MetricsPort int `yaml:"metricsPort" validate:"required"`
}
type HTTPServer struct {
Port int `yaml:"port" validate:"required"`
ShutdownTimeSec int `yaml:"shutdownTimeSec" validate:"gte=1"`
ReadHeaderTimeoutSec int `yaml:"readHeaderTimeoutSec" validate:"gte=1"`
}
type AuthConfig struct {
Username string `yaml:"username" validate:"required"`
Password string `yaml:"password" validate:"required"`
}
type AzureConfig struct {
SubscriptionKey string `yaml:"subscriptionKey" validate:"required"`
}
type ShutdownConfig struct {
TimeSec1 int `yaml:"timeSec1" validate:"gte=1"`
TimeSec2 int `yaml:"timeSec2" validate:"gte=1"`
}
type DebugConfig struct {
GinMode bool `yaml:"ginMode"`
Wait bool `yaml:"wait"`
}
type Config struct {
App *AppConfig `yaml:"app" validate:"required"`
DB *libconfig.DBConfig `yaml:"db" validate:"required"`
Auth *AuthConfig `yaml:"auth" validate:"required"`
Azure *AzureConfig `yaml:"azure" validate:"required"`
Trace *libconfig.TraceConfig `yaml:"trace" validate:"required"`
CORS *libconfig.CORSConfig `yaml:"cors" validate:"required"`
Shutdown *ShutdownConfig `yaml:"shutdown" validate:"required"`
Log *libconfig.LogConfig `yaml:"log" validate:"required"`
Debug *DebugConfig `yaml:"debug"`
Swagger *libconfig.SwaggerConfig `yaml:"swagger" validate:"required"`
}
//go:embed local.yml
//go:embed production.yml
var config embed.FS
func LoadConfig(env string) (*Config, error) {
confContent, err := config.ReadFile(env + ".yml")
if err != nil {
return nil, err
}
confContent = []byte(os.ExpandEnv(string(confContent)))
conf := &Config{}
if err := yaml.Unmarshal(confContent, conf); err != nil {
return nil, err
}
if err := lib.Validator.Struct(conf); err != nil {
return nil, err
}
return conf, nil
}