123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- package common
- import (
- "encoding/hex"
- "fmt"
- )
- import (
- "github.com/spf13/viper"
- )
- //配置文件
- var (
- BasicConfig = new(basic)
- ServerConfig = new(server)
- LoggerConfig = new(logger)
- RobotConfig = new(robot)
- PostgresConfig = new(postgres)
- MetadataConfig = new(metadata)
- )
- // 应用配置
- type server struct {
- Auth string
- Resource string
- Study string
- Protocol string
- Dcmtk string
- }
- type basic struct {
- Jwt string
- Mode string
- }
- // 日志配置
- type logger struct {
- LogDir string //日志文件夹路径
- LogLevel string //日志打印等级
- MaxSize int //在进行切割之前,日志文件的最大大小(以MB为单位)
- MaxBackups int //最多保留文件个数
- MaxAge int //保留旧文件的最大天数
- Compress bool //是否压缩/归档旧文件
- Stdout bool
- }
- type robot struct {
- Info string
- Dev string
- Prod string
- }
- // 数据库配置
- type postgres struct {
- Ip string
- Port int
- Username string
- Password string
- Name string
- }
- func (p *postgres) setup() {
- raw, err := hex.DecodeString(p.Password)
- if err != nil {
- panic("db password is invalid")
- }
- pwd, err := DBPwdDecrypt(raw)
- if err != nil {
- panic("db password is invalid")
- }
- p.Password = string(pwd)
- }
- // metadata配置
- type metadata struct {
- Locales []string
- Languages []Lang
- Product Product
- Sources []Source
- }
- func (p *metadata) setup() {
- if !ValidLanguages(p.Languages) {
- panic(fmt.Sprintf("invalid languages, optional values are: %v", AllLanguages()))
- }
- if !ValidProduct(p.Product) {
- panic(fmt.Sprintf("invalid product, optional values are: %v", AllProducts()))
- }
- if !ValidSources(p.Sources) {
- panic(fmt.Sprintf("invalid sources, optional values are: %v", AllSources()))
- }
- }
- func (p *metadata) GetLanguages() []Lang {
- if len(p.Languages) == 0 {
- return AllLanguages()
- }
- return p.Languages
- }
- func (p *metadata) GetProduct() Product {
- return p.Product
- }
- func (p *metadata) GetSources() []Source {
- if len(p.Sources) == 0 {
- return AllSources()
- }
- return p.Sources
- }
- func InitBasic(cfg *viper.Viper) *basic {
- var basic basic
- err := cfg.Unmarshal(&basic)
- if err != nil {
- panic("InitBasic err")
- }
- return &basic
- }
- func InitServer(cfg *viper.Viper) *server {
- var server server
- err := cfg.Unmarshal(&server)
- if err != nil {
- panic("InitServer err")
- }
- return &server
- }
- func InitLog(cfg *viper.Viper) *logger {
- var logger logger
- err := cfg.Unmarshal(&logger)
- if err != nil {
- panic("InitLog err")
- }
- return &logger
- }
- func InitRobot(cfg *viper.Viper) *robot {
- var robot robot
- err := cfg.Unmarshal(&robot)
- if err != nil {
- panic("InitRobot error")
- }
- return &robot
- }
- func InitPostgres(cfg *viper.Viper) *postgres {
- var postgres postgres
- err := cfg.Unmarshal(&postgres)
- if err != nil {
- panic("InitPostgres err")
- }
- postgres.setup()
- return &postgres
- }
- func InitMetadata(cfg *viper.Viper) *metadata {
- var metadata metadata
- err := cfg.Unmarshal(&metadata)
- if err != nil {
- panic("InitMetadata err")
- }
- metadata.setup()
- return &metadata
- }
- // 载入配置文件
- func SetupConfig(path string, addr string, mode string) {
- viper.SetConfigFile(path)
- if err := viper.ReadInConfig(); err != nil {
- panic(err)
- }
- cfgBasic := viper.Sub("basic")
- if cfgBasic == nil {
- panic("No found basic in the configuration")
- }
- BasicConfig = InitBasic(cfgBasic)
- cfgServer := viper.Sub("server")
- if cfgServer == nil {
- panic("No found server in the configuration")
- }
- ServerConfig = InitServer(cfgServer)
- BasicConfig.Mode = mode
- if addr != "" {
- ServerConfig.Auth = addr
- }
- cfgLog := viper.Sub("logger")
- if cfgLog == nil {
- panic("No found logger in the configuration")
- }
- LoggerConfig = InitLog(cfgLog)
- cfgRobot := viper.Sub("robot")
- if cfgLog == nil {
- panic("No found robot in the configuration")
- }
- RobotConfig = InitRobot(cfgRobot)
- cfgPostgres := viper.Sub("postgres")
- if cfgPostgres == nil {
- panic("No found postgres in the configuration")
- }
- PostgresConfig = InitPostgres(cfgPostgres)
- metadataApolloConfig := viper.Sub("metadata")
- if metadataApolloConfig == nil {
- panic("No found metadata in the configuration")
- }
- MetadataConfig = InitMetadata(metadataApolloConfig)
- }
|