Skip to content

Commit 16335c2

Browse files
committed
initial commit
0 parents  commit 16335c2

File tree

6 files changed

+120
-0
lines changed

6 files changed

+120
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
# Functions use the .func directory for local runtime data which should
3+
# generally not be tracked in source control:
4+
/.func

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Go HTTP Function
2+
3+
Welcome to your new Go Function! The boilerplate function code can be found in
4+
[`handle.go`](handle.go). This Function responds to HTTP requests.
5+
6+
## Development
7+
8+
Develop new features by adding a test to [`handle_test.go`](handle_test.go) for
9+
each feature, and confirm it works with `go test`.
10+
11+
Update the running analog of the function using the `func` CLI or client
12+
library, and it can be invoked from your browser or from the command line:
13+
14+
```console
15+
curl http://myfunction.example.com/
16+
```
17+
18+
For more, see [the complete documentation]('https://github.com/knative/func/tree/main/docs')
19+
20+

func.yaml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
specVersion: 0.34.0
2+
name: hello-func-go
3+
runtime: go
4+
registry: image-registry.openshift-image-registry.svc:5000/viraj
5+
image: image-registry.openshift-image-registry.svc:5000/viraj/hello-func-go:latest
6+
imageDigest: sha256:3375c0d79b40e8ba1197cc02fc3612d4a6cb23e23a8a426700f6c6d1f4e3f7a9
7+
created: 2022-12-06T13:23:09.244792694+05:30
8+
invocation:
9+
format: http
10+
build:
11+
buildpacks:
12+
- paketo-buildpacks/go-dist
13+
- ghcr.io/boson-project/go-function-buildpack:tip
14+
builder: pack
15+
buildEnvs: []
16+
run:
17+
volumes: []
18+
envs: []
19+
deploy:
20+
namespace: viraj
21+
annotations: {}
22+
options: {}
23+
labels: []
24+
healthEndpoints:
25+
liveness: /health/liveness
26+
readiness: /health/readiness

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module function
2+
3+
go 1.14

handle.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package function
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"strings"
8+
)
9+
10+
// Handle an HTTP Request.
11+
func Handle(ctx context.Context, res http.ResponseWriter, req *http.Request) {
12+
/*
13+
* YOUR CODE HERE
14+
*
15+
* Try running `go test`. Add more test as you code in `handle_test.go`.
16+
*/
17+
18+
fmt.Println("Received request")
19+
fmt.Println(prettyPrint(req)) // echo to local output
20+
fmt.Fprintf(res, prettyPrint(req)) // echo to caller
21+
}
22+
23+
func prettyPrint(req *http.Request) string {
24+
b := &strings.Builder{}
25+
fmt.Fprintf(b, "%v %v %v %v\n", req.Method, req.URL, req.Proto, req.Host)
26+
for k, vv := range req.Header {
27+
for _, v := range vv {
28+
fmt.Fprintf(b, " %v: %v\n", k, v)
29+
}
30+
}
31+
32+
if req.Method == "POST" {
33+
req.ParseForm()
34+
fmt.Fprintln(b, "Body:")
35+
for k, v := range req.Form {
36+
fmt.Fprintf(b, " %v: %v\n", k, v)
37+
}
38+
}
39+
40+
return b.String()
41+
}

handle_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package function
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
)
9+
10+
// TestHandle ensures that Handle executes without error and returns the
11+
// HTTP 200 status code indicating no errors.
12+
func TestHandle(t *testing.T) {
13+
var (
14+
w = httptest.NewRecorder()
15+
req = httptest.NewRequest("GET", "http://example.com/test", nil)
16+
res *http.Response
17+
)
18+
19+
Handle(context.Background(), w, req)
20+
res = w.Result()
21+
defer res.Body.Close()
22+
23+
if res.StatusCode != 200 {
24+
t.Fatalf("unexpected response code: %v", res.StatusCode)
25+
}
26+
}

0 commit comments

Comments
 (0)