feat : update config

This commit is contained in:
zhangwenjian
2020-07-27 22:34:01 +08:00
parent 2b92b100e3
commit 7e12e55b56
5 changed files with 36 additions and 38 deletions
+2 -18
View File
@@ -11,9 +11,7 @@ type Application struct {
JwtSecret string
Mode string
DemoMsg string
Domain string
IsHttps bool
Logger *Logger
EnableDP bool
}
func InitApplication(cfg *viper.Viper) *Application {
@@ -26,14 +24,7 @@ func InitApplication(cfg *viper.Viper) *Application {
JwtSecret: cfg.GetString("jwtSecret"),
Mode: cfg.GetString("mode"),
DemoMsg: cfg.GetString("demoMsg"),
Domain: cfg.GetString("domain"),
IsHttps: cfg.GetBool("ishttps"),
Logger: &Logger{
Path: cfg.GetString("logger.path"),
Level: cfg.GetString("logger.level"),
Stdout: cfg.GetBool("logger.stdout"),
Enabled: cfg.GetBool("logger.enabled"),
},
EnableDP: cfg.GetBool("enabledp"),
}
}
@@ -47,10 +38,3 @@ func portDefault(cfg *viper.Viper) string {
}
}
func isHttpsDefault(cfg *viper.Viper) bool {
if cfg.GetString("ishttps") == "" || cfg.GetBool("ishttps") == false {
return false
} else {
return true
}
}
+24 -9
View File
@@ -2,22 +2,33 @@ package config
import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"io/ioutil"
"log"
"os"
"strings"
)
// 数据库配置项
var cfgDatabase *viper.Viper
// 应用配置项
var cfgApplication *viper.Viper
// Token配置项
var cfgJwt *viper.Viper
// Log配置项
var cfgLogger *viper.Viper
// Ssl配置项 非必须
var cfgSsl *viper.Viper
// 代码生成配置项 非必须
var cfgGen *viper.Viper
//载入配置文件
func ConfigSetup(path string) {
func Setup(path string) {
viper.SetConfigFile(path)
content, err := ioutil.ReadFile(path)
if err != nil {
@@ -32,37 +43,41 @@ func ConfigSetup(path string) {
cfgDatabase = viper.Sub("settings.database")
if cfgDatabase == nil {
panic("config not found settings.database")
panic("No found settings.database in the configuration")
}
DatabaseConfig = InitDatabase(cfgDatabase)
cfgApplication = viper.Sub("settings.application")
if cfgApplication == nil {
panic("config not found settings.application")
panic("No found settings.application in the configuration")
}
ApplicationConfig = InitApplication(cfgApplication)
cfgJwt = viper.Sub("settings.jwt")
if cfgJwt == nil {
panic("config not found settings.jwt")
panic("No found settings.jwt in the configuration")
}
JwtConfig = InitJwt(cfgJwt)
cfgLogger = viper.Sub("settings.logger")
if cfgLogger == nil {
panic("config not found settings.logger")
panic("No found settings.logger in the configuration")
}
LoggerConfig = InitLog(cfgLogger)
cfgSsl = viper.Sub("settings.ssl")
if cfgSsl == nil {
panic("config not found settings.ssl")
}
// Ssl不是系统强制要求的配置,默认可以不用配置,将设置为关闭状态
fmt.Println("warning config not found settings.ssl in the configuration")
SslConfig = new(Ssl)
SslConfig.Enable = false
} else {
SslConfig = InitSsl(cfgSsl)
}
cfgGen = viper.Sub("settings.gen")
if cfgGen == nil {
panic("config not found settings.gen")
panic("No found settings.gen")
}
GenConfig = InitGen(cfgGen)
}
-9
View File
@@ -5,8 +5,6 @@ import "github.com/spf13/viper"
type Database struct {
Driver string
Source string
DBName string
Logger *Logger
}
func InitDatabase(cfg *viper.Viper) *Database {
@@ -14,13 +12,6 @@ func InitDatabase(cfg *viper.Viper) *Database {
db := &Database{
Driver: cfg.GetString("driver"),
Source: cfg.GetString("source"),
DBName: cfg.GetString("dbname"),
Logger: &Logger{
Path: cfg.GetString("logger.path"),
Level: cfg.GetString("logger.level"),
Stdout: cfg.GetBool("logger.stdout"),
Enabled: cfg.GetBool("logger.enabled"),
},
}
return db
}
+6 -2
View File
@@ -6,15 +6,19 @@ type Logger struct {
Path string
Level string
Stdout bool
Enabled bool
EnabledBUS bool
EnabledREQ bool
EnabledDB bool
}
func InitLog(cfg *viper.Viper) *Logger {
return &Logger{
Path: cfg.GetString("path"),
Level: cfg.GetString("level"),
Enabled: cfg.GetBool("enabled"),
Stdout: cfg.GetBool("stdout"),
EnabledBUS: cfg.GetBool("enabledbus"),
EnabledREQ: cfg.GetBool("enabledreq"),
EnabledDB: cfg.GetBool("enableddb"),
}
}
+4
View File
@@ -5,12 +5,16 @@ import "github.com/spf13/viper"
type Ssl struct {
KeyStr string
Pem string
Enable bool
Domain string
}
func InitSsl(cfg *viper.Viper) *Ssl {
return &Ssl{
KeyStr: cfg.GetString("key"),
Pem: cfg.GetString("pem"),
Enable: cfg.GetBool("enable"),
Domain: cfg.GetString("domain"),
}
}