-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
69 lines (57 loc) · 1.32 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
61
62
63
64
65
66
67
68
69
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/moyada/smoke-ping/v2/monitor"
"github.com/moyada/smoke-ping/v2/util"
"io/ioutil"
"net/http"
)
var hostJob = make(map[string]*monitor.WebTask)
func httpServer() {
gin.ForceConsoleColor()
engine := gin.Default()
engine.GET("ping/:host", ping)
engine.GET("pong/:host", pong)
engine.GET("stop", stop)
engine.Run(":7777")
}
func ping(c *gin.Context) {
host := c.Param("host")
if !util.IsValidIpAddress(host) {
c.String(http.StatusBadRequest, "host invalid")
return
}
task := monitor.Task{Host: host, Size: 64, Output: "report", Collector: &monitor.Chart{}}
job := &monitor.WebTask{Task: task}
hostJob[host] = job
go job.Start()
msg := fmt.Sprintf("start monitoring %v", task.Host)
fmt.Println(msg)
c.String(http.StatusOK, msg)
}
func pong(c *gin.Context) {
host := c.Param("host")
if !util.IsValidIpAddress(host) {
c.String(http.StatusBadRequest, "host invalid")
return
}
job, exist := hostJob[host]
if !exist {
c.String(http.StatusNotFound, "%v monitor not found", host)
return
}
output, err := job.Done()
if err != nil {
c.Error(err)
return
}
file, _ := ioutil.ReadFile(output)
c.Writer.WriteString(string(file))
}
func stop(c *gin.Context) {
for _, task := range hostJob {
task.Done()
}
c.String(http.StatusOK, "ok")
}