-
Notifications
You must be signed in to change notification settings - Fork 264
/
server_monitor.go
69 lines (55 loc) · 1.3 KB
/
server_monitor.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
package server
import (
"fmt"
"io/fs"
"net/http"
"strings"
"github.com/WuKongIM/WuKongIM/pkg/wkhttp"
"github.com/WuKongIM/WuKongIM/pkg/wklog"
"github.com/WuKongIM/WuKongIM/version"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
type MonitorServer struct {
s *Server
r *wkhttp.WKHttp
wklog.Log
addr string
}
func NewMonitorServer(s *Server) *MonitorServer {
r := wkhttp.New()
r.Use(wkhttp.CORSMiddleware())
return &MonitorServer{
addr: s.opts.Monitor.Addr,
s: s,
r: r,
Log: wklog.NewWKLog("MonitorServer"),
}
}
func (m *MonitorServer) Start() {
m.r.GetGinRoute().Use(gzip.Gzip(gzip.DefaultCompression))
st, _ := fs.Sub(version.WebFs, "web/dist")
m.r.GetGinRoute().NoRoute(func(c *gin.Context) {
if strings.HasPrefix(c.Request.URL.Path, "/web") {
c.FileFromFS("./index.html", http.FS(st))
return
}
})
m.r.GetGinRoute().StaticFS("/web", http.FS(st))
// 监控api
monitorapi := NewMonitorAPI(m.s)
monitorapi.Route(m.r)
go func() {
err := m.r.Run(m.addr) // listen and serve
if err != nil {
panic(err)
}
}()
m.Info("MonitorServer started", zap.String("addr", m.addr))
_, port := parseAddr(m.addr)
m.Info(fmt.Sprintf("Monitor web address: http://localhost:%d/web", port))
}
func (m *MonitorServer) Stop() error {
return nil
}