yoake/internal/comm/api.go

72 lines
1.8 KiB
Go
Raw Normal View History

2022-11-11 16:15:22 -06:00
package comm
import (
"github.com/eternal-flame-AD/yoake/internal/auth"
"github.com/eternal-flame-AD/yoake/internal/comm/model"
2022-11-13 00:17:42 -06:00
"github.com/eternal-flame-AD/yoake/internal/util"
2022-11-11 16:15:22 -06:00
"github.com/labstack/echo/v4"
)
type CommStatusResponse struct {
Communicators []struct {
Method string
SupportedMIME []string
} `json:"communicators"`
}
2022-11-13 00:17:42 -06:00
func (c *Communicator) RegisterAPIRoute(g *echo.Group) {
2022-11-11 16:15:22 -06:00
send := g.Group("/send", auth.RequireMiddleware(auth.RoleAdmin))
{
send.POST("", func(ctx echo.Context) error {
var msg model.GenericMessage
if err := ctx.Bind(&msg); err != nil {
return err
}
2022-11-19 11:04:03 -06:00
if err := c.SendGenericMessage("", &msg, false); err != nil {
2022-11-11 16:15:22 -06:00
return err
}
2022-11-19 11:04:03 -06:00
ctx.JSON(200, msg)
2022-11-11 16:15:22 -06:00
return nil
})
send.POST("/:method", func(ctx echo.Context) error {
var msg model.GenericMessage
if err := ctx.Bind(&msg); err != nil {
return err
}
2022-11-19 11:04:03 -06:00
if err := c.SendGenericMessage(ctx.Param("method"), &msg, ctx.QueryParam("force") == "1"); err != nil {
2022-11-11 16:15:22 -06:00
return err
}
2022-11-19 11:04:03 -06:00
ctx.JSON(200, msg)
2022-11-11 16:15:22 -06:00
return nil
})
}
g.GET("/status", func(ctx echo.Context) error {
var communicators []struct {
Method string
SupportedMIME []string
}
for _, comm := range c.fallbackCommunicators {
communicators = append(communicators, struct {
Method string
SupportedMIME []string
}{
Method: comm,
2022-11-13 00:17:42 -06:00
SupportedMIME: c.commMethods[comm].SupportedMIME(),
2022-11-11 16:15:22 -06:00
})
}
2022-11-13 00:17:42 -06:00
for key, comm := range c.commMethods {
if !util.Contain(c.fallbackCommunicators, key) {
2022-11-11 16:15:22 -06:00
communicators = append(communicators, struct {
Method string
SupportedMIME []string
}{
Method: key,
SupportedMIME: comm.SupportedMIME(),
})
}
}
return ctx.JSON(200, CommStatusResponse{Communicators: communicators})
})
}