Skip to content
This repository has been archived by the owner on Apr 17, 2024. It is now read-only.

feat: add knative serving and functions #14

Merged
merged 14 commits into from
Dec 20, 2023
Prev Previous commit
Next Next commit
feat: add skeleton for producer and consumer
Signed-off-by: Zuhair AlSader <zuhair@koor.tech>
  • Loading branch information
zalsader committed Dec 13, 2023
commit 00a7eda1d9fc2e195ef7f9c1fdb0e3b8663993f2
5 changes: 5 additions & 0 deletions knative/consumer/.funcignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

# Use the .funcignore file to exclude files which should not be
# tracked in the image build. To instruct the system not to track
# files in the image build, add the regex pattern or file information
# to this file.
5 changes: 5 additions & 0 deletions knative/consumer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

# Functions use the .func directory for local runtime data which should
# generally not be tracked in source control. To instruct the system to track
# .func in source control, comment the following line (prefix it with '# ').
/.func
20 changes: 20 additions & 0 deletions knative/consumer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 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.

## 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 or client
library, and it can be invoked from your browser or from the command line:

```console
curl http://myfunction.example.com/
```

For more, see [the complete documentation]('https://github.com/knative/func/tree/main/docs')


4 changes: 4 additions & 0 deletions knative/consumer/func.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
specVersion: 0.35.0
name: consumer
runtime: go
created: 2023-12-13T00:39:05.888786906-05:00
3 changes: 3 additions & 0 deletions knative/consumer/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module function

go 1.14
41 changes: 41 additions & 0 deletions knative/consumer/handle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package function

import (
"context"
"fmt"
"net/http"
"strings"
)

// Handle an HTTP Request.
func Handle(ctx context.Context, res http.ResponseWriter, req *http.Request) {
/*
* YOUR CODE HERE
*
* Try running `go test`. Add more test as you code in `handle_test.go`.
*/

fmt.Println("Received request")
fmt.Println(prettyPrint(req)) // echo to local output
fmt.Fprintf(res, prettyPrint(req)) // echo to caller
}

func prettyPrint(req *http.Request) string {
b := &strings.Builder{}
fmt.Fprintf(b, "%v %v %v %v\n", req.Method, req.URL, req.Proto, req.Host)
for k, vv := range req.Header {
for _, v := range vv {
fmt.Fprintf(b, " %v: %v\n", k, v)
}
}

if req.Method == "POST" {
req.ParseForm()
fmt.Fprintln(b, "Body:")
for k, v := range req.Form {
fmt.Fprintf(b, " %v: %v\n", k, v)
}
}

return b.String()
}
26 changes: 26 additions & 0 deletions knative/consumer/handle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package function

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

// TestHandle ensures that Handle executes without error and returns the
// HTTP 200 status code indicating no errors.
func TestHandle(t *testing.T) {
var (
w = httptest.NewRecorder()
req = httptest.NewRequest("GET", "http://example.com/test", nil)
res *http.Response
)

Handle(context.Background(), w, req)
res = w.Result()
defer res.Body.Close()

if res.StatusCode != 200 {
t.Fatalf("unexpected response code: %v", res.StatusCode)
}
}
5 changes: 5 additions & 0 deletions knative/producer/.funcignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

# Use the .funcignore file to exclude files which should not be
# tracked in the image build. To instruct the system not to track
# files in the image build, add the regex pattern or file information
# to this file.
5 changes: 5 additions & 0 deletions knative/producer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

# Functions use the .func directory for local runtime data which should
# generally not be tracked in source control. To instruct the system to track
# .func in source control, comment the following line (prefix it with '# ').
/.func
20 changes: 20 additions & 0 deletions knative/producer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 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.

## 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 or client
library, and it can be invoked from your browser or from the command line:

```console
curl http://myfunction.example.com/
```

For more, see [the complete documentation]('https://github.com/knative/func/tree/main/docs')


4 changes: 4 additions & 0 deletions knative/producer/func.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
specVersion: 0.35.0
name: producer
runtime: go
created: 2023-12-13T00:38:54.564292994-05:00
3 changes: 3 additions & 0 deletions knative/producer/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module function

go 1.14
41 changes: 41 additions & 0 deletions knative/producer/handle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package function

import (
"context"
"fmt"
"net/http"
"strings"
)

// Handle an HTTP Request.
func Handle(ctx context.Context, res http.ResponseWriter, req *http.Request) {
/*
* YOUR CODE HERE
*
* Try running `go test`. Add more test as you code in `handle_test.go`.
*/

fmt.Println("Received request")
fmt.Println(prettyPrint(req)) // echo to local output
fmt.Fprintf(res, prettyPrint(req)) // echo to caller
}

func prettyPrint(req *http.Request) string {
b := &strings.Builder{}
fmt.Fprintf(b, "%v %v %v %v\n", req.Method, req.URL, req.Proto, req.Host)
for k, vv := range req.Header {
for _, v := range vv {
fmt.Fprintf(b, " %v: %v\n", k, v)
}
}

if req.Method == "POST" {
req.ParseForm()
fmt.Fprintln(b, "Body:")
for k, v := range req.Form {
fmt.Fprintf(b, " %v: %v\n", k, v)
}
}

return b.String()
}
26 changes: 26 additions & 0 deletions knative/producer/handle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package function

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

// TestHandle ensures that Handle executes without error and returns the
// HTTP 200 status code indicating no errors.
func TestHandle(t *testing.T) {
var (
w = httptest.NewRecorder()
req = httptest.NewRequest("GET", "http://example.com/test", nil)
res *http.Response
)

Handle(context.Background(), w, req)
res = w.Result()
defer res.Body.Close()

if res.StatusCode != 200 {
t.Fatalf("unexpected response code: %v", res.StatusCode)
}
}