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 JwtSecret string
Mode string Mode string
DemoMsg string DemoMsg string
Domain string EnableDP bool
IsHttps bool
Logger *Logger
} }
func InitApplication(cfg *viper.Viper) *Application { func InitApplication(cfg *viper.Viper) *Application {
@@ -26,14 +24,7 @@ func InitApplication(cfg *viper.Viper) *Application {
JwtSecret: cfg.GetString("jwtSecret"), JwtSecret: cfg.GetString("jwtSecret"),
Mode: cfg.GetString("mode"), Mode: cfg.GetString("mode"),
DemoMsg: cfg.GetString("demoMsg"), DemoMsg: cfg.GetString("demoMsg"),
Domain: cfg.GetString("domain"), EnableDP: cfg.GetBool("enabledp"),
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"),
},
} }
} }
@@ -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 ( import (
"fmt" "fmt"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper" "github.com/spf13/viper"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"strings" "strings"
) )
// 数据库配置项
var cfgDatabase *viper.Viper var cfgDatabase *viper.Viper
// 应用配置项
var cfgApplication *viper.Viper var cfgApplication *viper.Viper
// Token配置项
var cfgJwt *viper.Viper var cfgJwt *viper.Viper
// Log配置项
var cfgLogger *viper.Viper var cfgLogger *viper.Viper
// Ssl配置项 非必须
var cfgSsl *viper.Viper var cfgSsl *viper.Viper
// 代码生成配置项 非必须
var cfgGen *viper.Viper var cfgGen *viper.Viper
//载入配置文件 //载入配置文件
func ConfigSetup(path string) { func Setup(path string) {
viper.SetConfigFile(path) viper.SetConfigFile(path)
content, err := ioutil.ReadFile(path) content, err := ioutil.ReadFile(path)
if err != nil { if err != nil {
@@ -32,37 +43,41 @@ func ConfigSetup(path string) {
cfgDatabase = viper.Sub("settings.database") cfgDatabase = viper.Sub("settings.database")
if cfgDatabase == nil { if cfgDatabase == nil {
panic("config not found settings.database") panic("No found settings.database in the configuration")
} }
DatabaseConfig = InitDatabase(cfgDatabase) DatabaseConfig = InitDatabase(cfgDatabase)
cfgApplication = viper.Sub("settings.application") cfgApplication = viper.Sub("settings.application")
if cfgApplication == nil { if cfgApplication == nil {
panic("config not found settings.application") panic("No found settings.application in the configuration")
} }
ApplicationConfig = InitApplication(cfgApplication) ApplicationConfig = InitApplication(cfgApplication)
cfgJwt = viper.Sub("settings.jwt") cfgJwt = viper.Sub("settings.jwt")
if cfgJwt == nil { if cfgJwt == nil {
panic("config not found settings.jwt") panic("No found settings.jwt in the configuration")
} }
JwtConfig = InitJwt(cfgJwt) JwtConfig = InitJwt(cfgJwt)
cfgLogger = viper.Sub("settings.logger") cfgLogger = viper.Sub("settings.logger")
if cfgLogger == nil { if cfgLogger == nil {
panic("config not found settings.logger") panic("No found settings.logger in the configuration")
} }
LoggerConfig = InitLog(cfgLogger) LoggerConfig = InitLog(cfgLogger)
cfgSsl = viper.Sub("settings.ssl") cfgSsl = viper.Sub("settings.ssl")
if cfgSsl == nil { 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)
} }
SslConfig = InitSsl(cfgSsl)
cfgGen = viper.Sub("settings.gen") cfgGen = viper.Sub("settings.gen")
if cfgGen == nil { if cfgGen == nil {
panic("config not found settings.gen") panic("No found settings.gen")
} }
GenConfig = InitGen(cfgGen) GenConfig = InitGen(cfgGen)
} }
-9
View File
@@ -5,8 +5,6 @@ import "github.com/spf13/viper"
type Database struct { type Database struct {
Driver string Driver string
Source string Source string
DBName string
Logger *Logger
} }
func InitDatabase(cfg *viper.Viper) *Database { func InitDatabase(cfg *viper.Viper) *Database {
@@ -14,13 +12,6 @@ func InitDatabase(cfg *viper.Viper) *Database {
db := &Database{ db := &Database{
Driver: cfg.GetString("driver"), Driver: cfg.GetString("driver"),
Source: cfg.GetString("source"), 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 return db
} }
+6 -2
View File
@@ -6,15 +6,19 @@ type Logger struct {
Path string Path string
Level string Level string
Stdout bool Stdout bool
Enabled bool EnabledBUS bool
EnabledREQ bool
EnabledDB bool
} }
func InitLog(cfg *viper.Viper) *Logger { func InitLog(cfg *viper.Viper) *Logger {
return &Logger{ return &Logger{
Path: cfg.GetString("path"), Path: cfg.GetString("path"),
Level: cfg.GetString("level"), Level: cfg.GetString("level"),
Enabled: cfg.GetBool("enabled"),
Stdout: cfg.GetBool("stdout"), 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 { type Ssl struct {
KeyStr string KeyStr string
Pem string Pem string
Enable bool
Domain string
} }
func InitSsl(cfg *viper.Viper) *Ssl { func InitSsl(cfg *viper.Viper) *Ssl {
return &Ssl{ return &Ssl{
KeyStr: cfg.GetString("key"), KeyStr: cfg.GetString("key"),
Pem: cfg.GetString("pem"), Pem: cfg.GetString("pem"),
Enable: cfg.GetBool("enable"),
Domain: cfg.GetString("domain"),
} }
} }