feat:完善项目信息
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@ -6,7 +6,8 @@ SciencesServer
|
||||
*.exe
|
||||
*.test
|
||||
*.prof
|
||||
config.yaml
|
||||
default_engine.yaml
|
||||
default_setting.yaml
|
||||
upload/*
|
||||
log/*
|
||||
cmd/ctl/ctl
|
||||
|
@ -38,7 +38,7 @@ func (c *Sub) sync(tx *gorm.DB, database string) error {
|
||||
}
|
||||
}
|
||||
// 重新使用默认的数据库
|
||||
tx.Exec(fmt.Sprintf("use %s;", config.SettingInfo.Engine.Mysql.DBName))
|
||||
tx.Exec(fmt.Sprintf("use %s;", config.EngineInfo.Engines.Mysql.Database))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ func Init() {
|
||||
// 活动加入监听
|
||||
service.Subscribe(config.EventForActivityJoinProduce, event.NewActivityJoin())
|
||||
// 开启权限
|
||||
service.NewAuth().Register()(config.SettingInfo.Engine.DBMode, orm.GetDB(), model.NewSysAuthRule().TableName())
|
||||
service.NewAuth().Register()(config.EngineInfo.Mode, orm.GetDB(), model.NewSysAuthRule().TableName())
|
||||
// 开启缓存存储
|
||||
service.NewCache()().Init()
|
||||
}
|
||||
|
@ -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}
|
||||
}
|
||||
}
|
@ -2,16 +2,17 @@ package config
|
||||
|
||||
var (
|
||||
SettingInfo = new(Setting)
|
||||
EngineInfo = new(Engine)
|
||||
//RPCServerSettingInfo = new(RPCServerSetting)
|
||||
)
|
||||
|
||||
// Mysql 配置
|
||||
type Mysql struct {
|
||||
Database string `yaml:"database"`
|
||||
Host string `yaml:"host"`
|
||||
Port int `yaml:"port"`
|
||||
User string `yaml:"user"`
|
||||
Username string `yaml:"username"`
|
||||
Password string `yaml:"password"`
|
||||
DBName string `yaml:"db_name"`
|
||||
Parameters string `yaml:"parameters"`
|
||||
}
|
||||
|
||||
@ -60,25 +61,13 @@ type Setting struct {
|
||||
ESServer struct {
|
||||
Host string `yaml:"host"`
|
||||
} `yaml:"es_server"`
|
||||
|
||||
// Rate 限流器
|
||||
Rate struct {
|
||||
Limit int `yaml:"limit"`
|
||||
Capacity int `yaml:"capacity"`
|
||||
}
|
||||
|
||||
// Engine 配置
|
||||
Engine struct {
|
||||
Debug bool `yaml:"debug"`
|
||||
DBMode string `yaml:"db_mode"`
|
||||
MaxLifetime int `yaml:"max_lifetime"`
|
||||
MaxOpenConns int `yaml:"max_open_conns"`
|
||||
MaxIdleConns int `yaml:"max_idle_conns"`
|
||||
TablePrefix string `yaml:"table_prefix"`
|
||||
Complex bool `yaml:"complex"`
|
||||
Mysql *Mysql `yaml:"mysql"`
|
||||
Sqlite *Sqlite `yaml:"sqlite"`
|
||||
}
|
||||
|
||||
// Cache 缓存配置
|
||||
Cache struct {
|
||||
Type string `yaml:"type"`
|
||||
@ -102,6 +91,19 @@ type Setting struct {
|
||||
}
|
||||
}
|
||||
|
||||
// Engine 配置
|
||||
type Engine struct {
|
||||
Debug bool `yaml:"debug"`
|
||||
Mode string `yaml:"mode"`
|
||||
MaxLifetime int `yaml:"max_lifetime"`
|
||||
MaxOpenConns int `yaml:"max_open_conns"`
|
||||
MaxIdleConns int `yaml:"max_idle_conns"`
|
||||
Engines struct {
|
||||
Mysql *Mysql `yaml:"mysql"`
|
||||
Sqlite *Sqlite `yaml:"sqlite"`
|
||||
} `yaml:"engines"`
|
||||
}
|
||||
|
||||
// RPCServerSetting 配置
|
||||
type RPCServerSetting struct {
|
||||
Key int `yaml:"key"`
|
||||
|
24
keys/config.yaml
Normal file
24
keys/config.yaml
Normal file
@ -0,0 +1,24 @@
|
||||
# RPCServer RPC服务
|
||||
servers:
|
||||
HTTP:
|
||||
# RPC 地址
|
||||
host: 127.0.0.1
|
||||
# RPC 端口
|
||||
port: 8084
|
||||
# RPC TLS验证
|
||||
is_tls: false
|
||||
# RPC TLSName
|
||||
tls_name: SQZN_SOCKET.RPC
|
||||
# RPC pem秘钥
|
||||
pem: keys/rpc/socket/server.pem
|
||||
MQTT:
|
||||
# RPC 地址
|
||||
host: 127.0.0.1
|
||||
# RPC 端口
|
||||
port: 8084
|
||||
# RPC TLS验证
|
||||
is_tls: false
|
||||
# RPC TLSName
|
||||
tls_name: MQTT.RPC
|
||||
# RPC pem秘钥
|
||||
pem: keys/rpc/mqtt/server.pem
|
@ -8,13 +8,13 @@ import (
|
||||
)
|
||||
|
||||
type Mysql struct {
|
||||
User, Password, Host string
|
||||
Port int
|
||||
DBName, Parameters string
|
||||
Host, Username, Password string
|
||||
Port int
|
||||
Database, Parameters string
|
||||
}
|
||||
|
||||
func (this *Mysql) DSN() gorm.Dialector {
|
||||
return mysql.Open(fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?%s",
|
||||
this.User, this.Password, this.Host, this.Port, this.DBName, this.Parameters,
|
||||
this.Username, this.Password, this.Host, this.Port, this.Database, this.Parameters,
|
||||
))
|
||||
}
|
||||
|
@ -101,8 +101,8 @@ func (this *Instance) Init() *Instance {
|
||||
option := &gorm.Config{
|
||||
DisableForeignKeyConstraintWhenMigrating: true,
|
||||
NamingStrategy: schema.NamingStrategy{
|
||||
TablePrefix: this.tablePrefix,
|
||||
SingularTable: this.singularTable,
|
||||
TablePrefix: this.tablePrefix,
|
||||
//SingularTable: this.singularTable,
|
||||
},
|
||||
}
|
||||
if this.debug {
|
||||
|
35
utils/file_test.go
Normal file
35
utils/file_test.go
Normal file
@ -0,0 +1,35 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"gopkg.in/yaml.v2"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type config struct {
|
||||
Version int
|
||||
Mapping map[string]string
|
||||
}
|
||||
|
||||
func TestNewFile(t *testing.T) {
|
||||
file, err := os.OpenFile("test.yml", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
|
||||
if err != nil {
|
||||
t.Log(err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
enc := yaml.NewEncoder(file)
|
||||
|
||||
err = enc.Encode(config{
|
||||
Version: 7,
|
||||
Mapping: map[string]string{
|
||||
"key": "value1",
|
||||
"key2": "value2",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Log(err)
|
||||
return
|
||||
}
|
||||
}
|
@ -1,8 +1,6 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"SciencesServer/app/basic/config"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@ -27,11 +25,4 @@ func TestLoadConfig(t *testing.T) {
|
||||
//out := make([]*ManagePatent, 0)
|
||||
//LoadConfig(file, &out)
|
||||
//fmt.Println(AnyToJSON(out))
|
||||
|
||||
file := "../file/sys_industry.json"
|
||||
out := make([]*config.MemoryForIndustry, 0)
|
||||
|
||||
LoadConfig(file, &out)
|
||||
|
||||
fmt.Println(AnyToJSON(out))
|
||||
}
|
||||
|
Reference in New Issue
Block a user