101 lines
3.2 KiB
Go
101 lines
3.2 KiB
Go
package serve
|
||
|
||
import (
|
||
"SciencesServer/app"
|
||
"SciencesServer/app/session"
|
||
"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/spf13/cobra"
|
||
"net/http"
|
||
)
|
||
|
||
var (
|
||
Cmd = &cobra.Command{
|
||
Use: "start",
|
||
Short: "启动项目",
|
||
Run: func(cmd *cobra.Command, args []string) {
|
||
run()
|
||
},
|
||
}
|
||
configFile string
|
||
engineFile string
|
||
)
|
||
|
||
func init() {
|
||
Cmd.PersistentFlags().StringVarP(&config.Mode, "mode", "m", "debug", "项目模式debug/release,默认debug")
|
||
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)
|
||
logger.NewLogger().Init(&logger.Option{File: obj.Log.File, LeastDay: obj.Log.LeastDay, Level: obj.Log.Level, IsStdout: false}).Load()
|
||
})
|
||
// 载入数据引擎配置
|
||
utils.LoadConfig(engineFile, config.EngineInfo, func(i interface{}) {
|
||
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.WithPort(config.SettingInfo.Server.Port),
|
||
web.WithReadTimeout(config.SettingInfo.Server.ReadTimeout),
|
||
web.WithWriteTimeout(config.SettingInfo.Server.WriteTimeout),
|
||
web.WithIdleTimeout(config.SettingInfo.Server.IdleTimeout),
|
||
web.WithHandler(router.NewRouter(&router.Option{
|
||
Mode: config.Mode, IsCors: true,
|
||
RateLimitConfig: &router.RateLimitConfig{
|
||
IsRate: true, Limit: config.SettingInfo.Rate.Limit, Capacity: config.SettingInfo.Rate.Capacity,
|
||
},
|
||
}).Init()),
|
||
web.WithFunction(func(host string) (bool, func(r *http.Request)) {
|
||
// 判断
|
||
// 安全域名
|
||
_cache, _ := cache.Cache.HGet(config.RedisKeyForTenant, host)
|
||
|
||
if _cache == "" {
|
||
return true, nil
|
||
}
|
||
return true, nil
|
||
// 解析信息
|
||
out := new(session.Tenant)
|
||
_ = out.UnmarshalBinary([]byte(_cache))
|
||
|
||
return true, func(r *http.Request) {
|
||
r.Header.Set(config.ContentForTenantID, out.IDToString())
|
||
}
|
||
}),
|
||
).Run()
|
||
}
|