From 76ca837fd66f0b29cdf70812996079734e4f0a4c Mon Sep 17 00:00:00 2001 From: henry Date: Tue, 28 Dec 2021 09:18:32 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E5=AE=8C=E5=96=84=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/{init.go => migrate/instance.go} | 86 +++++---------------- app/init.go | 2 + app/service/cache.go | 81 +++++++++++++++++++ cmd/cmd.go | 32 ++++++++ cmd/migrate/init.go | 69 +++++++++++++++++ cmd/serve/init.go | 76 ++++++++++++++++++ cmd/serve/serve.go | 2 - config/config.go | 4 + main.go | 14 +--- 9 files changed, 288 insertions(+), 78 deletions(-) rename app/common/{init.go => migrate/instance.go} (74%) create mode 100644 app/service/cache.go create mode 100644 cmd/cmd.go create mode 100644 cmd/migrate/init.go create mode 100644 cmd/serve/init.go diff --git a/app/common/init.go b/app/common/migrate/instance.go similarity index 74% rename from app/common/init.go rename to app/common/migrate/instance.go index d14e453..7ef1ee1 100644 --- a/app/common/init.go +++ b/app/common/migrate/instance.go @@ -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 } diff --git a/app/init.go b/app/init.go index 06f9e25..8078d69 100644 --- a/app/init.go +++ b/app/init.go @@ -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() } diff --git a/app/service/cache.go b/app/service/cache.go new file mode 100644 index 0000000..690050d --- /dev/null +++ b/app/service/cache.go @@ -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{} + } +} diff --git a/cmd/cmd.go b/cmd/cmd.go new file mode 100644 index 0000000..eddb599 --- /dev/null +++ b/cmd/cmd.go @@ -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) + } +} diff --git a/cmd/migrate/init.go b/cmd/migrate/init.go new file mode 100644 index 0000000..3d25c41 --- /dev/null +++ b/cmd/migrate/init.go @@ -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========================") +} diff --git a/cmd/serve/init.go b/cmd/serve/init.go new file mode 100644 index 0000000..2016081 --- /dev/null +++ b/cmd/serve/init.go @@ -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()) +} diff --git a/cmd/serve/serve.go b/cmd/serve/serve.go index d00bcad..b5880a4 100644 --- a/cmd/serve/serve.go +++ b/cmd/serve/serve.go @@ -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() diff --git a/config/config.go b/config/config.go index 184e0a1..6d9f1a4 100644 --- a/config/config.go +++ b/config/config.go @@ -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"` diff --git a/main.go b/main.go index 3de1771..ea0ff2d 100644 --- a/main.go +++ b/main.go @@ -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() }