6f846a0a3c
Consume configurable queues, format signals (including period), share NotifyService with HTTP, and drop duplicate bodies within 1h.
64 lines
1.5 KiB
Go
64 lines
1.5 KiB
Go
package handler
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
|
|
"aiaa-notification-service/internal/model"
|
|
"aiaa-notification-service/internal/notify"
|
|
"aiaa-notification-service/internal/parser"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type NotifyHandler struct {
|
|
svc *notify.Service
|
|
}
|
|
|
|
func NewNotifyHandler(svc *notify.Service) *NotifyHandler {
|
|
return &NotifyHandler{svc: svc}
|
|
}
|
|
|
|
func (h *NotifyHandler) Handle(c *gin.Context) {
|
|
src := c.MustGet("source").(*model.Source)
|
|
|
|
body, err := io.ReadAll(c.Request.Body)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "read body failed"})
|
|
return
|
|
}
|
|
|
|
p, err := parser.NewParser(src.ParseMode, src.ParsePattern)
|
|
if err != nil {
|
|
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": "parser setup: " + err.Error()})
|
|
return
|
|
}
|
|
msg, err := p.Parse(body)
|
|
if err != nil {
|
|
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": "parse failed: " + err.Error()})
|
|
return
|
|
}
|
|
|
|
res, err := h.svc.Process(c.Request.Context(), notify.Request{
|
|
Source: src, Event: msg.Event, Data: msg.Data,
|
|
})
|
|
if err != nil {
|
|
if errors.Is(err, notify.ErrUnprocessable) {
|
|
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
if !res.Matched {
|
|
c.JSON(http.StatusOK, gin.H{"matched": false})
|
|
return
|
|
}
|
|
if res.Filtered {
|
|
c.JSON(http.StatusOK, gin.H{"matched": true, "filtered": true, "reason": res.Reason})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"matched": true, "channels": res.Channels, "accepted": true})
|
|
}
|