Skip to content

Commit

Permalink
Add example function.
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinHeinz committed Nov 18, 2019
1 parent 9202f22 commit 01b6a50
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
Empty file removed functions/.gitkeep
Empty file.
10 changes: 10 additions & 0 deletions functions/first-func.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 1.0
provider:
name: openfaas
gateway: http://127.0.0.1:31112
functions:
first-func:
lang: golang-mod
handler: ./first-func
image: martinheinz/first-func:latest

20 changes: 20 additions & 0 deletions functions/first-func/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package function

import (
"fmt"
"net/http"

"github.com/openfaas-incubator/go-function-sdk"
)

// Handle a function invocation
func Handle(req handler.Request) (handler.Response, error) {
var err error

message := fmt.Sprintf("Hello world, input was: %s", string(req.Body))

return handler.Response{
Body: []byte(message),
StatusCode: http.StatusOK,
}, err
}
17 changes: 17 additions & 0 deletions functions/first-func/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package function

import (
"github.com/openfaas-incubator/go-function-sdk"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)

func TestHandleReturnsCorrectResponse(t *testing.T) {
expected := handler.Response{Body: []byte("Hello world, input was: John"), StatusCode: http.StatusOK}
response, err := Handle(handler.Request{Body: []byte("John"), Method: "GET"})

assert.Nil(t, err)
assert.Equal(t, response.StatusCode, expected.StatusCode)
assert.Equal(t, response.Body, expected.Body)
}

0 comments on commit 01b6a50

Please sign in to comment.