chore: 并入 iotlcm-admin-svr 历史到 admin-svr/(原提交 49818c6)

This commit is contained in:
MiFaZhan
2026-11-02 13:27:59 +08:00
1250 changed files with 154111 additions and 0 deletions
+10
View File
@@ -0,0 +1,10 @@
// Package cmd
// @Link https://github.com/xiujiecn/xiu-admin
// @Copyright Copyright (c) 2025 LiXiujie
// @Author Lxj <xxx@example.com>
// @License https://github.com/xiujiecn/xiu-admin/blob/master/LICENSE
package cmd
var (
Main = Http
)
+61
View File
@@ -0,0 +1,61 @@
// Package cmd
// @Link https://github.com/xiujiecn/xiu-admin
// @Copyright Copyright (c) 2025 LiXiujie
// @Author Lxj <xxx@example.com>
// @License https://github.com/xiujiecn/xiu-admin/blob/master/LICENSE
package cmd
import (
"context"
"errors"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/cache"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/util/gmode"
)
// 系统初始化
func SystemInit(ctx context.Context) {
// 检查数据库连接
if err := CheckConnDB(ctx); err != nil {
panic(err)
}
// 设置gf运行模式
SetGFMode(ctx)
// 默认上海时区
if err := gtime.SetTimeZone("Asia/Shanghai"); err != nil {
g.Log().Fatalf(ctx, "时区设置异常 err: %+v", err)
return
}
// 设置缓存适配器
cache.SetAdapter(ctx)
}
// CheckConnDB 检查数据库连接
func CheckConnDB(ctx context.Context) error {
err := g.DB().PingMaster()
if err != nil {
return errors.New("连接到数据库失败")
}
return nil
}
// 设置gf运行模式
func SetGFMode(ctx context.Context) {
gfmode := g.Cfg().MustGet(ctx, "system.gfmode").String()
if len(gfmode) == 0 {
gfmode = gmode.NOT_SET
}
var modes = []string{gmode.DEVELOP, gmode.TESTING, gmode.STAGING, gmode.PRODUCT}
for _, mode := range modes {
if mode == gfmode {
gmode.Set(mode)
break
}
}
}
func DBHandle(ctx context.Context) {
}
+66
View File
@@ -0,0 +1,66 @@
package cmd
import (
"context"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/cmd/inithttp"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/exportcenter"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/rabbitmq"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/websocket"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/router"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/service"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/tasks"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcmd"
_ "git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/queues/httpqueues"
)
func init() {
inithttp.RegisterHttpInitFunc("initJobTask", initJobTask)
inithttp.RegisterHttpInitFunc("exportCenter", initExportCenter)
inithttp.RegisterHttpCleanupFunc("rabbitmq", func(ctx context.Context) error {
return rabbitmq.Close()
})
}
// initExportCenter 导出中心:补偿上次进程遗留的任务,并启动过期文件清理。
func initExportCenter(ctx context.Context) error {
exportcenter.RecoverOnStart(ctx)
exportcenter.StartCleanup(ctx)
return nil
}
var Http = &gcmd.Command{
Name: "http",
Usage: "http",
Brief: "HTTP控制台服务",
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
g.Log().Infof(ctx, "Http init start")
serverWg.Add(1)
s := g.Server()
go func() {
waitServerClose(ctx)
websocket.StopWebSocket(ctx)
s.Shutdown()
serverWg.Done()
}()
err = inithttp.InitHttp(ctx)
if err != nil {
g.Log().Errorf(ctx, "http init error: %v", err)
return err
}
s.SetDumpRouterMap(true)
router.InitRouter(ctx, s)
websocket.StartWebSocket(ctx)
s.Run()
inithttp.CleanupHttp(ctx)
return nil
},
}
func initJobTask(ctx context.Context) error {
tasks.TasksInstance(ctx)
service.SysJob().InitRegister()
return nil
}
@@ -0,0 +1,46 @@
// Package cmd
// @Link https://github.com/xiujiecn/xiu-admin
// @Copyright Copyright (c) 2025 LiXiujie
// @Author Lxj <xxx@example.com>
// @License https://github.com/xiujiecn/xiu-admin/blob/master/LICENSE
package cmd
import (
"context"
"errors"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/xgen"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gmode"
)
var (
initFuncList map[string]func(ctx context.Context) error
)
func RegisterInitFunc(name string, fn func(ctx context.Context) error) {
if initFuncList == nil {
initFuncList = make(map[string]func(ctx context.Context) error)
}
initFuncList[name] = fn
}
func InitSystemDeferFunc(ctx context.Context) error {
for name, fn := range initFuncList {
if fn == nil {
g.Log().Errorf(ctx, "InitSystemDeferFunc %s fn is nil.", name)
return errors.New("func is nil")
}
err := fn(ctx)
if err != nil {
g.Log().Errorf(ctx, "InitSystemDeferFunc %s err: %v", name, err)
return err
}
g.Log().Infof(ctx, "InitSystemDeferFunc %s success.", name)
}
// 初始化生成代码配置
if !gmode.IsProduct() {
xgen.Init(ctx)
}
return nil
}
@@ -0,0 +1,59 @@
package inithttp
import (
"context"
"sync"
"github.com/gogf/gf/v2/frame/g"
)
var (
httpInitFunc map[string]func(ctx context.Context) error
httpCleanupFunc map[string]func(ctx context.Context) error
rwLock sync.RWMutex
)
func RegisterHttpInitFunc(name string, fn func(ctx context.Context) error) {
rwLock.Lock()
defer rwLock.Unlock()
if httpInitFunc == nil {
httpInitFunc = make(map[string]func(ctx context.Context) error)
}
httpInitFunc[name] = fn
}
func RegisterHttpCleanupFunc(name string, fn func(ctx context.Context) error) {
rwLock.Lock()
defer rwLock.Unlock()
if httpCleanupFunc == nil {
httpCleanupFunc = make(map[string]func(ctx context.Context) error)
}
httpCleanupFunc[name] = fn
}
func InitHttp(ctx context.Context) error {
rwLock.RLock()
defer rwLock.RUnlock()
for name, fn := range httpInitFunc {
if err := fn(ctx); err != nil {
g.Log().Errorf(ctx, "httpInitFunc %s error: %v", name, err)
return err
} else {
g.Log().Infof(ctx, "httpInitFunc %s success", name)
}
}
return nil
}
func CleanupHttp(ctx context.Context) error {
rwLock.RLock()
defer rwLock.RUnlock()
for name, fn := range httpCleanupFunc {
if err := fn(ctx); err != nil {
g.Log().Errorf(ctx, "httpCleanupFunc %s error: %v", name, err)
} else {
g.Log().Infof(ctx, "httpCleanupFunc %s success", name)
}
}
return nil
}
+74
View File
@@ -0,0 +1,74 @@
// Package cmd
// @Link https://github.com/xiujiecn/xiu-admin
// @Copyright Copyright (c) 2025 LiXiujie
// @Author Lxj <xxx@example.com>
// @License https://github.com/xiujiecn/xiu-admin/blob/master/LICENSE
package cmd
import (
"context"
"fmt"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/cmd/inithttp"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/consts"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/websocket"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/service"
"time"
)
func init() {
inithttp.RegisterHttpInitFunc("InitMonitor", func(ctx context.Context) error {
InitMonitor()
return nil
})
}
func InitMonitor() {
go func() {
for {
RunMonitor()
time.Sleep(time.Second * 5)
}
}()
websocket.RegisterTagCallback(consts.WSTagMonitorServer, func(client *websocket.Client, tag string) {
RunMonitor()
})
}
func RunMonitor() {
ctx := context.Background()
hostInfo := service.MonitorServer().GetHostInfo(ctx)
websocket.SendToTag(consts.WSTagMonitorServer, &websocket.WResponse{
Event: fmt.Sprintf(consts.WSEventMonitorServer, consts.MonitorServerHost),
Data: hostInfo,
})
sysLoad := service.MonitorServer().GetSysLoad(ctx)
websocket.SendToTag(consts.WSTagMonitorServer, &websocket.WResponse{
Event: fmt.Sprintf(consts.WSEventMonitorServer, consts.MonitorServerSysLoad),
Data: sysLoad,
})
cpuInfo := service.MonitorServer().GetCpuInfo(ctx)
websocket.SendToTag(consts.WSTagMonitorServer, &websocket.WResponse{
Event: fmt.Sprintf(consts.WSEventMonitorServer, consts.MonitorServerCpu),
Data: cpuInfo,
})
memInfo := service.MonitorServer().GetMemInfo(ctx)
websocket.SendToTag(consts.WSTagMonitorServer, &websocket.WResponse{
Event: fmt.Sprintf(consts.WSEventMonitorServer, consts.MonitorServerMem),
Data: memInfo,
})
diskInfo := service.MonitorServer().GetDiskInfo(ctx)
websocket.SendToTag(consts.WSTagMonitorServer, &websocket.WResponse{
Event: fmt.Sprintf(consts.WSEventMonitorServer, consts.MonitorServerDisk),
Data: diskInfo,
})
netInfo := service.MonitorServer().GetNetStatusInfo(ctx)
websocket.SendToTag(consts.WSTagMonitorServer, &websocket.WResponse{
Event: fmt.Sprintf(consts.WSEventMonitorServer, consts.MonitorServerNet),
Data: netInfo,
})
goInfo := service.MonitorServer().GetGoInfo(ctx)
websocket.SendToTag(consts.WSTagMonitorServer, &websocket.WResponse{
Event: fmt.Sprintf(consts.WSEventMonitorServer, consts.MonitorServerGo),
Data: goInfo,
})
}
+58
View File
@@ -0,0 +1,58 @@
package cmd
import (
"context"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/consts"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/event"
"os"
"sync"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/os/gproc"
)
var (
serverCloseSignal = make(chan struct{}, 1)
serverWg = sync.WaitGroup{}
once sync.Once
)
// 等待接收服务关闭信号
func waitServerClose(ctx context.Context) {
signalListen(ctx, handleSignalClose)
<-serverCloseSignal
}
// 等待所有服务关闭
func waitAllServerClose(ctx context.Context) {
serverWg.Wait()
}
// 处理服务关闭信号
func handleSignalClose(sig os.Signal) {
serverCloseEvent(gctx.GetInitCtx())
serverCloseSignal <- struct{}{}
}
// 监听信号
func signalListen(ctx context.Context, handler ...gproc.SigHandler) {
g.Go(ctx, func(ctx context.Context) {
// syscall.SIGINT,
// syscall.SIGQUIT,
// syscall.SIGKILL,
// syscall.SIGTERM,
// syscall.SIGABRT.
gproc.AddSigHandlerShutdown(handler...)
gproc.Listen()
}, func(ctx context.Context, exception error) {
g.Log().Fatalf(ctx, "信号监听异常 err: %+v", exception)
})
}
// serverCloseEvent 关闭事件
func serverCloseEvent(ctx context.Context) {
once.Do(func() {
event.EventsInstance().Emit(ctx, consts.EventKeyServerClose, nil)
})
}