feat:完善项目信息

This commit is contained in:
henry
2021-12-28 09:18:32 +08:00
parent 24806c5d80
commit 76ca837fd6
9 changed files with 288 additions and 78 deletions

View File

@ -1,28 +1,26 @@
package common
package migrate
import (
config2 "SciencesServer/app/basic/config"
"SciencesServer/app/common/model"
"SciencesServer/config"
"SciencesServer/serve/orm"
"SciencesServer/utils"
"fmt"
"gorm.io/gorm"
)
type Instance struct {
gormDB *gorm.DB
}
type Option func(instance *Instance)
type synchronized struct {
iModel model.IModel
iValues func() interface{}
Catch func() interface{}
}
type caches struct {
iModel model.IModel
iValues func() interface{}
toCache func(values interface{})
}
func initModel() {
db := orm.GetDB()
func (this *Instance) Handle() {
db := this.gormDB
function := func(synchronized ...*synchronized) {
for _, v := range synchronized {
@ -139,62 +137,18 @@ func initModel() {
&synchronized{iModel: model.NewActivityExamine()}, &synchronized{iModel: model.NewActivityJoin()},
)
}
func initCacheMode() {
db := orm.GetDB()
function := func(cache ...*caches) {
for _, v := range cache {
if db.Migrator().HasTable(v.iModel) {
if v.iValues != nil {
if values := v.iValues(); values != nil {
v.toCache(values)
}
}
}
}
func WithGormDBOption(db *gorm.DB) Option {
return func(instance *Instance) {
instance.gormDB = db
}
function(
&caches{iModel: model.NewSysTenant(), iValues: func() interface{} {
out := make([]*model.SysConfig, 0)
_ = model.Find(model.NewSysConfig(), &out)
return out
}, toCache: func(values interface{}) {
out := values.([]*model.SysConfig)
for _, v := range out {
config.SystemConfig[v.Key] = v.Value
}
}},
)
function(
&caches{iModel: model.NewSysIndustry(), iValues: func() interface{} {
out := make([]*model.SysIndustry, 0)
_ = model.ScanFields(model.NewSysIndustry(), &out, []string{"id", "name"})
return out
}, toCache: func(values interface{}) {
out := values.([]*model.SysIndustry)
for _, v := range out {
config2.MemoryForIndustryInfo[fmt.Sprintf("%d", v.ID)] = v.Name
}
}},
)
function(
&caches{iModel: model.NewSysPlatform(), iValues: func() interface{} {
out := make([]*model.SysPlatform, 0)
_ = model.ScanFields(model.NewSysPlatform(), &out, []string{"id", "key", "link"})
return out
}, toCache: func(values interface{}) {
out := values.([]*model.SysPlatform)
for _, v := range out {
if v.Link == "" {
continue
}
config2.MemoryForPlatformInfo[v.Link] = v.Key
}
}},
)
}
func Init() {
initModel()
initCacheMode()
func NewInstance(options ...Option) *Instance {
out := new(Instance)
for _, v := range options {
v(out)
}
return out
}

View File

@ -25,4 +25,6 @@ func Init() {
service.Subscribe(config.EventForActivityJoinProduce, event.NewActivityJoin())
// 开启权限
service.NewAuth().Register()(config.SettingInfo.Engine.DBMode, orm.GetDB(), model.NewSysAuthRule().TableName())
// 开启缓存存储
service.NewCache()().Init()
}

81
app/service/cache.go Normal file
View File

@ -0,0 +1,81 @@
package service
import (
config2 "SciencesServer/app/basic/config"
"SciencesServer/app/common/model"
"SciencesServer/config"
"SciencesServer/serve/orm"
"fmt"
)
type Cache struct {
}
type CacheHandle func() *Cache
type caches struct {
iModel model.IModel
iValues func() interface{}
toCache func(values interface{})
}
func (this *Cache) Init() {
db := orm.GetDB()
function := func(cache ...*caches) {
for _, v := range cache {
if db.Migrator().HasTable(v.iModel) {
if v.iValues != nil {
if values := v.iValues(); values != nil {
v.toCache(values)
}
}
}
}
}
function(
&caches{iModel: model.NewSysTenant(), iValues: func() interface{} {
out := make([]*model.SysConfig, 0)
_ = model.Find(model.NewSysConfig(), &out)
return out
}, toCache: func(values interface{}) {
out := values.([]*model.SysConfig)
for _, v := range out {
config.SystemConfig[v.Key] = v.Value
}
}},
)
function(
&caches{iModel: model.NewSysIndustry(), iValues: func() interface{} {
out := make([]*model.SysIndustry, 0)
_ = model.ScanFields(model.NewSysIndustry(), &out, []string{"id", "name"})
return out
}, toCache: func(values interface{}) {
out := values.([]*model.SysIndustry)
for _, v := range out {
config2.MemoryForIndustryInfo[fmt.Sprintf("%d", v.ID)] = v.Name
}
}},
)
function(
&caches{iModel: model.NewSysPlatform(), iValues: func() interface{} {
out := make([]*model.SysPlatform, 0)
_ = model.ScanFields(model.NewSysPlatform(), &out, []string{"id", "key", "link"})
return out
}, toCache: func(values interface{}) {
out := values.([]*model.SysPlatform)
for _, v := range out {
if v.Link == "" {
continue
}
config2.MemoryForPlatformInfo[v.Link] = v.Key
}
}},
)
}
func NewCache() CacheHandle {
return func() *Cache {
return &Cache{}
}
}

32
cmd/cmd.go Normal file
View File

@ -0,0 +1,32 @@
package cmd
import (
"SciencesServer/cmd/migrate"
"SciencesServer/cmd/serve"
"fmt"
"github.com/spf13/cobra"
"os"
)
var rootCmd = &cobra.Command{
Use: "science",
Short: "Science manage server",
SilenceUsage: true,
DisableAutoGenTag: true,
Long: "Science manage server",
Version: "v1.0",
}
func init() {
rootCmd.AddCommand(migrate.Cmd)
rootCmd.AddCommand(serve.Cmd)
}
func Execute() {
err := rootCmd.Execute()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}

69
cmd/migrate/init.go Normal file
View File

@ -0,0 +1,69 @@
package migrate
import (
"SciencesServer/app/common/migrate"
"SciencesServer/serve/orm"
"SciencesServer/serve/orm/logic"
"fmt"
"github.com/spf13/cobra"
)
type mysql struct {
host string
port int
database string
username string
password string
}
type sqlite struct {
path string
name string
}
var (
Cmd = &cobra.Command{
Use: "init",
Short: "初始化配置",
Example: "serve init -m mysql -d sciences -u root -p 123456",
Run: func(cmd *cobra.Command, args []string) {
run()
},
}
mode string
_mysql = new(mysql)
_sqlite = new(sqlite)
)
func init() {
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")
Cmd.PersistentFlags().StringVarP(&_mysql.database, "mysql_database", "d", "", "数据库,默认为空")
Cmd.PersistentFlags().StringVarP(&_mysql.username, "mysql_username", "u", "root", "用户名默认为root")
Cmd.PersistentFlags().StringVarP(&_mysql.password, "mysql_password", "p", "", "密码,默认为空")
Cmd.PersistentFlags().StringVarP(&_sqlite.path, "sqlite_path", "a", "data", "Sqlite文件存放地址默认data")
Cmd.PersistentFlags().StringVarP(&_sqlite.name, "sqlite_name", "n", "app.db", "Sqlite文件存放地址默认app.db")
}
func run() {
fmt.Println("========================\n=== 初始化项目配置 ===\n========================")
// 初始化数据引擎
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",
}),
orm.WithSqliteOption(&logic.Sqlite{Path: _sqlite.path, Name: _sqlite.name}),
).Init()
fmt.Println("========================\n=== 数据引擎创建成功 ===\n========================")
// 迁移数据
migrate.NewInstance(migrate.WithGormDBOption(engine.Engine)).Handle()
fmt.Println("========================\n=== 数据初始化成功 ===\n========================")
}

76
cmd/serve/init.go Normal file
View File

@ -0,0 +1,76 @@
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"
"fmt"
"github.com/gin-gonic/gin"
"github.com/spf13/cobra"
"strings"
)
var (
Cmd = &cobra.Command{
Use: "start",
Short: "启动项目",
Run: func(cmd *cobra.Command, args []string) {
run()
},
}
configFile string
)
func init() {
Cmd.PersistentFlags().StringVarP(&configFile, "config_file", "c", "./config.yaml", "项目配置文件,默认./config.yaml")
}
func run() {
fmt.Println("========================\n==== 启动项目服务 ====\n========================")
utils.LoadConfig(configFile, 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()
})
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()
cache.Init()
cron.Init()
app.Init()
tools.Init()
// 开启Elasticsearch
es.NewInstance(es.WithEsAddress([]string{config.SettingInfo.ESServer.Host})).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())
}

View File

@ -2,7 +2,6 @@ package serve
import (
"SciencesServer/app"
"SciencesServer/app/common"
"SciencesServer/config"
"SciencesServer/cron"
"SciencesServer/router"
@ -64,7 +63,6 @@ func (this *Serve) Run() {
orm.WithSqliteOption(&logic.Sqlite{Path: config.SettingInfo.Engine.Sqlite.Path, Name: config.SettingInfo.Engine.Sqlite.Name}),
).Init().Local()
//task.Init()
common.Init()
cron.Init()
app.Init()
tools.Init()

View File

@ -56,6 +56,10 @@ type Setting struct {
IdleTimeout int `yaml:"idle_timeout"`
}
// ESServer 配置
ESServer struct {
Host string `yaml:"host"`
} `yaml:"es_server"`
// Rate 限流器
Rate struct {
Limit int `yaml:"limit"`

14
main.go
View File

@ -1,16 +1,9 @@
package main
import (
"SciencesServer/cmd/serve"
"SciencesServer/cmd"
"flag"
"runtime"
"github.com/common-nighthawk/go-figure"
)
var (
ConfigFile = flag.String("config", "./config.yaml", "DB data path")
RpcConfigFile = flag.String("rpc_config", "./keys/config.yaml", "DB data path")
)
// @title SciencesServer API文档
@ -24,6 +17,7 @@ var (
func main() {
flag.Parse()
runtime.GOMAXPROCS(runtime.NumCPU())
figure.NewFigure("SciencesServer", "", true).Print()
serve.NewServe()(&serve.Option{Config: *ConfigFile, RpcConfig: *RpcConfigFile}).Run()
//figure.NewFigure("SciencesServer", "", true).Print()
//serve.NewServe()(&serve.Option{Config: *ConfigFile, RpcConfig: *RpcConfigFile}).Run()
cmd.Execute()
}