-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
69 lines (58 loc) · 2.44 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
package config
import (
"app/adapter/casdoor"
"app/adapter/featureflags"
"app/adapter/http"
"app/adapter/logger"
"app/adapter/metrics"
"app/adapter/mongo"
"app/adapter/postgres"
"app/adapter/redis"
"app/adapter/tracing"
"os"
"github.com/go-playground/validator/v10"
"github.com/spf13/viper"
"go.uber.org/fx"
)
var (
Module = fx.Module(
"config",
fx.Provide(NewAppConfig),
fx.Provide(func(config AppConfig) casdoor.Config { return config.Casdoor }),
fx.Provide(func(config AppConfig) featureflags.Config { return config.FeatureFlags }),
fx.Provide(func(config AppConfig) http.Config { return config.HTTP }),
fx.Provide(func(config AppConfig) logger.Config { return config.Logger }),
fx.Provide(func(config AppConfig) metrics.Config { return config.Metrics }),
fx.Provide(func(config AppConfig) mongo.Config { return config.Mongo }),
fx.Provide(func(config AppConfig) postgres.Config { return config.Postgres }),
fx.Provide(func(config AppConfig) redis.Config { return config.Redis }),
fx.Provide(func(config AppConfig) tracing.Config { return config.Tracing }),
)
)
type AppConfig struct {
Casdoor casdoor.Config `mapstructure:"casdoor" validate:"required"`
Env string `mapstructure:"env" validate:"required,oneof=development staging production"`
FeatureFlags featureflags.Config `mapstructure:"feature_flags" validate:"required"`
HTTP http.Config `mapstructure:"http" validate:"required"`
Logger logger.Config `mapstructure:"logger" validate:"required"`
Metrics metrics.Config `mapstructure:"metrics" validate:"required"`
Mongo mongo.Config `mapstructure:"mongo" validate:"required"`
Postgres postgres.Config `mapstructure:"postgres" validate:"required"`
Redis redis.Config `mapstructure:"redis" validate:"required"`
Tracing tracing.Config `mapstructure:"tracing" validate:"required"`
}
func NewAppConfig(validator *validator.Validate) (AppConfig, error) {
viperInstance := viper.New()
viperInstance.MustBindEnv("env", "ENV")
viperInstance.MustBindEnv("postgres.uri", "POSTGRES_URI")
viperInstance.MustBindEnv("redis.addr", "REDIS_ADDR")
viperInstance.SetConfigFile(os.Getenv("CONFIG_PATH"))
var config AppConfig
if err := viperInstance.ReadInConfig(); err != nil {
return config, err
}
if err := viperInstance.UnmarshalExact(&config); err != nil {
return config, err
}
return config, validator.Struct(config)
}