89 lines
2.6 KiB
Go
89 lines
2.6 KiB
Go
package serve
|
||
|
||
import (
|
||
"SciencesServer/app"
|
||
"SciencesServer/app/common"
|
||
"SciencesServer/config"
|
||
"SciencesServer/cron"
|
||
"SciencesServer/router"
|
||
"SciencesServer/rpc/client"
|
||
"SciencesServer/serve/cache"
|
||
"SciencesServer/serve/es"
|
||
"SciencesServer/serve/logger"
|
||
"SciencesServer/serve/orm"
|
||
"SciencesServer/serve/web"
|
||
"SciencesServer/task"
|
||
"SciencesServer/tools"
|
||
"SciencesServer/utils"
|
||
"strings"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
type Serve struct {
|
||
*Option
|
||
}
|
||
|
||
type Option struct {
|
||
Config string `json:"config"`
|
||
RpcConfig string `json:"rpc_config"`
|
||
}
|
||
|
||
func (this *Serve) Run() {
|
||
// 载入配置
|
||
utils.LoadConfig(this.Option.Config, 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()
|
||
})
|
||
utils.LoadConfig(this.Option.RpcConfig, config.RPCServerSettingInfo, func(i interface{}) {
|
||
obj := i.(*config.RPCServerSetting)
|
||
go utils.TryCatch(func() {
|
||
options := make(map[string]*client.ServerOption, 0)
|
||
|
||
for k, v := range obj.Servers {
|
||
options[k] = &client.ServerOption{
|
||
Host: v.Host, Port: v.Port, IsTLS: v.IsTLS, TLSName: v.TLSName, Pem: v.Pem,
|
||
}
|
||
}
|
||
client.NewServer(options).Run()
|
||
})
|
||
})
|
||
cache.Init()
|
||
//orm.Init()
|
||
// TODO:待优化完善
|
||
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.WithMysqlUser(config.SettingInfo.Engine.Mysql.User),
|
||
).Init()
|
||
task.Init()
|
||
common.Init()
|
||
cron.Init()
|
||
app.Init()
|
||
tools.Init()
|
||
// 开启Elasticsearch
|
||
es.NewEs()(&es.EsConfig{Address: []string{"http://192.168.0.188:9200"}}).Run()
|
||
// 开启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())
|
||
}
|
||
|
||
func NewServe() func(option *Option) *Serve {
|
||
return func(option *Option) *Serve {
|
||
return &Serve{option}
|
||
}
|
||
}
|