From 0309eaf9c701787061ce5df6156e9a0d6891844a Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Thu, 21 Nov 2024 14:59:10 -0800 Subject: [PATCH] Sample app --- README.md | 1 - go.mod | 5 +++ go.sum | 2 + main.go | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+), 1 deletion(-) delete mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/README.md b/README.md deleted file mode 100644 index 39802f6..0000000 --- a/README.md +++ /dev/null @@ -1 +0,0 @@ -stub diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ab71f27 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/gopatchy/p-fc-run + +go 1.23.2 + +require github.com/gofrs/uuid v4.4.0+incompatible diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..c0ad687 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA= +github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..854063c --- /dev/null +++ b/main.go @@ -0,0 +1,110 @@ +package main + +import ( + "fmt" + "net/http" + "os" + "strconv" + "strings" + + "github.com/gofrs/uuid" +) + +func logRequest(r *http.Request) { + uri := r.RequestURI + method := r.Method + fmt.Println("Got request!", method, uri) +} + +func main() { + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + logRequest(r) + fmt.Fprintf(w, "Hello! you've requested %s\n", r.URL.Path) + }) + + http.HandleFunc("/cached", func(w http.ResponseWriter, r *http.Request) { + logRequest(r) + maxAgeParams, ok := r.URL.Query()["max-age"] + if ok && len(maxAgeParams) > 0 { + maxAge, _ := strconv.Atoi(maxAgeParams[0]) + w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%d", maxAge)) + } + responseHeaderParams, ok := r.URL.Query()["headers"] + if ok { + for _, header := range responseHeaderParams { + h := strings.Split(header, ":") + w.Header().Set(h[0], strings.TrimSpace(h[1])) + } + } + statusCodeParams, ok := r.URL.Query()["status"] + if ok { + statusCode, _ := strconv.Atoi(statusCodeParams[0]) + w.WriteHeader(statusCode) + } + requestID := uuid.Must(uuid.NewV4()) + fmt.Fprint(w, requestID.String()) + }) + + http.HandleFunc("/headers", func(w http.ResponseWriter, r *http.Request) { + logRequest(r) + keys, ok := r.URL.Query()["key"] + if ok && len(keys) > 0 { + fmt.Fprint(w, r.Header.Get(keys[0])) + return + } + headers := []string{} + headers = append(headers, fmt.Sprintf("host=%s", r.Host)) + for key, values := range r.Header { + headers = append(headers, fmt.Sprintf("%s=%s", key, strings.Join(values, ","))) + } + fmt.Fprint(w, strings.Join(headers, "\n")) + }) + + http.HandleFunc("/env", func(w http.ResponseWriter, r *http.Request) { + logRequest(r) + keys, ok := r.URL.Query()["key"] + if ok && len(keys) > 0 { + fmt.Fprint(w, os.Getenv(keys[0])) + return + } + envs := []string{} + envs = append(envs, os.Environ()...) + fmt.Fprint(w, strings.Join(envs, "\n")) + }) + + http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) { + logRequest(r) + codeParams, ok := r.URL.Query()["code"] + if ok && len(codeParams) > 0 { + statusCode, _ := strconv.Atoi(codeParams[0]) + if statusCode >= 200 && statusCode < 600 { + w.WriteHeader(statusCode) + } + } + requestID := uuid.Must(uuid.NewV4()) + fmt.Fprint(w, requestID.String()) + }) + + port := os.Getenv("PORT") + if port == "" { + port = "80" + } + + for _, encodedRoute := range strings.Split(os.Getenv("ROUTES"), ",") { + if encodedRoute == "" { + continue + } + pathAndBody := strings.SplitN(encodedRoute, "=", 2) + path, body := pathAndBody[0], pathAndBody[1] + http.HandleFunc("/"+path, func(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, body) + }) + } + + bindAddr := fmt.Sprintf(":%s", port) + fmt.Printf("==> Server listening at %s 🚀\n", bindAddr) + + if err := http.ListenAndServe(bindAddr, nil); err != nil { + panic(err) + } +}