hotgo/server/internal/cmd/http.go

110 lines
2.6 KiB
Go
Raw Normal View History

2022-11-24 23:37:34 +08:00
// Package cmd
2022-02-25 17:11:17 +08:00
// @Link https://github.com/bufanyun/hotgo
// @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"
"hotgo/internal/library/addons"
2022-11-24 23:37:34 +08:00
"hotgo/internal/library/casbin"
"hotgo/internal/router"
"hotgo/internal/service"
"hotgo/internal/websocket"
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 - 网站拒绝显示此网页")
})
// 初始化请求前回调
s.BindHookHandler("/*any", ghttp.HookBeforeServe, service.Hook().BeforeServe)
// 请求响应结束后回调
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,
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)
// 注册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)
// 注册前台页面路由
router.Home(ctx, group)
// 注册插件路由
addons.RegisterModulesRouter(ctx, group)
2022-02-25 17:11:17 +08:00
})
// 启动tcp服务
service.TCPServer().Start(ctx)
2022-11-24 23:37:34 +08:00
// https
setSSL(ctx, s)
2022-02-25 17:11:17 +08:00
serverWg.Add(1)
// 信号监听
signalListen(ctx, signalHandlerForOverall)
go func() {
select {
case <-serverCloseSignal:
websocket.Stop()
service.TCPServer().Stop(ctx)
s.Shutdown() // 主服务建议放在最后一个关闭
g.Log().Debug(ctx, "http successfully closed ..")
serverWg.Done()
}
}()
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
},
}
)
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)
}
}