-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
67 lines (58 loc) · 1.29 KB
/
server.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
package web
import (
"net/http"
"strconv"
"time"
"github.com/gin-gonic/gin"
"github.com/orvice/monitor-server/enum"
"github.com/orvice/monitor-server/internal/config"
"github.com/orvice/monitor-server/internal/hub"
"github.com/orvice/monitor-server/internal/mod"
)
type Ret struct {
Code int `json:"code"`
Message string `json:"message"`
}
func DataCollect(c *gin.Context) {
if c.GetHeader(enum.PostKey) != config.PostKey {
c.JSON(http.StatusUnauthorized, Ret{
Code: http.StatusUnauthorized,
Message: "Wrong Key",
})
return
}
nid := c.Param("node_id")
nodeID, err := strconv.Atoi(nid)
if err != nil {
c.JSON(http.StatusBadRequest, Ret{
Code: http.StatusUnauthorized,
Message: err.Error(),
})
return
}
rawData, err := c.GetRawData()
if err != nil {
c.JSON(http.StatusBadRequest, Ret{
Code: http.StatusUnauthorized,
Message: err.Error(),
})
}
data := mod.Packet{
NodeID: int32(nodeID),
Message: rawData,
}
hub.Manager.SendPacket(data)
c.JSON(http.StatusOK, Ret{})
return
}
func PacketStat(c *gin.Context) {
data := map[string]interface{}{
"time": hub.Manager.GetLastTime(),
}
if hub.Manager.GetLastTime().Before(time.Now().Add(-time.Minute)) {
c.JSON(http.StatusNotAcceptable, data)
return
}
c.JSON(http.StatusOK, data)
return
}