60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
package common
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
type Config struct {
|
|
Service Service `mapstructure:"service" json:"service"` // 服务配置
|
|
MysqlService Mysql `mapstructure:"mysql_service" json:"mysql_service"`
|
|
}
|
|
|
|
type Service struct {
|
|
Sites map[string]SiteConfig `mapstructure:"site" json:"site"`
|
|
}
|
|
|
|
type SiteConfig struct {
|
|
UnauthURI string `mapstructure:"unauth_uri" json:"unauth_uri"`
|
|
}
|
|
|
|
type Mysql struct {
|
|
Server string `mapstructure:"server" json:"server"`
|
|
Port string `mapstructure:"port" json:"port"`
|
|
Database string `mapstructure:"database" json:"database"`
|
|
Username string `mapstructure:"username" json:"username"`
|
|
Password string `mapstructure:"password" json:"password"`
|
|
}
|
|
|
|
var (
|
|
cfg Config
|
|
loadOnce sync.Once
|
|
)
|
|
|
|
func LoadConfig() Config {
|
|
loadOnce.Do(func() {
|
|
var configPath = getConfigPath()
|
|
viper.SetConfigType("yaml")
|
|
viper.SetConfigFile(configPath)
|
|
err := viper.ReadInConfig()
|
|
if err != nil {
|
|
panic(fmt.Errorf("read file failed err:%v,file:%s", err, configPath))
|
|
}
|
|
err = viper.Unmarshal(&cfg)
|
|
if err != nil {
|
|
panic(fmt.Errorf("errpr viper.Unnarshal err:%v,file:%s", err, configPath))
|
|
}
|
|
})
|
|
return cfg
|
|
}
|
|
|
|
func GetMysqlConfig() Mysql {
|
|
return cfg.MysqlService
|
|
}
|
|
|
|
func GetServiceConfig() Service {
|
|
return cfg.Service
|
|
}
|