mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
CloudEvents I/O format support. (#948)
* CloudEvents I/O format support. * Updated format doc. * Remove log lines * This adds support for CloudEvent ingestion at the http router layer. * Updated per comments. * Responds with full CloudEvent message. * Fixed up per comments * Fix tests * Checks for cloudevent content-type * doesn't error on missing content-type.
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -49,13 +50,34 @@ type Param struct {
|
||||
}
|
||||
type Params []Param
|
||||
|
||||
const (
|
||||
ceMimeType = "application/cloudevents+json"
|
||||
)
|
||||
|
||||
func FromRequest(a Agent, app *models.App, path string, req *http.Request) CallOpt {
|
||||
return func(c *call) error {
|
||||
route, err := a.GetRoute(req.Context(), app.ID, path)
|
||||
ctx := req.Context()
|
||||
route, err := a.GetRoute(ctx, app.ID, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log := common.Logger(ctx)
|
||||
// Check whether this is a CloudEvent, if coming in via HTTP router (only way currently), then we'll look for a special header
|
||||
// Content-Type header: https://github.com/cloudevents/spec/blob/master/http-transport-binding.md#32-structured-content-mode
|
||||
// Expected Content-Type for a CloudEvent: application/cloudevents+json; charset=UTF-8
|
||||
contentType := req.Header.Get("Content-Type")
|
||||
t, _, err := mime.ParseMediaType(contentType)
|
||||
if err != nil {
|
||||
// won't fail here, but log
|
||||
log.Debugf("Could not parse Content-Type header: %v", err)
|
||||
} else {
|
||||
if t == ceMimeType {
|
||||
c.IsCloudEvent = true
|
||||
route.Format = models.FormatCloudEvent
|
||||
}
|
||||
}
|
||||
|
||||
if route.Format == "" {
|
||||
route.Format = models.FormatDefault
|
||||
}
|
||||
@@ -258,6 +280,9 @@ func (a *agent) GetCall(opts ...CallOpt) (Call, error) {
|
||||
type call struct {
|
||||
*models.Call
|
||||
|
||||
// IsCloudEvent flag whether this was ingested as a cloud event. This may become the default or only way.
|
||||
IsCloudEvent bool `json:"is_cloud_event"`
|
||||
|
||||
da DataAccess
|
||||
w io.Writer
|
||||
req *http.Request
|
||||
|
||||
Reference in New Issue
Block a user