From 278c611babd0a69d33e8ec4faaacfc969e015074 Mon Sep 17 00:00:00 2001 From: Adrien le Maire Date: Fri, 8 Dec 2023 10:32:20 +0100 Subject: [PATCH] first commit --- .funcignore | 5 +++++ .gitignore | 5 +++++ func.yaml | 4 ++++ go.mod | 3 +++ handle.go | 41 +++++++++++++++++++++++++++++++++++++++++ handle_test.go | 26 ++++++++++++++++++++++++++ 6 files changed, 84 insertions(+) create mode 100644 .funcignore create mode 100644 .gitignore create mode 100644 func.yaml create mode 100644 go.mod create mode 100644 handle.go create mode 100644 handle_test.go diff --git a/.funcignore b/.funcignore new file mode 100644 index 0000000..e8e281c --- /dev/null +++ b/.funcignore @@ -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. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..965f0d4 --- /dev/null +++ b/.gitignore @@ -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 diff --git a/func.yaml b/func.yaml new file mode 100644 index 0000000..0e62e2c --- /dev/null +++ b/func.yaml @@ -0,0 +1,4 @@ +specVersion: 0.35.0 +name: hello +runtime: go +created: 2023-12-07T19:54:11.056629+01:00 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..4b9f90e --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module function + +go 1.14 diff --git a/handle.go b/handle.go new file mode 100644 index 0000000..9883591 --- /dev/null +++ b/handle.go @@ -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() +} diff --git a/handle_test.go b/handle_test.go new file mode 100644 index 0000000..a98b4d8 --- /dev/null +++ b/handle_test.go @@ -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) + } +}