Skip to content

Commit

Permalink
add support for HTTP response object. Closes #245 (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
mthenw committed Aug 24, 2017
1 parent 5f91158 commit 3572a3c
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 9 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,16 @@ arbitrary payload, subscribed function receives an event in above schema. `data`
Status code:
- `200 OK` with payload with function response

#### Respond to an HTTP Event

To respond to an HTTP event a function needs to return object with following fields:

- `statusCode` - `int` - response status code, default: 200
- `headers` - `object` - response headers
- `body` - `string` - response body

Currently, the event gateway supports only string responses.

### Invoking a Registered Function - Sync Function Invocation

**Endpoint**
Expand Down
20 changes: 20 additions & 0 deletions router/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package router

import (
"fmt"
"net/http"
)

// ErrHTTPResponseObjectMalformed occurs when HTTP response object is not valid JSON.
type ErrHTTPResponseObjectMalformed struct {
StatusCode int
}

func (e ErrHTTPResponseObjectMalformed) Error() string {
return fmt.Sprintf("HTTP response object returned by function malformed.")
}

// NewErrHTTPResponseObjectMalformed return ErrHTTPResponseObjectMalformed
func NewErrHTTPResponseObjectMalformed() ErrHTTPResponseObjectMalformed {
return ErrHTTPResponseObjectMalformed{http.StatusInternalServerError}
}
7 changes: 7 additions & 0 deletions router/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ type HTTPEvent struct {
Method string `json:"method"`
}

// HTTPResponse is a response schema returned by subscribed function in case of HTTP event.
type HTTPResponse struct {
StatusCode int `json:"statusCode"`
Headers map[string]string `json:"headers"`
Body string `json:"body"`
}

const (
mimeJSON = "application/json"
mimeOctetStrem = "application/octet-stream"
Expand Down
22 changes: 13 additions & 9 deletions router/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestMain(t *testing.T) {
etcd.Register()
}

func TestIntegration_Subscription(t *testing.T) {
func TestIntegration_AsyncSubscription(t *testing.T) {
logCfg := zap.NewDevelopmentConfig()
logCfg.DisableStacktrace = true
log, _ := logCfg.Build()
Expand Down Expand Up @@ -99,46 +99,50 @@ func TestIntegration_Subscription(t *testing.T) {
shutdownGuard.ShutdownAndWait()
}

func TestIntegration_HTTPSubscription(t *testing.T) {
func TestIntegration_HTTPResponse(t *testing.T) {
logCfg := zap.NewDevelopmentConfig()
logCfg.DisableStacktrace = true
log, _ := logCfg.Build()

kv, shutdownGuard := newTestEtcd()

testAPIServer := newConfigAPIServer(kv, log)
defer testAPIServer.Close()

router, testRouterServer := newTestRouterServer(kv, log)
defer testRouterServer.Close()

testTargetServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "😸")
fmt.Fprintf(w, `{"statusCode":201,"headers":{"content-type":"text/html"},"body":"<head></head>"}`)
}))
defer testTargetServer.Close()

post(testAPIServer.URL+"/v1/functions",
functions.Function{
ID: functions.FunctionID("supersmileyfunction"),
ID: functions.FunctionID("httpresponse"),
Provider: &functions.Provider{
Type: functions.HTTPEndpoint,
URL: testTargetServer.URL,
},
})

post(testAPIServer.URL+"/v1/subscriptions", subscriptions.Subscription{
FunctionID: functions.FunctionID("supersmileyfunction"),
FunctionID: functions.FunctionID("httpresponse"),
Event: "http",
Method: "GET",
Path: "/smilez",
Path: "/httpresponse",
})

select {
case <-router.WaitForEndpoint(subscriptions.NewEndpointID("GET", "/smilez")):
case <-router.WaitForEndpoint(subscriptions.NewEndpointID("GET", "/httpresponse")):
case <-time.After(10 * time.Second):
panic("timed out waiting for endpoint to be configured!")
}

_, _, body := get(testRouterServer.URL + "/smilez")
assert.Equal(t, "😸", body)
statusCode, headers, body := get(testRouterServer.URL + "/httpresponse")
assert.Equal(t, statusCode, 201)
assert.Equal(t, headers.Get("content-type"), "text/html")
assert.Equal(t, body, "<head></head>")

router.Drain()
shutdownGuard.ShutdownAndWait()
Expand Down
19 changes: 19 additions & 0 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,25 @@ func (router *Router) handleSyncEvent(name string, payload []byte, w http.Respon
zap.String("functionId", string(functionID)), zap.String("event", string(payload)),
zap.String("response", string(resp)))

if name == eventHTTP {
httpResponse := &HTTPResponse{StatusCode: http.StatusOK}
err = json.Unmarshal(resp, httpResponse)
if err != nil {
httperr := NewErrHTTPResponseObjectMalformed()
http.Error(w, httperr.Error(), httperr.StatusCode)

router.log.Info(httperr.Error(), zap.String("response", string(resp)))

return
}

for key, value := range httpResponse.Headers {
w.Header().Set(key, value)
}
w.WriteHeader(httpResponse.StatusCode)
resp = []byte(httpResponse.Body)
}

_, err = w.Write(resp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down

0 comments on commit 3572a3c

Please sign in to comment.