mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Update json (#463)
* wip * wip * Added more fields to JSON and added blank line between objects. * Update tests. * wip * Updated to represent recent discussions. * Fixed up the json test * More docs * Changed from blank line to bracket, newline, open bracket. * Blank line added back, easier for delimiting.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
@@ -14,8 +15,8 @@ type errorProto struct {
|
||||
error
|
||||
}
|
||||
|
||||
func (e errorProto) IsStreamable() bool { return false }
|
||||
func (e errorProto) Dispatch(io.Writer, *http.Request) error { return e }
|
||||
func (e errorProto) IsStreamable() bool { return false }
|
||||
func (e errorProto) Dispatch(ctx context.Context, ci CallInfo, w io.Writer) error { return e }
|
||||
|
||||
// ContainerIO defines the interface used to talk to a hot function.
|
||||
// Internally, a protocol must know when to alternate between stdin and stdout.
|
||||
@@ -26,7 +27,64 @@ type ContainerIO interface {
|
||||
// Dispatch will handle sending stdin and stdout to a container. Implementers
|
||||
// of Dispatch may format the input and output differently. Dispatch must respect
|
||||
// the req.Context() timeout / cancellation.
|
||||
Dispatch(w io.Writer, req *http.Request) error
|
||||
Dispatch(ctx context.Context, ci CallInfo, w io.Writer) error
|
||||
}
|
||||
|
||||
// CallInfo is passed into dispatch with only the required data the protocols require
|
||||
type CallInfo interface {
|
||||
CallID() string
|
||||
ContentType() string
|
||||
Input() io.Reader
|
||||
|
||||
// ProtocolType let's function/fdk's know what type original request is. Only 'http' for now.
|
||||
// This could be abstracted into separate Protocol objects for each type and all the following information could go in there.
|
||||
// This is a bit confusing because we also have the protocol's for getting information in and out of the function containers.
|
||||
ProtocolType() string
|
||||
Request() *http.Request
|
||||
RequestURL() string
|
||||
Headers() map[string][]string
|
||||
}
|
||||
|
||||
type callInfoImpl struct {
|
||||
call *models.Call
|
||||
req *http.Request
|
||||
}
|
||||
|
||||
func (ci callInfoImpl) CallID() string {
|
||||
return ci.call.ID
|
||||
}
|
||||
|
||||
func (ci callInfoImpl) ContentType() string {
|
||||
return ci.req.Header.Get("Content-Type")
|
||||
}
|
||||
|
||||
// Input returns the call's input/body
|
||||
func (ci callInfoImpl) Input() io.Reader {
|
||||
return ci.req.Body
|
||||
}
|
||||
|
||||
func (ci callInfoImpl) ProtocolType() string {
|
||||
return "http"
|
||||
}
|
||||
|
||||
// Request basically just for the http format, since that's the only that makes sense to have the full request as is
|
||||
func (ci callInfoImpl) Request() *http.Request {
|
||||
return ci.req
|
||||
}
|
||||
func (ci callInfoImpl) RequestURL() string {
|
||||
return ci.req.URL.RequestURI()
|
||||
}
|
||||
|
||||
func (ci callInfoImpl) Headers() map[string][]string {
|
||||
return ci.req.Header
|
||||
}
|
||||
|
||||
func NewCallInfo(call *models.Call, req *http.Request) CallInfo {
|
||||
ci := &callInfoImpl{
|
||||
call: call,
|
||||
req: req,
|
||||
}
|
||||
return ci
|
||||
}
|
||||
|
||||
// Protocol defines all protocols that operates a ContainerIO.
|
||||
|
||||
Reference in New Issue
Block a user