56 lines
1.0 KiB
Go
56 lines
1.0 KiB
Go
package config
|
|
|
|
import (
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
type Config struct {
|
|
Server ServerConfig
|
|
Redis RedisConfig
|
|
Services ServicesConfig
|
|
}
|
|
|
|
type ServerConfig struct {
|
|
Port int
|
|
}
|
|
|
|
type RedisConfig struct {
|
|
Addr string
|
|
Password string
|
|
DB int
|
|
}
|
|
|
|
type ServicesConfig struct {
|
|
UserService UserServiceConfig
|
|
}
|
|
|
|
type UserServiceConfig struct {
|
|
Addr string
|
|
}
|
|
|
|
func Load() (*Config, error) {
|
|
viper.SetConfigName("config")
|
|
viper.SetConfigType("yaml")
|
|
viper.AddConfigPath("./config")
|
|
viper.AddConfigPath("../config")
|
|
viper.AddConfigPath("../../config")
|
|
|
|
viper.SetDefault("server.port", 8000)
|
|
viper.SetDefault("redis.addr", "backend-redis:6379")
|
|
viper.SetDefault("redis.password", "")
|
|
viper.SetDefault("redis.db", 0)
|
|
viper.SetDefault("services.userService.addr", "backend-user-svc:9000")
|
|
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
var config Config
|
|
if err := viper.Unmarshal(&config); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &config, nil
|
|
} |