新增系统设置接口

This commit is contained in:
wxb committed 2020-08-07 10:22:52 +08:00
commit 389d2970c0
150 files changed
+21555

No files matched your search

+31
View File
@@ -0,0 +1,31 @@
package config
import "github.com/spf13/viper"
type Application struct {
ReadTimeout int
WriterTimeout int
Host string
Port string
Name string
JwtSecret string
Mode string
DemoMsg string
EnableDP bool
}
func InitApplication(cfg *viper.Viper) *Application {
return &Application{
ReadTimeout: cfg.GetInt("readTimeout"),
WriterTimeout: cfg.GetInt("writerTimeout"),
Host: cfg.GetString("host"),
Port: cfg.GetString("port"),
Name: cfg.GetString("name"),
JwtSecret: cfg.GetString("jwtSecret"),
Mode: cfg.GetString("mode"),
DemoMsg: cfg.GetString("demoMsg"),
EnableDP: cfg.GetBool("enabledp"),
}
}
var ApplicationConfig = new(Application)
+84
View File
@@ -0,0 +1,84 @@
package config
import (
"fmt"
"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 Setup(path string) {
viper.SetConfigFile(path)
content, err := ioutil.ReadFile(path)
if err != nil {
log.Fatal(fmt.Sprintf("Read config file fail: %s", err.Error()))
}
//Replace environment variables
err = viper.ReadConfig(strings.NewReader(os.ExpandEnv(string(content))))
if err != nil {
log.Fatal(fmt.Sprintf("Parse config file fail: %s", err.Error()))
}
cfgDatabase = viper.Sub("settings.database")
if cfgDatabase == nil {
panic("No found settings.database in the configuration")
}
DatabaseConfig = InitDatabase(cfgDatabase)
cfgApplication = viper.Sub("settings.application")
if cfgApplication == nil {
panic("No found settings.application in the configuration")
}
ApplicationConfig = InitApplication(cfgApplication)
cfgJwt = viper.Sub("settings.jwt")
if cfgJwt == nil {
panic("No found settings.jwt in the configuration")
}
JwtConfig = InitJwt(cfgJwt)
cfgLogger = viper.Sub("settings.logger")
if cfgLogger == nil {
panic("No found settings.logger in the configuration")
}
LoggerConfig = InitLog(cfgLogger)
cfgSsl = viper.Sub("settings.ssl")
if cfgSsl == nil {
// 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("No found settings.gen")
}
GenConfig = InitGen(cfgGen)
}
+19
View File
@@ -0,0 +1,19 @@
package config
import "github.com/spf13/viper"
type Database struct {
Driver string
Source string
}
func InitDatabase(cfg *viper.Viper) *Database {
db := &Database{
Driver: cfg.GetString("driver"),
Source: cfg.GetString("source"),
}
return db
}
var DatabaseConfig = new(Database)
+17
View File
@@ -0,0 +1,17 @@
package config
import "github.com/spf13/viper"
type Gen struct {
DBName string
FrontPath string
}
func InitGen(cfg *viper.Viper) *Gen {
return &Gen{
DBName: cfg.GetString("dbname"),
FrontPath: cfg.GetString("frontpath"),
}
}
var GenConfig = new(Gen)
+19
View File
@@ -0,0 +1,19 @@
package config
import (
"github.com/spf13/viper"
)
type Jwt struct {
Secret string
Timeout int64
}
func InitJwt(cfg *viper.Viper) *Jwt {
return &Jwt{
Secret: cfg.GetString("secret"),
Timeout: cfg.GetInt64("timeout"),
}
}
var JwtConfig = new(Jwt)
+27
View File
@@ -0,0 +1,27 @@
package config
import "github.com/spf13/viper"
type Logger struct {
Path string
Level string
Stdout bool
EnabledBUS bool
EnabledREQ bool
EnabledDB bool
EnabledJOB bool `default:"false"`
}
func InitLog(cfg *viper.Viper) *Logger {
return &Logger{
Path: cfg.GetString("path"),
Level: cfg.GetString("level"),
Stdout: cfg.GetBool("stdout"),
EnabledBUS: cfg.GetBool("enabledbus"),
EnabledREQ: cfg.GetBool("enabledreq"),
EnabledDB: cfg.GetBool("enableddb"),
EnabledJOB: cfg.GetBool("enabledjob"),
}
}
var LoggerConfig = new(Logger)
+21
View File
@@ -0,0 +1,21 @@
package config
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"),
}
}
var SslConfig = new(Ssl)