Files
2021-12-28 10:38:02 +08:00

83 lines
2.7 KiB
Go

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
engineFile string
)
func init() {
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() {
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()
})
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()
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())
}