Template
1
0
Fork 0
goth.stack/api/tools.sendsse.go

49 lines
1.1 KiB
Go
Raw Permalink Normal View History

2023-05-18 20:04:55 -06:00
package api
import (
"net/http"
2024-11-03 17:01:48 -06:00
"goth.stack/lib"
2023-05-18 20:04:55 -06:00
"github.com/labstack/echo/v4"
)
2024-10-20 16:16:50 -06:00
// SSEDemoSend godoc
// @Summary Send SSE message
// @Description Sends a message to a specified SSE channel
// @Tags sse,tools
// @Accept json
// @Produce json
// @Param channel query string false "Channel name"
// @Param message query string false "Message to send"
// @Success 200 {object} map[string]string
// @Failure 400 {object} map[string]string
// @Router /tools/sendsse [post]
2024-06-04 15:40:29 -06:00
func SSEDemoSend(c echo.Context) error {
2023-05-18 20:04:55 -06:00
channel := c.QueryParam("channel")
if channel == "" {
channel = "default"
}
// Get message from query parameters, form value, or request body
message := c.QueryParam("message")
if message == "" {
message = c.FormValue("message")
if message == "" {
var body map[string]string
if err := c.Bind(&body); err != nil {
return err
}
message = body["message"]
}
}
if message == "" {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "message parameter is required"})
}
2024-06-04 15:40:29 -06:00
// Send message
2024-06-04 16:07:13 -06:00
lib.SSEServer.SendSSE(channel, message)
2023-05-18 20:04:55 -06:00
return c.JSON(http.StatusOK, map[string]string{"status": "message sent"})
}