/
config.go
52 lines (40 loc) · 1.29 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 main
import (
"embed"
"os"
_ "embed"
"github.com/go-playground/validator/v10"
"gopkg.in/yaml.v2"
"github.com/kujilabo/bamboo-root/helper"
"github.com/kujilabo/bamboo-root/internal"
)
var (
Validator = validator.New()
)
type AppConfig struct {
Name string `yaml:"name" validate:"required"`
}
type Config struct {
App *AppConfig `yaml:"app" validate:"required"`
Workers map[string]*helper.WorkerClientConfig `yaml:"workers" validate:"required"`
Trace *helper.TraceConfig `yaml:"trace" validate:"required"`
Log *helper.LogConfig `yaml:"log" validate:"required"`
}
//go:embed debug.yml
var config embed.FS
func LoadConfig(env string) (*Config, error) {
filename := env + ".yml"
confContent, err := config.ReadFile(filename)
if err != nil {
return nil, internal.Errorf("config.ReadFile. filename: %s, err: %w", filename, err)
}
confContent = []byte(os.ExpandEnv(string(confContent)))
conf := &Config{}
if err := yaml.Unmarshal(confContent, conf); err != nil {
return nil, internal.Errorf("yaml.Unmarshal. filename: %s, err: %w", filename, err)
}
if err := Validator.Struct(conf); err != nil {
return nil, internal.Errorf("Validator.Structl. filename: %s, err: %w", filename, err)
}
return conf, nil
}