Merge branch 'dev' of https://github.com/go-admin-team/go-admin into go-admin-team-dev
This commit is contained in:
commit
0db2e8ef13
46 files changed
+459
-3627
No files matched your search
@@ -1,12 +1,10 @@
|
||||
package config
|
||||
|
||||
import "github.com/spf13/viper"
|
||||
|
||||
type Application struct {
|
||||
ReadTimeout int
|
||||
WriterTimeout int
|
||||
Host string
|
||||
Port string
|
||||
Port int64
|
||||
Name string
|
||||
JwtSecret string
|
||||
Mode string
|
||||
@@ -14,18 +12,4 @@ type Application struct {
|
||||
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)
|
||||
+69
-60
@@ -2,83 +2,92 @@ package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"github.com/go-admin-team/go-admin-core/config"
|
||||
"github.com/go-admin-team/go-admin-core/config/source"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// 数据库配置项
|
||||
var cfgDatabase *viper.Viper
|
||||
var (
|
||||
ExtendConfig interface{}
|
||||
)
|
||||
|
||||
// 应用配置项
|
||||
var cfgApplication *viper.Viper
|
||||
// Settings 兼容原先的配置结构
|
||||
type Settings struct {
|
||||
Settings Config `yaml:"settings"`
|
||||
}
|
||||
|
||||
// Token配置项
|
||||
var cfgJwt *viper.Viper
|
||||
// Config 配置集合
|
||||
type Config struct {
|
||||
Application *Application `yaml:"application"`
|
||||
Ssl *Ssl `yaml:"ssl"`
|
||||
Logger *Logger `yaml:"logger"`
|
||||
Jwt *Jwt `yaml:"jwt"`
|
||||
Database *Database `yaml:"database"`
|
||||
Gen *Gen `yaml:"gen"`
|
||||
Extend interface{} `yaml:"extend"`
|
||||
}
|
||||
|
||||
// Log配置项
|
||||
var cfgLogger *viper.Viper
|
||||
var (
|
||||
_watch config.Watcher
|
||||
_cfg *Settings
|
||||
)
|
||||
|
||||
// Ssl配置项 非必须
|
||||
var cfgSsl *viper.Viper
|
||||
|
||||
// 代码生成配置项 非必须
|
||||
var cfgGen *viper.Viper
|
||||
|
||||
//载入配置文件
|
||||
func Setup(path string) {
|
||||
viper.SetConfigFile(path)
|
||||
content, err := ioutil.ReadFile(path)
|
||||
// Setup 载入配置文件
|
||||
func Setup(f func(opts ...source.Option) source.Source, options ...source.Option) {
|
||||
c, err := config.NewConfig()
|
||||
if err != nil {
|
||||
log.Fatal(fmt.Sprintf("Read config file fail: %s", err.Error()))
|
||||
log.Fatal(fmt.Sprintf("New config object fail: %s", err.Error()))
|
||||
}
|
||||
|
||||
//Replace environment variables
|
||||
err = viper.ReadConfig(strings.NewReader(os.ExpandEnv(string(content))))
|
||||
err = c.Load(
|
||||
f(options...),
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatal(fmt.Sprintf("Parse config file fail: %s", err.Error()))
|
||||
log.Fatal(fmt.Sprintf("Load config source fail: %s", err.Error()))
|
||||
}
|
||||
|
||||
cfgDatabase = viper.Sub("settings.database")
|
||||
if cfgDatabase == nil {
|
||||
panic("No found settings.database in the configuration")
|
||||
_cfg = &Settings{Config{
|
||||
Application: ApplicationConfig,
|
||||
Ssl: SslConfig,
|
||||
Logger: LoggerConfig,
|
||||
Jwt: JwtConfig,
|
||||
Database: DatabaseConfig,
|
||||
Gen: GenConfig,
|
||||
Extend: ExtendConfig,
|
||||
}}
|
||||
err = c.Scan(_cfg)
|
||||
if err != nil {
|
||||
log.Fatal(fmt.Sprintf("Scan config fail: %s", err.Error()))
|
||||
}
|
||||
DatabaseConfig = InitDatabase(cfgDatabase)
|
||||
|
||||
cfgApplication = viper.Sub("settings.application")
|
||||
if cfgApplication == nil {
|
||||
panic("No found settings.application in the configuration")
|
||||
_watch, err = c.Watch()
|
||||
if err != nil {
|
||||
log.Fatal(fmt.Sprintf("Watch config source fail: %s", err.Error()))
|
||||
}
|
||||
ApplicationConfig = InitApplication(cfgApplication)
|
||||
}
|
||||
|
||||
cfgJwt = viper.Sub("settings.jwt")
|
||||
if cfgJwt == nil {
|
||||
panic("No found settings.jwt in the configuration")
|
||||
// Watch 配置监听, 重载时报错,不影响运行
|
||||
func Watch() {
|
||||
for {
|
||||
v, err := _watch.Next()
|
||||
if err != nil {
|
||||
if err.Error() != "watch stopped" {
|
||||
log.Println(fmt.Sprintf("Next config fail: %s", err.Error()))
|
||||
}
|
||||
break
|
||||
}
|
||||
err = v.Scan(_cfg)
|
||||
if err != nil {
|
||||
log.Println(fmt.Sprintf("Scan config fail: %s", err.Error()))
|
||||
break
|
||||
}
|
||||
fmt.Println(DatabaseConfig)
|
||||
}
|
||||
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")
|
||||
// Stop 停止监听
|
||||
func Stop() {
|
||||
err := _watch.Stop()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
GenConfig = InitGen(cfgGen)
|
||||
}
|
||||
@@ -1,19 +1,8 @@
|
||||
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)
|
||||
@@ -1,17 +1,8 @@
|
||||
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)
|
||||
@@ -1,19 +1,8 @@
|
||||
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)
|
||||
@@ -1,7 +1,5 @@
|
||||
package config
|
||||
|
||||
import "github.com/spf13/viper"
|
||||
|
||||
type Logger struct {
|
||||
Path string
|
||||
Level string
|
||||
@@ -12,16 +10,4 @@ type Logger struct {
|
||||
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)
|
||||
@@ -1,7 +1,5 @@
|
||||
package config
|
||||
|
||||
import "github.com/spf13/viper"
|
||||
|
||||
type Ssl struct {
|
||||
KeyStr string
|
||||
Pem string
|
||||
@@ -9,13 +7,4 @@ type Ssl struct {
|
||||
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)
|
||||
@@ -11,3 +11,7 @@ const (
|
||||
Mysql = "mysql" //mysql数据库标识
|
||||
Sqlite = "sqlite3" //sqlite
|
||||
)
|
||||
|
||||
func (e Mode) String() string {
|
||||
return string(e)
|
||||
}
|
||||
@@ -72,7 +72,6 @@ func Start() {
|
||||
func Stop(ctx context.Context) {
|
||||
quit := make(chan os.Signal)
|
||||
signal.Notify(quit, os.Interrupt)
|
||||
<-quit
|
||||
fmt.Printf("%s Shutdown Server ... \r\n", tools.GetCurrentTimeStr())
|
||||
err := _server.Shutdown(ctx)
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user