63 lines
1.3 KiB
Go
63 lines
1.3 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
type Config struct {
|
|
Server ServerConfig `mapstructure:"server"`
|
|
MySQL MySQLConfig `mapstructure:"mysql"`
|
|
Redis RedisConfig `mapstructure:"redis"`
|
|
Log LogConfig `mapstructure:"log"`
|
|
Cron CronConfig `mapstructure:"cron"`
|
|
}
|
|
|
|
type ServerConfig struct {
|
|
Port int `mapstructure:"port"`
|
|
ReadTimeout string `mapstructure:"readTimeout"`
|
|
WriteTimeout string `mapstructure:"writeTimeout"`
|
|
}
|
|
|
|
type MySQLConfig struct {
|
|
DSN string `mapstructure:"dsn"`
|
|
}
|
|
|
|
type RedisConfig struct {
|
|
Addr string `mapstructure:"addr"`
|
|
Password string `mapstructure:"password"`
|
|
DB int `mapstructure:"db"`
|
|
}
|
|
|
|
type LogConfig struct {
|
|
Level string `mapstructure:"level"`
|
|
Format string `mapstructure:"format"`
|
|
}
|
|
|
|
type CronConfig struct {
|
|
Enabled bool `mapstructure:"enabled"`
|
|
Timezone string `mapstructure:"timezone"`
|
|
}
|
|
|
|
func Load() (*Config, error) {
|
|
v := viper.New()
|
|
v.SetConfigName("application")
|
|
v.SetConfigType("yaml")
|
|
v.AddConfigPath(".")
|
|
|
|
v.SetEnvPrefix("APP")
|
|
v.AutomaticEnv()
|
|
|
|
if err := v.ReadInConfig(); err != nil {
|
|
return nil, fmt.Errorf("read config: %w", err)
|
|
}
|
|
|
|
var cfg Config
|
|
if err := v.Unmarshal(&cfg); err != nil {
|
|
return nil, fmt.Errorf("unmarshal config: %w", err)
|
|
}
|
|
|
|
return &cfg, nil
|
|
}
|