Skip to content

Commit

Permalink
temporary remote HTTP response support
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciej Winnicki committed Aug 15, 2017
1 parent 00616de commit ff37ade
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 80 deletions.
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,16 +304,6 @@ Request: arbitrary payload, subscribed function receives an event in above schem

Response: 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)

`POST /` with `Event` header set to `invoke` and `Function-ID` set to function ID.
Expand Down
7 changes: 0 additions & 7 deletions router/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ 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
49 changes: 0 additions & 49 deletions router/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,55 +144,6 @@ func TestIntegration_HTTPSubscription(t *testing.T) {
shutdownGuard.ShutdownAndWait()
}

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, `{"statusCode":201,"headers":{"content-type":"text/html"},"body":"<head></head>"}`)
}))
defer testTargetServer.Close()

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

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

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

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()
}

func wait10Seconds(ch <-chan struct{}, errMsg string) {
select {
case <-ch:
Expand Down
14 changes: 0 additions & 14 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,20 +207,6 @@ 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 {
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 ff37ade

Please sign in to comment.