-
Notifications
You must be signed in to change notification settings - Fork 0
/
msc.go
117 lines (103 loc) · 2.73 KB
/
msc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
- @Author: reel
- @Date: 2023-05-17 22:49:53
- @LastEditors: reel
- @LastEditTime: 2023-12-30 20:45:48
- @Description: 后台管理中心
*/
package msc
import (
"fmt"
"net/http"
"github.com/fbs-io/core/cron"
"github.com/fbs-io/core/internal/config"
"github.com/fbs-io/core/logx"
"github.com/fbs-io/core/mscui/mscui"
"github.com/fbs-io/core/service"
"github.com/fbs-io/core/session"
"github.com/fbs-io/core/store/cache"
"github.com/gin-gonic/gin"
)
type handler struct {
config *config.Config
cache cache.Store
session session.Session
cron cron.Cron
procinfos []map[string]interface{}
sysinfo map[string]interface{}
srvStatus []map[string]interface{}
}
func Init(engine *gin.Engine, conf *config.Config, cache cache.Store, cron cron.Cron) {
m := &handler{
config: conf,
cache: cache,
session: session.New(
session.Store(cache),
session.Prefix("msc::session"),
),
cron: cron,
procinfos: make([]map[string]interface{}, 0, 20),
sysinfo: make(map[string]interface{}, 0),
srvStatus: make([]map[string]interface{}, 0),
}
m.cron.AddJob(
func() {
info := m.getProcessInfo()
data := []map[string]interface{}{}
for _, i := range info {
data = append(data, map[string]interface{}{
"pid": i.PID,
"pname": i.PName,
"cpupercent": fmt.Sprintf("%.2f", i.CpuPercent) + "%",
"meminfo": fmt.Sprintf("%d MB", i.MemInfo),
"io": fmt.Sprintf("%d ", i.IO),
})
m.procinfos = data
}
logx.Sys.Info("进程信息", logx.F("details", data))
},
"系统进程Top20",
20,
)
m.cron.AddJob(
func() {
logx.Sys.Info("系统信息", logx.Details(m.getSysInfo()))
m.srvStatus = service.Status()
logx.Sys.Info("服务状态", logx.F("details", m.srvStatus))
},
"当前系统资源使用率及服务状态查询",
2,
)
// 加载中间件
engine.Use(m.log())
engine.Use(m.cors())
engine.Use(m.signature())
// 加载静态资源
engine.GET("/website/*filepath", func(ctx *gin.Context) {
staticSrv := http.FileServer(http.FS(mscui.Static))
staticSrv.ServeHTTP(ctx.Writer, ctx.Request)
})
engine.GET("/", func(ctx *gin.Context) {
ctx.Header("Content-Type", "text/html;charset=utf-8")
ctx.String(200, string(mscui.Index))
})
engine.NoRoute(func(ctx *gin.Context) {
ctx.Header("Content-Type", "text/html;charset=utf-8")
ctx.String(200, string(mscui.Index))
ctx.Writer.Flush()
})
// 区分后台管理中心和业务路由
mscui := engine.Group("msc")
ajax := mscui.Group("ajax")
// 登陆相关
{
ajax.POST("login", m.login())
}
{
// 服务器信息
ajax.GET("/sysinfo", m.sysInfo())
ajax.GET("/hostinfo", m.hostInfo())
ajax.GET("/srvstatus", m.getSrvStatus())
ajax.POST("/srvrestart", m.setSrvRestart())
}
}