-
Notifications
You must be signed in to change notification settings - Fork 114
/
webapi.go
45 lines (38 loc) · 1.07 KB
/
webapi.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
package spammer
import (
"net/http"
"time"
"github.com/labstack/echo"
)
func handleRequest(c echo.Context) error {
var request Request
if err := c.Bind(&request); err != nil {
return c.JSON(http.StatusBadRequest, Response{Error: err.Error()})
}
switch request.Cmd {
case "start":
if request.MPM == 0 {
request.MPM = 1
}
messageSpammer.Shutdown()
messageSpammer.Start(request.MPM, time.Minute)
log.Infof("Started spamming messages with %d MPM", request.MPM)
return c.JSON(http.StatusOK, Response{Message: "started spamming messages"})
case "stop":
messageSpammer.Shutdown()
log.Info("Stopped spamming messages")
return c.JSON(http.StatusOK, Response{Message: "stopped spamming messages"})
default:
return c.JSON(http.StatusBadRequest, Response{Error: "invalid cmd in request"})
}
}
// Response is the HTTP response of a spammer request.
type Response struct {
Message string `json:"message"`
Error string `json:"error"`
}
// Request contains the parameters of a spammer request.
type Request struct {
Cmd string `json:"cmd"`
MPM int `json:"mpm"`
}