chore: 并入 iotlcm-app 历史到 app/(原提交 f40bda9)
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
// Package cmd
|
||||
// @Link https://github.com/xiujiecn/xiu-admin
|
||||
// @Copyright Copyright (c) 2025 LiXiujie
|
||||
// @Author Lxj <li@example.com>
|
||||
// @License https://github.com/xiujiecn/xiu-admin/blob/master/LICENSE
|
||||
package cmd
|
||||
|
||||
var (
|
||||
Main = Http
|
||||
)
|
||||
@@ -0,0 +1,61 @@
|
||||
// Package cmd
|
||||
// @Link https://github.com/xiujiecn/xiu-admin
|
||||
// @Copyright Copyright (c) 2025 LiXiujie
|
||||
// @Author Lxj <li@example.com>
|
||||
// @License https://github.com/xiujiecn/xiu-admin/blob/master/LICENSE
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-app/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) {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-app/internal/cmd/inithttp"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-app/internal/library/websocket"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-app/internal/router"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-app/internal/service"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-app/internal/service/allocatedispatch"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-app/internal/tasks"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gcmd"
|
||||
|
||||
_ "git.5iot.com/iotlcm-public/iotlcm-app/internal/queues/httpqueues"
|
||||
)
|
||||
|
||||
func init() {
|
||||
inithttp.RegisterHttpInitFunc("initJobTask", initJobTask)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
dumpRouterMap := g.Cfg().MustGet(ctx, "server.dumpRouterMap", false).Bool()
|
||||
s.SetDumpRouterMap(dumpRouterMap)
|
||||
router.InitRouter(ctx, s)
|
||||
websocket.StartWebSocket(ctx)
|
||||
stopAllocateRecovery := allocatedispatch.Start(ctx)
|
||||
defer stopAllocateRecovery()
|
||||
s.Run()
|
||||
stopAllocateRecovery()
|
||||
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 <li@example.com>
|
||||
// @License https://github.com/xiujiecn/xiu-admin/blob/master/LICENSE
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-app/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
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
// Package cmd
|
||||
// @Link https://github.com/xiujiecn/xiu-admin
|
||||
// @Copyright Copyright (c) 2025 LiXiujie
|
||||
// @Author Lxj <li@example.com>
|
||||
// @License https://github.com/xiujiecn/xiu-admin/blob/master/LICENSE
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-app/internal/cmd/inithttp"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-app/internal/consts"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-app/internal/library/websocket"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-app/internal/service"
|
||||
)
|
||||
|
||||
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,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"sync"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-app/internal/consts"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-app/internal/library/event"
|
||||
|
||||
"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)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user