Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeykaplin authored Oct 14, 2021
2 parents 69066c9 + 12fc04a commit cf51423
Show file tree
Hide file tree
Showing 15 changed files with 1,009 additions and 0 deletions.
33 changes: 33 additions & 0 deletions applications/knative/function/figlet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Go HTTP Function

Welcome to your new Go Function! The boilerplate function code can be found in
[`handle.go`](handle.go). This Function responds to HTTP requests.

The function returns:
- OK in ASCII art on a GET request
```
curl x.x.x.x:80 -H "Host: figlet.knative-figlet.example.com"
___ . _ __
/ _ \ . | |/ /
| (_) | . | ' <
\___/ . |_|\_\
.
```
- value of the `text` key in ASCII art on a POST request
```
curl x.x.x.x:80 -H "Host: figlet.knative-figlet.example.com" -H "Content-Type: application/json" -d '{"text":"test"}'
_____ . ___ . ___ . _____
|_ _| . | __| . / __| . |_ _|
| | . | _| . \__ \ . | |
|_| . |___| . |___/ . |_|
. . .
```

## Development

Develop new features by adding a test to [`handle_test.go`](handle_test.go) for
each feature, and confirm it works with `go test`.

Update the running analog of the function using the `func` CLI.

For more, see [the complete documentation]('https://github.com/knative-sandbox/kn-plugin-func/tree/main/docs')
19 changes: 19 additions & 0 deletions applications/knative/function/figlet/func.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: figlet
namespace: knative-figlet
runtime: go
image: docker.io/thunderboltsid/figlet:latest
imageDigest: sha256:99d7c274304547296f24b9ac3d97618e2111748dc4efa862b308f9e0a18182d4
builder: paketobuildpacks/builder:base
builders:
base: paketobuildpacks/builder:base
default: paketobuildpacks/builder:base
full: paketobuildpacks/builder:full
buildpacks:
- paketo-buildpacks/go-dist
- ghcr.io/boson-project/go-function-buildpack:tip
healthEndpoints: {}
volumes: []
envs: []
annotations: {}
options: {}
labels: []
5 changes: 5 additions & 0 deletions applications/knative/function/figlet/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module function

go 1.14

require github.com/thunderboltsid/cli-tools-go v0.2.0
13 changes: 13 additions & 0 deletions applications/knative/function/figlet/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/thunderboltsid/cli-tools-go v0.2.0 h1:0D14o1dg4PkHzcW3I3O6zW0tCxLFo4mrPCSg8dYP9vo=
github.com/thunderboltsid/cli-tools-go v0.2.0/go.mod h1:gQy0ehUtS2UVwA/7ulEXwQCVi+j1bVBi0QAcaHUwZoc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
52 changes: 52 additions & 0 deletions applications/knative/function/figlet/handle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package function

import (
"context"
"encoding/json"
"fmt"
"github.com/thunderboltsid/cli-tools-go/figlet"
"io/ioutil"
"net/http"
)

// Handle an HTTP Request.
func Handle(ctx context.Context, res http.ResponseWriter, req *http.Request) {
res.WriteHeader(200)
res.Header().Add("Content-Type", "text/plain")

if req.Method == http.MethodPost {
if req.Body == nil {
http.Error(res, "Please send a request body", 400)
return
}

body, err := ioutil.ReadAll(req.Body)
defer req.Body.Close()
if err != nil {
http.Error(res, fmt.Sprintf("unable to read request body: %v", err), 500)
return
}


type bodyData struct {
Text string `json:"text"`
}
bd := bodyData{}

if err := json.Unmarshal(body, &bd); err != nil {
http.Error(res, fmt.Sprintf("unable to unmarshal request body: %v", err), 500)
return
}

if err := figlet.New(figlet.WithWriter(res), figlet.WithMsg(bd.Text)); err != nil {
http.Error(res, fmt.Sprintf("unable to create a figlet: %v", err), 500)
return
}
return
}

if err := figlet.New(figlet.WithWriter(res), figlet.WithMsg("ok")); err != nil {
http.Error(res, fmt.Sprintf("unable to return OK: %v", err), 500)
return
}
}
59 changes: 59 additions & 0 deletions applications/knative/function/figlet/handle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package function

import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
)

// TestHandle ensures that Handle executes without error, returns 2000 and sets
// and sets a content-type.
func TestHandle_GET(t *testing.T) {
var (
w = httptest.NewRecorder()
req = httptest.NewRequest("GET", "http://example.com/test", nil)
res *http.Response
err error
)

// Invoke the Handler via a standard Go http.Handler
func(w http.ResponseWriter, req *http.Request) {
Handle(context.Background(), w, req)
}(w, req)
res = w.Result()
defer res.Body.Close()

// Assert postconditions
if err != nil {
t.Fatalf("unepected error in Handle: %v", err)
}
if res.StatusCode != 200 {
t.Fatalf("unexpected response code: %v", res.StatusCode)
}
}

func TestHandle_POST(t *testing.T) {
var (
w = httptest.NewRecorder()
req = httptest.NewRequest("POST", "http://example.com/test", strings.NewReader(`{"text":"test"}`))
res *http.Response
err error
)

// Invoke the Handler via a standard Go http.Handler
func(w http.ResponseWriter, req *http.Request) {
Handle(context.Background(), w, req)
}(w, req)
res = w.Result()
defer res.Body.Close()

// Assert postconditions
if err != nil {
t.Fatalf("unepected error in Handle: %v", err)
}
if res.StatusCode != 200 {
t.Fatalf("unexpected response code: %v", res.StatusCode)
}
}
111 changes: 111 additions & 0 deletions applications/knative/sequence/example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
apiVersion: v1
kind: Namespace
metadata:
name: knative-sequence

---

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: event-display
namespace: knative-sequence
spec:
template:
spec:
containers:
- image: gcr.io/knative-releases/knative.dev/eventing/cmd/event_display

---

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: first
namespace: knative-sequence
spec:
template:
spec:
containers:
- image: gcr.io/knative-releases/knative.dev/eventing/cmd/appender
env:
- name: MESSAGE
value: " - Handled by 0"

---

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: second
namespace: knative-sequence
spec:
template:
spec:
containers:
- image: gcr.io/knative-releases/knative.dev/eventing/cmd/appender
env:
- name: MESSAGE
value: " - Handled by 1"

---

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: third
namespace: knative-sequence
spec:
template:
spec:
containers:
- image: gcr.io/knative-releases/knative.dev/eventing/cmd/appender
env:
- name: MESSAGE
value: " - Handled by 2"
---

apiVersion: flows.knative.dev/v1
kind: Sequence
metadata:
name: sequence
namespace: knative-sequence
spec:
channelTemplate:
apiVersion: messaging.knative.dev/v1
kind: InMemoryChannel
steps:
- ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: first
- ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: second
- ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: third
reply:
ref:
kind: Service
apiVersion: serving.knative.dev/v1
name: event-display

---

apiVersion: sources.knative.dev/v1beta2
kind: PingSource
metadata:
name: ping-source
namespace: knative-sequence
spec:
schedule: "*/1 * * * *"
contentType: "application/json"
data: '{"message": "Hello world!"}'
sink:
ref:
apiVersion: flows.knative.dev/v1
kind: Sequence
name: sequence
Loading

0 comments on commit cf51423

Please sign in to comment.