feat:完善项目信息
This commit is contained in:
32
cmd/cmd.go
Normal file
32
cmd/cmd.go
Normal 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
69
cmd/migrate/init.go
Normal 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
76
cmd/serve/init.go
Normal 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())
|
||||
}
|
@ -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()
|
||||
|
Reference in New Issue
Block a user