2021-12-28 09:18:32 +08:00
|
|
|
|
package serve
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"SciencesServer/app"
|
2022-01-11 10:41:46 +08:00
|
|
|
|
"SciencesServer/app/session"
|
2021-12-28 09:18:32 +08:00
|
|
|
|
"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"
|
2022-01-05 16:09:55 +08:00
|
|
|
|
"net/http"
|
2021-12-28 09:18:32 +08:00
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
Cmd = &cobra.Command{
|
|
|
|
|
Use: "start",
|
|
|
|
|
Short: "启动项目",
|
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
|
run()
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
configFile string
|
2021-12-28 10:38:02 +08:00
|
|
|
|
engineFile string
|
2021-12-28 09:18:32 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func init() {
|
2021-12-28 10:57:13 +08:00
|
|
|
|
Cmd.PersistentFlags().StringVarP(&config.Mode, "mode", "m", "debug", "项目模式debug/release,默认debug")
|
2021-12-28 10:38:02 +08:00
|
|
|
|
Cmd.PersistentFlags().StringVarP(&configFile, "setting", "s", "./default_setting.yaml", "项目配置文件,默认./default_setting.yaml")
|
|
|
|
|
Cmd.PersistentFlags().StringVarP(&engineFile, "engine", "e", "./default_engine.yaml", "项目数据配置文件,默认./default_engine.yaml")
|
2021-12-28 09:18:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func run() {
|
|
|
|
|
fmt.Println("========================\n==== 启动项目服务 ====\n========================")
|
2021-12-28 17:14:02 +08:00
|
|
|
|
// 载入配置信息
|
2021-12-28 09:18:32 +08:00
|
|
|
|
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()
|
|
|
|
|
})
|
2021-12-28 17:14:02 +08:00
|
|
|
|
// 载入数据引擎配置
|
2021-12-28 10:38:02 +08:00
|
|
|
|
utils.LoadConfig(engineFile, config.EngineInfo, func(i interface{}) {
|
|
|
|
|
obj := i.(*config.Engine)
|
2021-12-28 09:18:32 +08:00
|
|
|
|
|
2021-12-28 10:38:02 +08:00
|
|
|
|
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()
|
|
|
|
|
})
|
2021-12-28 09:18:32 +08:00
|
|
|
|
cache.Init()
|
|
|
|
|
cron.Init()
|
|
|
|
|
app.Init()
|
|
|
|
|
tools.Init()
|
|
|
|
|
// 开启Elasticsearch
|
|
|
|
|
es.NewInstance(es.WithEsAddress([]string{config.SettingInfo.ESServer.Host})).Init().Local()
|
|
|
|
|
// 开启web
|
2022-01-05 16:09:55 +08:00
|
|
|
|
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(src string) (bool, func(r *http.Request)) {
|
2022-01-11 10:41:46 +08:00
|
|
|
|
// 安全域名
|
|
|
|
|
_cache, _ := cache.Cache.HGet(config.RedisKeyForTenant, src)
|
2022-01-05 16:09:55 +08:00
|
|
|
|
|
2022-01-11 10:41:46 +08:00
|
|
|
|
if _cache == "" {
|
2022-01-05 16:09:55 +08:00
|
|
|
|
return true, nil
|
|
|
|
|
}
|
2022-01-11 10:41:46 +08:00
|
|
|
|
// 解析信息
|
|
|
|
|
out := new(session.Tenant)
|
|
|
|
|
_ = out.UnmarshalBinary([]byte(_cache))
|
|
|
|
|
|
2022-01-05 16:09:55 +08:00
|
|
|
|
return true, func(r *http.Request) {
|
2022-01-11 10:41:46 +08:00
|
|
|
|
r.Header.Set(config.ContentForTenantID, out.IDToString())
|
2022-01-05 16:09:55 +08:00
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
).Run()
|
2021-12-28 09:18:32 +08:00
|
|
|
|
}
|