2022-11-24 23:37:34 +08:00
|
|
|
|
// Package cmd
|
2022-02-25 17:11:17 +08:00
|
|
|
|
// @Link https://github.com/bufanyun/hotgo
|
2023-02-23 17:53:04 +08:00
|
|
|
|
// @Copyright Copyright (c) 2023 HotGo CLI
|
2022-11-24 23:37:34 +08:00
|
|
|
|
// @Author Ms <133814250@qq.com>
|
2022-02-25 17:11:17 +08:00
|
|
|
|
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
2022-11-24 23:37:34 +08:00
|
|
|
|
package cmd
|
2022-02-25 17:11:17 +08:00
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
|
|
|
"github.com/gogf/gf/v2/os/gcmd"
|
2023-02-23 17:53:04 +08:00
|
|
|
|
"hotgo/internal/library/addons"
|
2022-11-24 23:37:34 +08:00
|
|
|
|
"hotgo/internal/library/casbin"
|
|
|
|
|
"hotgo/internal/router"
|
|
|
|
|
"hotgo/internal/service"
|
2022-02-25 17:11:17 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2022-11-24 23:37:34 +08:00
|
|
|
|
Http = &gcmd.Command{
|
|
|
|
|
Name: "http",
|
|
|
|
|
Usage: "http",
|
|
|
|
|
Brief: "HTTP服务",
|
2022-02-25 17:11:17 +08:00
|
|
|
|
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
|
2022-11-24 23:37:34 +08:00
|
|
|
|
// 加载权限
|
|
|
|
|
casbin.InitEnforcer(ctx)
|
|
|
|
|
|
2022-02-25 17:11:17 +08:00
|
|
|
|
s := g.Server()
|
|
|
|
|
|
|
|
|
|
// 错误状态码接管
|
|
|
|
|
s.BindStatusHandler(404, func(r *ghttp.Request) {
|
|
|
|
|
r.Response.Writeln("404 - 你似乎来到了没有知识存在的荒原…")
|
|
|
|
|
})
|
|
|
|
|
s.BindStatusHandler(403, func(r *ghttp.Request) {
|
|
|
|
|
r.Response.Writeln("403 - 网站拒绝显示此网页")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 请求结束事件回调
|
2023-02-23 17:53:04 +08:00
|
|
|
|
s.BindHookHandler("/*any", ghttp.HookAfterOutput, service.Hook().AfterOutput)
|
2022-02-25 17:11:17 +08:00
|
|
|
|
|
|
|
|
|
s.Group("/", func(group *ghttp.RouterGroup) {
|
|
|
|
|
|
|
|
|
|
// 注册全局中间件
|
|
|
|
|
group.Middleware(
|
2022-11-24 23:37:34 +08:00
|
|
|
|
service.Middleware().Ctx, //必须第一个加载
|
|
|
|
|
service.Middleware().CORS,
|
2023-01-25 11:49:21 +08:00
|
|
|
|
service.Middleware().Blacklist,
|
2022-11-24 23:37:34 +08:00
|
|
|
|
service.Middleware().DemoLimit,
|
|
|
|
|
service.Middleware().ResponseHandler,
|
2022-02-25 17:11:17 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 注册后台路由
|
|
|
|
|
router.Admin(ctx, group)
|
|
|
|
|
|
2023-01-18 16:23:39 +08:00
|
|
|
|
// 注册Api路由
|
2022-02-25 17:11:17 +08:00
|
|
|
|
router.Api(ctx, group)
|
|
|
|
|
|
2022-11-24 23:37:34 +08:00
|
|
|
|
// 注册websocket路由
|
|
|
|
|
router.WebSocket(ctx, group)
|
|
|
|
|
|
2022-12-15 16:12:08 +08:00
|
|
|
|
// 注册前台页面路由
|
|
|
|
|
router.Home(ctx, group)
|
2023-02-23 17:53:04 +08:00
|
|
|
|
|
|
|
|
|
// 注册插件路由
|
|
|
|
|
addons.RegisterModulesRouter(ctx, group)
|
2022-02-25 17:11:17 +08:00
|
|
|
|
})
|
|
|
|
|
|
2022-11-24 23:37:34 +08:00
|
|
|
|
// 启动定时任务
|
|
|
|
|
service.SysCron().StartCron(ctx)
|
2022-02-25 17:11:17 +08:00
|
|
|
|
|
2022-11-24 23:37:34 +08:00
|
|
|
|
// 信号监听
|
|
|
|
|
signalListen(ctx, signalHandlerForCron, signalHandlerForWebSocket)
|
|
|
|
|
|
2023-01-18 16:23:39 +08:00
|
|
|
|
// https
|
|
|
|
|
setSSL(ctx, s)
|
2022-02-25 17:11:17 +08:00
|
|
|
|
|
|
|
|
|
// Just run the server.
|
|
|
|
|
s.Run()
|
2022-11-24 23:37:34 +08:00
|
|
|
|
|
2023-02-08 20:29:34 +08:00
|
|
|
|
return
|
2022-02-25 17:11:17 +08:00
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
)
|
2023-01-18 16:23:39 +08:00
|
|
|
|
|
|
|
|
|
func setSSL(ctx context.Context, s *ghttp.Server) {
|
|
|
|
|
config, err := service.SysConfig().GetLoadSSL(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
g.Log().Fatal(ctx, "ssl配置获取失败:err:%+v", err)
|
|
|
|
|
}
|
|
|
|
|
if config != nil && config.Switch {
|
|
|
|
|
s.EnableHTTPS(config.CrtPath, config.KeyPath)
|
|
|
|
|
}
|
|
|
|
|
}
|