-
Notifications
You must be signed in to change notification settings - Fork 21
/
cloudevents.go
32 lines (29 loc) · 1.12 KB
/
cloudevents.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
package handler
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/silverton-io/honeypot/pkg/annotator"
"github.com/silverton-io/honeypot/pkg/envelope"
"github.com/silverton-io/honeypot/pkg/params"
"github.com/silverton-io/honeypot/pkg/privacy"
"github.com/silverton-io/honeypot/pkg/response"
)
func CloudeventsHandler(h params.Handler) gin.HandlerFunc {
fn := func(c *gin.Context) {
if c.ContentType() == "application/cloudevents+json" || c.ContentType() == "application/cloudevents-batch+json" {
envelopes := envelope.BuildCloudeventEnvelopesFromRequest(c, h.Config, h.CollectorMeta)
annotatedEnvelopes := annotator.Annotate(envelopes, h.Cache)
anonymizedEnvelopes := privacy.AnonymizeEnvelopes(annotatedEnvelopes, h.Config.Privacy)
err := h.Manifold.Distribute(anonymizedEnvelopes, h.ProtocolStats)
if err != nil {
c.Header("Retry-After", response.RETRY_AFTER_60)
c.JSON(http.StatusServiceUnavailable, response.ManifoldDistributionError)
} else {
c.JSON(http.StatusOK, response.Ok)
}
} else {
c.JSON(http.StatusBadRequest, response.InvalidContentType)
}
}
return gin.HandlerFunc(fn)
}