/
http.go
73 lines (59 loc) · 1.94 KB
/
http.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
package spammer
import (
"net/http"
"runtime"
"github.com/labstack/echo/v4"
"github.com/gohornet/hornet/pkg/restapi"
)
const (
// RouteSpammer is the route for controlling the integrated spammer.
RouteSpammer = "/api/plugins/spammer"
// RouteSpammerStatus is the route to get the status of the spammer.
// GET the current status of the spammer.
RouteSpammerStatus = "/status"
// RouteSpammerStart is the route to start the spammer (with optional changing the settings).
// POST the settings to change and start the spammer.
RouteSpammerStart = "/start"
// RouteSpammerStop is the route to stop the spammer.
// POST to stop the spammer.
RouteSpammerStop = "/stop"
)
type spammerStatus struct {
Running bool `json:"running"`
MpsRateLimit float64 `json:"mpsRateLimit"`
CPUMaxUsage float64 `json:"cpuMaxUsage"`
SpammerWorkers int `json:"spammerWorkers"`
SpammerWorkersMax int `json:"spammerWorkersMax"`
}
type startCommand struct {
MpsRateLimit *float64 `json:"mpsRateLimit,omitempty"`
CPUMaxUsage *float64 `json:"cpuMaxUsage,omitempty"`
SpammerWorkers *int `json:"spammerWorkers,omitempty"`
}
func setupRoutes(g *echo.Group) {
g.GET(RouteSpammerStatus, func(c echo.Context) error {
return restapi.JSONResponse(c, http.StatusOK, &spammerStatus{
Running: isRunning,
MpsRateLimit: mpsRateLimitRunning,
CPUMaxUsage: cpuMaxUsageRunning,
SpammerWorkers: spammerWorkersRunning,
SpammerWorkersMax: runtime.NumCPU() - 1,
})
})
g.POST(RouteSpammerStart, func(c echo.Context) error {
cmd := &startCommand{}
if err := c.Bind(&cmd); err != nil {
return err
}
if err := start(cmd.MpsRateLimit, cmd.CPUMaxUsage, cmd.SpammerWorkers); err != nil {
return err
}
return c.JSON(http.StatusAccepted, nil)
})
g.POST(RouteSpammerStop, func(c echo.Context) error {
if err := stop(); err != nil {
return err
}
return c.JSON(http.StatusAccepted, nil)
})
}