Commit b0cd91c1 authored by vicotor's avatar vicotor

add api to add/remove bee

parent 1fd4fb38
......@@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"log/slog"
"strings"
// "github.com/gofiber/fiber/v2"
// "github.com/gofiber/fiber/v2/middleware/cors"
......@@ -525,3 +526,78 @@ func VerifyLike(c *fiber.Ctx) error {
})
}
type BeeReq struct {
Bee string `json:"bee"`
}
// BeeAdd godoc
// @Summary BeeAdd
// @Description add a bee client.
// @Tags bee
// @Accept json
// @Produce json
// @Param bee body BeeReq true "bee"
// @Success 200 {object} Res
// @Failure 400 {object} Res
// @Failure 500 {object} Res
// @Router /bee/add [post]
func BeeAdd(c *fiber.Ctx) error {
slog.Info(c.Route().Path, "body", string(c.Request().Body()))
req := BeeReq{}
if err := json.Unmarshal(c.Request().Body(), &req); err != nil {
slog.Error("json.Unmarshal(c.Request().Body(), &req)", "err", err.Error())
return c.JSON(Res{
Code: 500,
Msg: err.Error(),
})
}
swarm := swarm.GetSwarm()
bees := strings.Split(req.Bee, ",")
for _, bee := range bees {
swarm.AddClient(bee)
slog.Info("add new bee", "Bee", bee)
}
return c.JSON(Res{
Code: 200,
})
}
// BeeDel godoc
// @Summary BeeDel
// @Description add a bee client.
// @Tags bee
// @Accept json
// @Produce json
// @Param bee body BeeReq true "bee"
// @Success 200 {object} Res
// @Failure 400 {object} Res
// @Failure 500 {object} Res
// @Router /bee/del [post]
func BeeDel(c *fiber.Ctx) error {
slog.Info(c.Route().Path, "body", string(c.Request().Body()))
req := BeeReq{}
if err := json.Unmarshal(c.Request().Body(), &req); err != nil {
slog.Error("json.Unmarshal(c.Request().Body(), &req)", "err", err.Error())
return c.JSON(Res{
Code: 500,
Msg: err.Error(),
})
}
swarm := swarm.GetSwarm()
bees := strings.Split(req.Bee, ",")
for _, bee := range bees {
swarm.RemoveClient(bee)
slog.Info("remove bee", "Bee", bee)
}
return c.JSON(Res{
Code: 200,
})
}
......@@ -87,6 +87,8 @@ func main() {
app.Post("/task/stop", TaskStop)
app.Get("/verify/follower", VerifyFollower)
app.Get("/verify/retweeter", VerifyRetweeter)
app.Post("/bee/add", BeeAdd)
app.Post("/bee/del", BeeDel)
//VerifyLike
app.Get("/verify/like", VerifyLike)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment