feat:完善项目信息
This commit is contained in:
@ -6,8 +6,19 @@ import (
|
||||
"SciencesServer/serve/orm/logic"
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
"gopkg.in/yaml.v2"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Debug bool `json:"debug" yaml:"debug"`
|
||||
Mode string `json:"mode" yaml:"mode"`
|
||||
MaxLifetime int `json:"max_lifetime" yaml:"max_lifetime"`
|
||||
MaxOpenConns int `json:"max_open_conns" yaml:"max_open_conns"`
|
||||
MaxIdleConns int `json:"max_idle_conns" yaml:"max_idle_conns"`
|
||||
Engines map[string]map[string]interface{} `json:"engines" yaml:"engines"`
|
||||
}
|
||||
|
||||
type mysql struct {
|
||||
host string
|
||||
port int
|
||||
@ -30,6 +41,7 @@ var (
|
||||
run()
|
||||
},
|
||||
}
|
||||
file string
|
||||
mode string
|
||||
|
||||
_mysql = new(mysql)
|
||||
@ -37,6 +49,7 @@ var (
|
||||
)
|
||||
|
||||
func init() {
|
||||
Cmd.PersistentFlags().StringVarP(&file, "file", "f", "./default_engine.yaml", "文件存储,现支持yaml文件,默认./default_engine.yaml")
|
||||
Cmd.PersistentFlags().StringVarP(&mode, "mode", "m", "mysql", "数据引擎,支持mysql和sqlite,默认mysql")
|
||||
Cmd.PersistentFlags().StringVarP(&_mysql.host, "mysql_host", "H", "127.0.0.1", "主机名,默认127.0.0.1")
|
||||
Cmd.PersistentFlags().IntVarP(&_mysql.port, "mysql_port", "P", 3306, "端口号,默认为3306")
|
||||
@ -54,9 +67,9 @@ func run() {
|
||||
engine := orm.NewInstance(
|
||||
orm.WithDBMode(mode),
|
||||
orm.WithMysqlOption(&logic.Mysql{
|
||||
User: _mysql.username, Password: _mysql.password,
|
||||
Host: _mysql.host, Port: _mysql.port,
|
||||
DBName: _mysql.database, Parameters: "charset=utf8mb4,utf8&parseTime=True&loc=Local",
|
||||
Username: _mysql.username, Password: _mysql.password,
|
||||
Database: _mysql.database, Parameters: "charset=utf8mb4,utf8&parseTime=True&loc=Local",
|
||||
}),
|
||||
orm.WithSqliteOption(&logic.Sqlite{Path: _sqlite.path, Name: _sqlite.name}),
|
||||
).Init()
|
||||
@ -65,5 +78,44 @@ func run() {
|
||||
// 迁移数据
|
||||
migrate.NewInstance(migrate.WithGormDBOption(engine.Engine)).Handle()
|
||||
|
||||
if err := saveFile(file, &Config{
|
||||
Debug: true,
|
||||
Mode: mode,
|
||||
MaxLifetime: 3600,
|
||||
MaxOpenConns: 2000,
|
||||
MaxIdleConns: 1000,
|
||||
Engines: map[string]map[string]interface{}{
|
||||
"mysql": map[string]interface{}{
|
||||
"host": _mysql.host,
|
||||
"port": _mysql.port,
|
||||
"database": _mysql.database,
|
||||
"username": _mysql.username,
|
||||
"password": _mysql.password,
|
||||
"parameters": "charset=utf8mb4,utf8&parseTime=True&loc=Local",
|
||||
},
|
||||
"sqlite": map[string]interface{}{
|
||||
"path": _sqlite.path,
|
||||
"name": _sqlite.name,
|
||||
},
|
||||
},
|
||||
}); err != nil {
|
||||
fmt.Errorf("数据初始化文件错误:%v", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("========================\n=== 数据初始化成功 ===\n========================")
|
||||
}
|
||||
|
||||
func saveFile(_file string, data *Config) error {
|
||||
file, err := os.OpenFile(_file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
enc := yaml.NewEncoder(file)
|
||||
|
||||
err = enc.Encode(data)
|
||||
|
||||
return err
|
||||
}
|
||||
|
@ -28,10 +28,12 @@ var (
|
||||
},
|
||||
}
|
||||
configFile string
|
||||
engineFile string
|
||||
)
|
||||
|
||||
func init() {
|
||||
Cmd.PersistentFlags().StringVarP(&configFile, "config_file", "c", "./config.yaml", "项目配置文件,默认./config.yaml")
|
||||
Cmd.PersistentFlags().StringVarP(&configFile, "setting", "s", "./default_setting.yaml", "项目配置文件,默认./default_setting.yaml")
|
||||
Cmd.PersistentFlags().StringVarP(&engineFile, "engine", "e", "./default_engine.yaml", "项目数据配置文件,默认./default_engine.yaml")
|
||||
}
|
||||
|
||||
func run() {
|
||||
@ -42,21 +44,25 @@ func run() {
|
||||
obj.Upload.Exts = strings.Split(obj.Upload.Ext, ",")
|
||||
logger.NewLogger().Init(&logger.Option{File: obj.Log.File, LeastDay: obj.Log.LeastDay, Level: obj.Log.Level, IsStdout: false}).Load()
|
||||
})
|
||||
orm.NewInstance(orm.WithDebug(config.SettingInfo.Engine.Debug),
|
||||
orm.WithDBMode(config.SettingInfo.Engine.DBMode),
|
||||
orm.WithTablePrefix(config.SettingInfo.Engine.TablePrefix),
|
||||
orm.WithSingularTable(!config.SettingInfo.Engine.Complex),
|
||||
orm.WithMaxIdleConns(config.SettingInfo.Engine.MaxIdleConns),
|
||||
orm.WithMaxOpenConns(config.SettingInfo.Engine.MaxOpenConns),
|
||||
orm.WithMaxLifetime(config.SettingInfo.Engine.MaxLifetime),
|
||||
orm.WithMysqlOption(&logic.Mysql{
|
||||
User: config.SettingInfo.Engine.Mysql.User, Password: config.SettingInfo.Engine.Mysql.Password,
|
||||
Host: config.SettingInfo.Engine.Mysql.Host, Port: config.SettingInfo.Engine.Mysql.Port,
|
||||
DBName: config.SettingInfo.Engine.Mysql.DBName, Parameters: config.SettingInfo.Engine.Mysql.Parameters,
|
||||
}),
|
||||
orm.WithSqliteOption(&logic.Sqlite{Path: config.SettingInfo.Engine.Sqlite.Path, Name: config.SettingInfo.Engine.Sqlite.Name}),
|
||||
).Init().Local()
|
||||
fmt.Println(engineFile)
|
||||
utils.LoadConfig(engineFile, config.EngineInfo, func(i interface{}) {
|
||||
fmt.Println(utils.AnyToJSON(i))
|
||||
obj := i.(*config.Engine)
|
||||
|
||||
orm.NewInstance(
|
||||
orm.WithDebug(obj.Debug),
|
||||
orm.WithDBMode(obj.Mode),
|
||||
orm.WithMaxIdleConns(obj.MaxIdleConns),
|
||||
orm.WithMaxOpenConns(obj.MaxOpenConns),
|
||||
orm.WithMaxLifetime(obj.MaxLifetime),
|
||||
orm.WithMysqlOption(&logic.Mysql{
|
||||
Host: obj.Engines.Mysql.Host, Port: obj.Engines.Mysql.Port,
|
||||
Username: obj.Engines.Mysql.Username, Password: obj.Engines.Mysql.Password,
|
||||
Database: obj.Engines.Mysql.Database, Parameters: obj.Engines.Mysql.Parameters,
|
||||
}),
|
||||
orm.WithSqliteOption(&logic.Sqlite{Path: obj.Engines.Sqlite.Path, Name: obj.Engines.Sqlite.Name}),
|
||||
).Init().Local()
|
||||
})
|
||||
cache.Init()
|
||||
cron.Init()
|
||||
app.Init()
|
||||
|
@ -1,87 +0,0 @@
|
||||
package serve
|
||||
|
||||
import (
|
||||
"SciencesServer/app"
|
||||
"SciencesServer/config"
|
||||
"SciencesServer/cron"
|
||||
"SciencesServer/router"
|
||||
"SciencesServer/serve/cache"
|
||||
"SciencesServer/serve/es"
|
||||
"SciencesServer/serve/logger"
|
||||
"SciencesServer/serve/orm"
|
||||
"SciencesServer/serve/orm/logic"
|
||||
"SciencesServer/serve/web"
|
||||
"SciencesServer/tools"
|
||||
"SciencesServer/utils"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Serve struct {
|
||||
*Option
|
||||
}
|
||||
|
||||
type Option struct {
|
||||
Config string `json:"config"`
|
||||
RpcConfig string `json:"rpc_config"`
|
||||
}
|
||||
|
||||
func (this *Serve) Run() {
|
||||
// 载入配置
|
||||
utils.LoadConfig(this.Option.Config, config.SettingInfo, func(i interface{}) {
|
||||
obj := i.(*config.Setting)
|
||||
obj.Upload.Exts = strings.Split(obj.Upload.Ext, ",")
|
||||
logger.NewLogger().Init(&logger.Option{File: obj.Log.File, LeastDay: obj.Log.LeastDay, Level: obj.Log.Level, IsStdout: false}).Load()
|
||||
})
|
||||
//utils.LoadConfig(this.Option.RpcConfig, config.RPCServerSettingInfo, func(i interface{}) {
|
||||
// obj := i.(*config.RPCServerSetting)
|
||||
// go utils.TryCatch(func() {
|
||||
// options := make(map[string]*client.ServerOption, 0)
|
||||
//
|
||||
// for k, v := range obj.Servers {
|
||||
// options[k] = &client.ServerOption{
|
||||
// Host: v.Host, Port: v.Port, IsTLS: v.IsTLS, TLSName: v.TLSName, Pem: v.Pem,
|
||||
// }
|
||||
// }
|
||||
// client.NewServer(options).Run()
|
||||
// })
|
||||
//})
|
||||
cache.Init()
|
||||
orm.NewInstance(orm.WithDebug(config.SettingInfo.Engine.Debug),
|
||||
orm.WithDBMode(config.SettingInfo.Engine.DBMode),
|
||||
orm.WithTablePrefix(config.SettingInfo.Engine.TablePrefix),
|
||||
orm.WithSingularTable(!config.SettingInfo.Engine.Complex),
|
||||
orm.WithMaxIdleConns(config.SettingInfo.Engine.MaxIdleConns),
|
||||
orm.WithMaxOpenConns(config.SettingInfo.Engine.MaxOpenConns),
|
||||
orm.WithMaxLifetime(config.SettingInfo.Engine.MaxLifetime),
|
||||
orm.WithMysqlOption(&logic.Mysql{
|
||||
User: config.SettingInfo.Engine.Mysql.User, Password: config.SettingInfo.Engine.Mysql.Password,
|
||||
Host: config.SettingInfo.Engine.Mysql.Host, Port: config.SettingInfo.Engine.Mysql.Port,
|
||||
DBName: config.SettingInfo.Engine.Mysql.DBName, Parameters: config.SettingInfo.Engine.Mysql.Parameters,
|
||||
}),
|
||||
orm.WithSqliteOption(&logic.Sqlite{Path: config.SettingInfo.Engine.Sqlite.Path, Name: config.SettingInfo.Engine.Sqlite.Name}),
|
||||
).Init().Local()
|
||||
//task.Init()
|
||||
cron.Init()
|
||||
app.Init()
|
||||
tools.Init()
|
||||
// 开启Elasticsearch
|
||||
es.NewInstance(es.WithEsAddress([]string{"http://192.168.0.188:9200"})).Init().Local()
|
||||
// 开启web
|
||||
web.NewWeb()(&web.WebConfig{
|
||||
Port: config.SettingInfo.Server.Port, ReadTimeout: config.SettingInfo.Server.ReadTimeout,
|
||||
WriteTimeout: config.SettingInfo.Server.WriteTimeout, IdleTimeout: config.SettingInfo.Server.IdleTimeout,
|
||||
}).Run(router.NewRouter(&router.Option{
|
||||
Mode: gin.DebugMode, IsCors: true,
|
||||
RateLimitConfig: &router.RateLimitConfig{
|
||||
IsRate: true, Limit: config.SettingInfo.Rate.Limit, Capacity: config.SettingInfo.Rate.Capacity,
|
||||
},
|
||||
}).Init())
|
||||
}
|
||||
|
||||
func NewServe() func(option *Option) *Serve {
|
||||
return func(option *Option) *Serve {
|
||||
return &Serve{option}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user