-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
60 lines (53 loc) · 1.33 KB
/
web.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
package web
import (
"fmt"
"github.com/orvice/monitor-server/internal/sio"
"github.com/weeon/contract"
"github.com/weeon/utils/ginutil"
"net/http"
"github.com/DeanThompson/ginpprof"
"github.com/catpie/cors"
"github.com/gin-gonic/gin"
"github.com/orvice/monitor-server/internal/config"
"github.com/orvice/monitor-server/internal/hub"
"github.com/orvice/monitor-server/internal/mod"
)
var (
nodes []mod.Node
)
func getNodesMap() map[string]mod.Node {
m := make(map[string]mod.Node)
for _, n := range nodes {
m[fmt.Sprintf("%d", n.ID)] = n
}
return m
}
func InitWeb(l contract.Logger) {
r := gin.Default()
r.Use(cors.Default())
r.Use(ginutil.WrapRequestID)
r.GET("/nodes", func(c *gin.Context) {
var err error
if nodes != nil {
c.JSON(http.StatusOK, nodes)
return
}
nodes, err = hub.NodeLoader.GetNodes()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{})
return
}
c.JSON(http.StatusOK, nodes)
return
})
if config.Debug {
ginpprof.Wrapper(r)
}
r.GET("/socket.io/", sio.SocketIOGinHandler)
r.POST("/socket.io/", sio.SocketIOGinHandler)
r.Handle("WS", "/socket.io/", sio.SocketIOGinHandler)
r.Handle("WSS", "/socket.io/", sio.SocketIOGinHandler)
r.POST("/nodes/:node_id", DataCollect)
r.GET("/packet_stat",PacketStat)
r.Run(config.ListenAddr) // listen and serve on 0.0.0.0:8080
}