feat: add Go backend

This commit is contained in:
richjohnrambo
2026-10-10 20:56:17 +08:00
parent 50fcfd2d3d
commit f6024f0964
1040 changed files with 107169 additions and 0 deletions
+35
View File
@@ -0,0 +1,35 @@
// Package cmd
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2023 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/service"
)
var (
Auth = &gcmd.Command{
Name: "auth",
Brief: "系统授权,当为第三方客户开发应用项目不想将源码和可执行文件让其随意使用时,可以通过授权的方式约束使用方。",
Description: `目前已实现,一对一、一对多、有效期授权,具体使用可以参考现有逻辑结合实际场景进行改造`,
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
service.AuthClient().Start(ctx)
serverWg.Add(1)
// 信号监听
signalListen(ctx, signalHandlerForOverall)
<-serverCloseSignal
service.AuthClient().Stop(ctx)
g.Log().Debug(ctx, "auth successfully closed ..")
serverWg.Done()
return
},
}
)
+86
View File
@@ -0,0 +1,86 @@
// Package cmd
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2023 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/utility/simple"
)
var (
Main = &gcmd.Command{
Description: `默认启动所有服务`,
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
return All.Func(ctx, parser)
},
}
Help = &gcmd.Command{
Name: "help",
Brief: "查看帮助",
Description: `
命令提示符
---------------------------------------------------------------------------------
启动服务
>> 所有服务 [go run main.go] 热编译 [gf run main.go]
>> HTTP服务 [go run main.go http]
>> 消息队列 [go run main.go queue]
>> 定时任务 [go run main.go cron]
>> 查看帮助 [go run main.go help]
---------------------------------------------------------------------------------
工具
>> 释放casbin权限,用于清理无效的权限设置 [go run main.go tools -m=casbin -a1=refresh]
>> 打印所有打包的资源文件列表 [go run main.go tools -m=gres -a1=dump]
>> 打印指定打包的资源文件内容 [go run main.go tools -m=gres -a1=content -a2=resource/template/home/index.html]
---------------------------------------------------------------------------------
升级更新
>> 修复菜单关系树 [go run main.go up -m=fix -a1=menuTree]
---------------------------------------------------------------------------------
更多
github地址:https://github.com/bufanyun/hotgo
文档地址:https://github.com/bufanyun/hotgo/tree/v2.0/docs/guide-zh-CN
HotGo框架交流1群:190966648
`,
}
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().Debug(ctx, "starting all server")
// 需要启动的服务
var allServers = []*gcmd.Command{Http, Queue, Cron}
for _, server := range allServers {
var cmd = server
simple.SafeGo(ctx, func(ctx context.Context) {
if err := cmd.Func(ctx, parser); err != nil {
g.Log().Fatalf(ctx, "%v start fail:%v", cmd.Name, err)
}
})
}
// 信号监听
signalListen(ctx, signalHandlerForOverall)
<-serverCloseSignal
serverWg.Wait()
g.Log().Debug(ctx, "all service successfully closed ..")
return
},
}
)
func init() {
if err := Main.AddCommand(All, Http, Queue, Cron, Auth, Tools, Up, Help); err != nil {
panic(err)
}
}
+45
View File
@@ -0,0 +1,45 @@
// Package cmd
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2023 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/os/gcmd"
_ "hotgo/internal/crons"
"hotgo/internal/global"
"hotgo/internal/library/cron"
"hotgo/internal/service"
)
var (
Cron = &gcmd.Command{
Name: "cron",
Brief: "定时任务,用来部署一些可独立运行的定时任务,通过tcp方式和后台保持长连接通讯,动态调整任务属性。",
Description: ``,
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
// 服务日志处理
cron.Logger().SetHandlers(global.LoggingServeLogHandler)
// 启动定时任务
service.SysCron().StartCron(ctx)
// tcp客户端
service.CronClient().Start(ctx)
serverWg.Add(1)
// 信号监听
signalListen(ctx, signalHandlerForOverall)
<-serverCloseSignal
service.CronClient().Stop(ctx)
cron.StopALL()
cron.Logger().Debug(ctx, "cron successfully closed ..")
serverWg.Done()
return
},
}
)
+44
View File
@@ -0,0 +1,44 @@
// Package cmd
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2023 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/os/gctx"
"github.com/gogf/gf/v2/os/gproc"
"hotgo/internal/consts"
"hotgo/utility/simple"
"os"
"sync"
)
var (
serverCloseSignal = make(chan struct{}, 1)
serverWg = sync.WaitGroup{}
once sync.Once
)
// signalHandlerForOverall 关闭信号处理
func signalHandlerForOverall(sig os.Signal) {
serverCloseEvent(gctx.GetInitCtx())
serverCloseSignal <- struct{}{}
}
// signalListen 信号监听
func signalListen(ctx context.Context, handler ...gproc.SigHandler) {
simple.SafeGo(ctx, func(ctx context.Context) {
gproc.AddSigHandlerShutdown(handler...)
gproc.Listen()
})
}
// serverCloseEvent 关闭事件
// 区别于服务收到退出信号后的处理,只会执行一次
func serverCloseEvent(ctx context.Context) {
once.Do(func() {
simple.Event().Call(consts.EventServerClose, ctx)
})
}
+106
View File
@@ -0,0 +1,106 @@
// Package cmd
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2023 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"
"github.com/gogf/gf/v2/util/gmode"
"hotgo/internal/library/addons"
"hotgo/internal/library/casbin"
"hotgo/internal/library/hggen"
"hotgo/internal/router"
"hotgo/internal/service"
"hotgo/internal/websocket"
)
var (
Http = &gcmd.Command{
Name: "http",
Usage: "http",
Brief: "HTTP服务,也可以称为主服务,包含http、websocket、tcpserver多个可对外服务",
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
// 初始化http服务
s := g.Server()
// 初始化请求前回调
s.BindHookHandler("/*any", ghttp.HookBeforeServe, service.Hook().BeforeServe)
// 请求响应结束后回调
s.BindHookHandler("/*any", ghttp.HookAfterOutput, service.Hook().AfterOutput)
// 注册全局中间件
s.BindMiddleware("/*any", []ghttp.HandlerFunc{
service.Middleware().Ctx, // 初始化请求上下文,一般需要第一个进行加载,后续中间件存在依赖关系
service.Middleware().CORS, // 跨域中间件,自动处理跨域问题
service.Middleware().Blacklist, // IP黑名单中间件,如果请求IP被后台拉黑,所有请求将被拒绝
service.Middleware().DemoLimit, // 演示系統操作限制,当开启演示模式时,所有POST请求将被拒绝
service.Middleware().PreFilter, // 请求输入预处理,api使用gf规范路由并且XxxReq结构体实现了validate.Filter接口即可隐式预处理
service.Middleware().ResponseHandler, // HTTP响应预处理,在业务处理完成后,对响应结果进行格式化和错误过滤,将处理后的数据发送给请求方
}...)
s.Group("/", func(group *ghttp.RouterGroup) {
// 注册后台路由
router.Admin(ctx, group)
// 注册Api路由
router.Api(ctx, group)
// 注册websocket路由
router.WebSocket(ctx, group)
// 注册前台页面路由
router.Home(ctx, group)
})
// 启动插件
if err = addons.StartModules(ctx, &addons.Option{Server: s}); err != nil {
return err
}
// 初始化casbin权限
casbin.InitEnforcer(ctx)
// 初始化生成代码配置
if !gmode.IsProduct() {
hggen.InIt(ctx)
}
// 启动tcp服务
service.TCPServer().Start(ctx)
// 启动服务监控
service.AdminMonitor().StartMonitor(ctx)
// 加载ip访问黑名单
service.SysBlacklist().Load(ctx)
// 注册支付成功回调方法
service.Pay().RegisterNotifyCall()
serverWg.Add(1)
// 信号监听
signalListen(ctx, signalHandlerForOverall)
go func() {
<-serverCloseSignal
websocket.Stop() // 关闭websocket
service.TCPServer().Stop(ctx) // 关闭tcp服务器
addons.StopModules(ctx) // 停止插件
_ = s.Shutdown() // 关闭http服务,主服务建议放在最后一个关闭
g.Log().Debug(ctx, "http successfully closed ..")
serverWg.Done()
}()
// Just run the server.
s.Run()
return
},
}
)
+43
View File
@@ -0,0 +1,43 @@
// Package cmd
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2023 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/os/gcmd"
"hotgo/internal/global"
"hotgo/internal/library/queue"
_ "hotgo/internal/queues"
"hotgo/utility/simple"
)
var (
Queue = &gcmd.Command{
Name: "queue",
Brief: "消息队列",
Description: ``,
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
// 服务日志处理
queue.Logger().SetHandlers(global.LoggingServeLogHandler)
simple.SafeGo(ctx, func(ctx context.Context) {
queue.Logger().Debug(ctx, "start queue consumer..")
queue.StartConsumersListener(ctx)
queue.Logger().Debug(ctx, "start queue consumer success..")
})
serverWg.Add(1)
// 信号监听
signalListen(ctx, signalHandlerForOverall)
<-serverCloseSignal
queue.Logger().Debug(ctx, "queue successfully closed ..")
serverWg.Done()
return
},
}
)
+107
View File
@@ -0,0 +1,107 @@
// Package cmd
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2023 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
package cmd
import (
"context"
"fmt"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcmd"
"github.com/gogf/gf/v2/os/gres"
"hotgo/internal/library/casbin"
)
var (
Tools = &gcmd.Command{
Name: "tools",
Brief: "常用工具",
Description: ``,
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
args := parser.GetOptAll()
g.Log().Debugf(ctx, "tools args:%v", args)
if len(args) == 0 {
err = gerror.New("tools args cannot be empty.")
return
}
method, ok := args["m"]
if !ok {
err = gerror.New("tools method cannot be empty.")
return
}
switch method {
case "casbin":
err = handleCasbin(ctx, args)
case "gres":
err = handleGRes(ctx, args)
default:
err = gerror.Newf("tools method[%v] does not exist", method)
}
if err == nil {
g.Log().Info(ctx, "tools exec successful!")
}
return
},
}
)
// handleCasbin casbin.
func handleCasbin(ctx context.Context, args map[string]string) (err error) {
a1, ok := args["a1"]
if !ok {
err = gerror.New("casbin args cannot be empty.")
return
}
casbin.InitEnforcer(ctx)
switch a1 {
case "clear":
err = casbin.Clear(ctx)
case "refresh":
err = casbin.Refresh(ctx)
default:
err = gerror.Newf("casbin a1 is invalid, a1:%v", a1)
}
return
}
func handleGRes(ctx context.Context, args map[string]string) (err error) {
a1, ok := args["a1"]
if !ok {
err = gerror.New("gres args cannot be empty.")
return
}
switch a1 {
case "dump":
gres.Dump()
case "content":
path, ok := args["a2"]
if !ok {
err = gerror.New("缺少查看文件路径参数:`a2`")
return
}
if !gres.Contains(path) {
err = gerror.Newf("没有找到资源文件:%v", path)
return
}
content := string(gres.GetContent(path))
if len(content) == 0 {
err = gerror.Newf("没有找到资源文件内容,请确认传入`a2`参数是一个文件,a2:%v", path)
return
}
fmt.Println("以下是资源文件内容:")
fmt.Println(content)
default:
err = gerror.Newf("handleGRes a1 is invalid, a1:%v", a1)
}
return
}
+65
View File
@@ -0,0 +1,65 @@
// Package cmd
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2023 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/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcmd"
"hotgo/internal/cmd/upgrade/fix"
)
var (
Up = &gcmd.Command{
Name: "up",
Brief: "处理hotgo版本升级更新带来的兼容问题",
Description: ``,
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
args := parser.GetOptAll()
g.Log().Debugf(ctx, "up args:%v", args)
if len(args) == 0 {
err = gerror.New("up args cannot be empty.")
return
}
method, ok := args["m"]
if !ok {
err = gerror.New("up method cannot be empty.")
return
}
switch method {
case "fix":
err = handleUpgradeFix(ctx, args)
default:
err = gerror.Newf("up method[%v] does not exist", method)
}
if err == nil {
g.Log().Info(ctx, "up exec successful!")
}
return
},
}
)
// handleUpgradeFix 处理修复脚本
func handleUpgradeFix(ctx context.Context, args map[string]string) (err error) {
a1, ok := args["a1"]
if !ok {
err = gerror.New("fix args cannot be empty.")
return
}
switch a1 {
case "menuTree":
fix.UpdateAdminMenuTree(ctx)
default:
err = gerror.Newf("fix a1 is invalid, a1:%v", a1)
}
return
}
@@ -0,0 +1,48 @@
// Package fix
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2023 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
package fix
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
"hotgo/internal/dao"
"hotgo/internal/library/hgorm"
"hotgo/internal/model/entity"
)
// UpdateAdminMenuTree 更新菜单关系树
// 根据树等级从上到下依次检查,将无效的关系树进行修复更新
func UpdateAdminMenuTree(ctx context.Context) {
var list []*entity.AdminMenu
err := dao.AdminMenu.Ctx(ctx).OrderAsc("level").Scan(&list)
if err != nil {
g.Log().Fatal(ctx, err)
}
genUpdateData := func(v *entity.AdminMenu) g.Map {
update := g.Map{"updated_at": gtime.Now()}
if v.Pid <= 0 {
update["level"] = 1
update["tree"] = ""
return update
}
// 生成下级关系树
update["pid"], update["level"], update["tree"], err = hgorm.GenSubTree(ctx, &dao.AdminMenu, v.Pid)
return update
}
for _, v := range list {
update := genUpdateData(v)
if v.Level == update["level"] && v.Tree == update["tree"] {
continue
}
if _, err = dao.AdminMenu.Ctx(ctx).WherePri(v.Id).Data(genUpdateData(v)).Update(); err != nil {
g.Log().Fatal(ctx, err)
}
}
}