first commit

This commit is contained in:
Adrien le Maire 2023-12-08 10:32:20 +01:00
parent c09ba1f9b5
commit 278c611bab
6 changed files with 84 additions and 0 deletions

5
.funcignore Normal file
View File

@ -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
.gitignore vendored Normal file
View File

@ -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

4
func.yaml Normal file
View File

@ -0,0 +1,4 @@
specVersion: 0.35.0
name: hello
runtime: go
created: 2023-12-07T19:54:11.056629+01:00

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module function
go 1.14

41
handle.go Normal file
View File

@ -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
handle_test.go Normal file
View File

@ -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)
}
}