Files
fn-serverless/vendor/github.com/fnproject/fdk-go/utils/cloudevent.go
Reed Allman 51ff7caeb2 Bye bye openapi (#1081)
* add DateTime sans mgo

* change all uses of strfmt.DateTime to common.DateTime, remove test strfmt usage

* remove api tests, system-test dep on api test

multiple reasons to remove the api tests:

* awkward dependency with fn_go meant generating bindings on a branched fn to
vendor those to test new stuff. this is at a minimum not at all intuitive,
worth it, nor a fun way to spend the finite amount of time we have to live.
* api tests only tested a subset of functionality that the server/ api tests
already test, and we risk having tests where one tests some thing and the
other doesn't. let's not. we have too many test suites as it is, and these
pretty much only test that we updated the fn_go bindings, which is actually a
hassle as noted above and the cli will pretty quickly figure out anyway.
* fn_go relies on openapi, which relies on mgo, which is deprecated and we'd
like to remove as a dependency. openapi is a _huge_ dep built in a NIH
fashion, that cannot simply remove the mgo dep as users may be using it.
we've now stolen their date time and otherwise killed usage of it in fn core,
for fn_go it still exists but that's less of a problem.

* update deps

removals:

* easyjson
* mgo
* go-openapi
* mapstructure
* fn_go
* purell
* go-validator

also, had to lock docker. we shouldn't use docker on master anyway, they
strongly advise against that. had no luck with latest version rev, so i locked
it to what we were using before. until next time.

the rest is just playing dep roulette, those end up removing a ton tho

* fix exec test to work

* account for john le cache
2018-06-21 11:09:16 -07:00

118 lines
3.0 KiB
Go

package utils
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strings"
"time"
)
type CloudEvent struct {
CloudEventsVersion string `json:"cloudEventsVersion"`
EventID string `json:"eventID"`
Source string `json:"source"`
EventType string `json:"eventType"`
EventTypeVersion string `json:"eventTypeVersion"`
EventTime time.Time `json:"eventTime"`
SchemaURL string `json:"schemaURL"`
ContentType string `json:"contentType"`
Data interface{} `json:"data"` // from docs: the payload is encoded into a media format which is specified by the contentType attribute (e.g. application/json)
}
type CloudEventInExtension struct {
Protocol CallRequestHTTP `json:"protocol"`
Deadline string `json:"deadline"`
}
type CloudEventOutExtension struct {
Protocol CallResponseHTTP `json:"protocol"`
}
type CloudEventIn struct {
CloudEvent
Extensions CloudEventInExtension `json:"extensions"`
}
type CloudEventOut struct {
CloudEvent
Extensions CloudEventOutExtension `json:"extensions"`
}
func writeError(ceOut *CloudEventOut, err error) {
ceOut.Extensions.Protocol.StatusCode = http.StatusInternalServerError
ceOut.Data = fmt.Sprintf(`{"error": %v}`, err.Error())
ceOut.ContentType = "application/json"
ceOut.EventTime = time.Now()
}
func DoCloudEventOnce(handler Handler, ctx context.Context, in io.Reader, out io.Writer, buf *bytes.Buffer, hdr http.Header) error {
buf.Reset()
ResetHeaders(hdr)
resp := Response{
Writer: buf,
Status: 200,
Header: hdr,
}
ceOut := CloudEventOut{
Extensions: CloudEventOutExtension{
Protocol: CallResponseHTTP{
StatusCode: http.StatusOK,
Headers: hdr,
},
},
CloudEvent: CloudEvent{
ContentType: "text/plain",
},
}
var ceIn CloudEventIn
err := json.NewDecoder(in).Decode(&ceIn)
if err != nil {
if err == io.EOF {
return err
}
writeError(&ceOut, err)
} else {
SetHeaders(ctx, ceIn.Extensions.Protocol.Headers)
SetRequestURL(ctx, ceIn.Extensions.Protocol.RequestURL)
SetMethod(ctx, ceIn.Extensions.Protocol.Method)
ctx, cancel := CtxWithDeadline(ctx, ceIn.Extensions.Deadline)
defer cancel()
if ceIn.ContentType == "application/json" {
// TODO this is lame, need to make FDK cloud event native and not io.Reader
err = json.NewEncoder(buf).Encode(ceIn.Data)
in := strings.NewReader(buf.String()) // string is immutable, we need a copy
buf.Reset()
handler.Serve(ctx, in, &resp)
} else {
handler.Serve(ctx, strings.NewReader(ceIn.Data.(string)), &resp)
}
}
ceOut.EventID = ceIn.EventID
ceOut.EventTime = time.Now()
ceOut.ContentType = ceOut.Extensions.Protocol.Headers.Get("Content-Type")
ceOut.Data = buf.String()
return json.NewEncoder(out).Encode(ceOut)
}
func DoCloudEvent(handler Handler, ctx context.Context, in io.Reader, out io.Writer) {
var buf bytes.Buffer
hdr := make(http.Header)
for {
err := DoCloudEventOnce(handler, ctx, in, out, &buf, hdr)
if err != nil {
log.Println(err.Error())
break
}
}
}