mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-08-28 13:03:22 +08:00
v2.0
This commit is contained in:
85
server/internal/cmd/cmd.go
Normal file
85
server/internal/cmd/cmd.go
Normal file
@@ -0,0 +1,85 @@
|
||||
// Package cmd
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2022 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
//
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gcmd"
|
||||
"github.com/gogf/gf/v2/os/grpool"
|
||||
)
|
||||
|
||||
var (
|
||||
serverCloseSignal chan struct{}
|
||||
Main = &gcmd.Command{
|
||||
Description: `
|
||||
欢迎使用HotGo!
|
||||
---------------------------------------------------------------------------------
|
||||
启动服务
|
||||
>> HTTP服务 [gf run main.go --args "http"]
|
||||
>> 消息队列 [gf run main.go --args "queue"]
|
||||
>> 所有服务 [gf run main.go --args "all"]
|
||||
|
||||
---------------------------------------------------------------------------------
|
||||
工具
|
||||
>> 释放casbin权限,用于清理无效的权限设置 [gf run main.go --args "tools -m=casbin -a1=refresh"]
|
||||
`,
|
||||
}
|
||||
|
||||
Help = &gcmd.Command{
|
||||
Name: "help",
|
||||
Brief: "查看帮助",
|
||||
Description: `
|
||||
欢迎使用 HotGo
|
||||
当前版本:v2.0.0
|
||||
`,
|
||||
}
|
||||
|
||||
All = &gcmd.Command{
|
||||
Name: "all",
|
||||
Brief: "start all server",
|
||||
Description: "this is the command entry for starting all server",
|
||||
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
|
||||
g.Log().Info(ctx, "start all server")
|
||||
|
||||
if err = grpool.AddWithRecover(ctx, func(ctx context.Context) {
|
||||
if err := Http.Func(ctx, parser); err != nil {
|
||||
g.Log().Fatal(ctx, "http server start fail:", err)
|
||||
}
|
||||
}); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err = grpool.AddWithRecover(ctx, func(ctx context.Context) {
|
||||
if err := Queue.Func(ctx, parser); err != nil {
|
||||
g.Log().Fatal(ctx, "queue consumer start fail:", err)
|
||||
}
|
||||
}); err != nil {
|
||||
g.Log().Fatal(ctx, "queue consumer start fail2:", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 信号监听
|
||||
signalListen(ctx, signalHandlerForOverall)
|
||||
|
||||
select {
|
||||
case <-serverCloseSignal:
|
||||
// ...
|
||||
}
|
||||
|
||||
g.Log().Info(ctx, "service successfully closed ..")
|
||||
return
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
if err := Main.AddCommand(Http, Queue, Tools, All, Help); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
serverCloseSignal = make(chan struct{}, 1)
|
||||
}
|
40
server/internal/cmd/handler_shutdown.go
Normal file
40
server/internal/cmd/handler_shutdown.go
Normal file
@@ -0,0 +1,40 @@
|
||||
// Package cmd
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2022 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
//
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gproc"
|
||||
"github.com/gogf/gf/v2/os/grpool"
|
||||
"hotgo/internal/crons"
|
||||
"hotgo/internal/websocket"
|
||||
"os"
|
||||
)
|
||||
|
||||
func signalHandlerForCron(sig os.Signal) {
|
||||
crons.StopALL()
|
||||
}
|
||||
|
||||
func signalHandlerForWebSocket(sig os.Signal) {
|
||||
websocket.Stop()
|
||||
}
|
||||
|
||||
func signalHandlerForOverall(sig os.Signal) {
|
||||
serverCloseSignal <- struct{}{}
|
||||
}
|
||||
|
||||
func signalListen(ctx context.Context, handler ...gproc.SigHandler) {
|
||||
err := grpool.AddWithRecover(ctx, func(ctx context.Context) {
|
||||
gproc.AddSigHandlerShutdown(handler...)
|
||||
gproc.Listen()
|
||||
})
|
||||
if err != nil {
|
||||
g.Log().Fatal(ctx, "signalListen Fatal:", err)
|
||||
return
|
||||
}
|
||||
}
|
102
server/internal/cmd/http.go
Normal file
102
server/internal/cmd/http.go
Normal file
@@ -0,0 +1,102 @@
|
||||
// Package cmd
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2022 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
//
|
||||
package cmd
|
||||
|
||||
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/casbin"
|
||||
"hotgo/internal/model"
|
||||
"hotgo/internal/router"
|
||||
"hotgo/internal/service"
|
||||
)
|
||||
|
||||
var (
|
||||
Http = &gcmd.Command{
|
||||
Name: "http",
|
||||
Usage: "http",
|
||||
Brief: "HTTP服务",
|
||||
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
|
||||
if _, err := g.Cfg().Get(ctx, "hotgo.debug"); err != nil {
|
||||
g.Log().Fatal(ctx, "配置读取异常:", err, "\r\n你确定 config/config.yaml 文件存在且格式正确吗?\r\n")
|
||||
}
|
||||
|
||||
// 加载权限
|
||||
casbin.InitEnforcer(ctx)
|
||||
|
||||
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.HookAfterOutput, service.Hook().GlobalLog)
|
||||
|
||||
s.Group("/", func(group *ghttp.RouterGroup) {
|
||||
|
||||
// 注册全局中间件
|
||||
group.Middleware(
|
||||
service.Middleware().Ctx, //必须第一个加载
|
||||
service.Middleware().CORS,
|
||||
service.Middleware().DemoLimit,
|
||||
service.Middleware().ResponseHandler,
|
||||
)
|
||||
|
||||
// 注册默认首页路由
|
||||
group.ALL("/", func(r *ghttp.Request) {
|
||||
r.Response.Write("hello hotGo!!")
|
||||
return
|
||||
})
|
||||
|
||||
group.ALL("/login", func(r *ghttp.Request) {
|
||||
r.Response.RedirectTo("/admin")
|
||||
})
|
||||
|
||||
// 注册后台路由
|
||||
router.Admin(ctx, group)
|
||||
|
||||
// 注册前台路由
|
||||
router.Api(ctx, group)
|
||||
|
||||
// 注册websocket路由
|
||||
router.WebSocket(ctx, group)
|
||||
|
||||
})
|
||||
|
||||
// 启动定时任务
|
||||
service.SysCron().StartCron(ctx)
|
||||
|
||||
// 信号监听
|
||||
signalListen(ctx, signalHandlerForCron, signalHandlerForWebSocket)
|
||||
|
||||
// 开启https访问
|
||||
var (
|
||||
sSLConfig *model.SSLConfig
|
||||
ssl, _ = g.Cfg().Get(ctx, "hotgo.ssl")
|
||||
)
|
||||
if err := ssl.Struct(&sSLConfig); err != nil {
|
||||
g.Log().Fatalf(ctx, "hotgo启动失败, ssl err:", err)
|
||||
return err
|
||||
}
|
||||
if sSLConfig != nil && sSLConfig.Switch {
|
||||
s.EnableHTTPS(sSLConfig.CrtPath, sSLConfig.KeyPath)
|
||||
}
|
||||
|
||||
// Just run the server.
|
||||
s.Run()
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
)
|
27
server/internal/cmd/queue.go
Normal file
27
server/internal/cmd/queue.go
Normal file
@@ -0,0 +1,27 @@
|
||||
// Package cmd
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2022 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
//
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gcmd"
|
||||
"hotgo/internal/queues"
|
||||
)
|
||||
|
||||
var (
|
||||
Queue = &gcmd.Command{
|
||||
Name: "queue",
|
||||
Brief: "消息队列",
|
||||
Description: ``,
|
||||
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
|
||||
g.Log().Infof(ctx, "start queue consumer..")
|
||||
queues.Run(ctx)
|
||||
return
|
||||
},
|
||||
}
|
||||
)
|
62
server/internal/cmd/tools.go
Normal file
62
server/internal/cmd/tools.go
Normal file
@@ -0,0 +1,62 @@
|
||||
// Package cmd
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2022 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
//
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gcmd"
|
||||
"hotgo/internal/library/casbin"
|
||||
)
|
||||
|
||||
var (
|
||||
Tools = &gcmd.Command{
|
||||
Name: "tools",
|
||||
Brief: "工具",
|
||||
Description: ``,
|
||||
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
|
||||
flags := parser.GetOptAll()
|
||||
g.Log().Warningf(ctx, "flags:%+v", flags)
|
||||
if len(flags) == 0 {
|
||||
g.Log().Fatal(ctx, "工具参数不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
method, ok := flags["m"]
|
||||
if !ok {
|
||||
g.Log().Fatal(ctx, "工具方法不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
switch method {
|
||||
case "casbin":
|
||||
a1, ok := flags["a1"]
|
||||
if !ok {
|
||||
g.Log().Fatal(ctx, "casbin参数不能为空")
|
||||
return
|
||||
}
|
||||
if a1 == "clear" {
|
||||
if err := casbin.Clear(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
} else if a1 == "refresh" {
|
||||
casbin.InitEnforcer(ctx)
|
||||
if err := casbin.Refresh(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
g.Log().Fatalf(ctx, "casbin参数无效,a1:%+v", a1)
|
||||
return
|
||||
}
|
||||
default:
|
||||
g.Log().Fatal(ctx, "工具方法不存在")
|
||||
}
|
||||
g.Log().Info(ctx, "执行完成!")
|
||||
return
|
||||
},
|
||||
}
|
||||
)
|
Reference in New Issue
Block a user