From a0cb9d8075bcb35d37882699645af6ebfd6fb9ac Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Sun, 21 Sep 2025 00:07:50 +0100 Subject: [PATCH] Refactor Signed-off-by: Oliver Davies --- go-api/go.mod | 3 +++ go-api/main.go | 6 ++++-- go-api/{ => utils}/utils.go | 6 +++--- 3 files changed, 10 insertions(+), 5 deletions(-) create mode 100644 go-api/go.mod rename go-api/{ => utils}/utils.go (84%) diff --git a/go-api/go.mod b/go-api/go.mod new file mode 100644 index 0000000..58337c2 --- /dev/null +++ b/go-api/go.mod @@ -0,0 +1,3 @@ +module go-api + +go 1.24.4 diff --git a/go-api/main.go b/go-api/main.go index 2006408..cd374b7 100644 --- a/go-api/main.go +++ b/go-api/main.go @@ -4,6 +4,8 @@ import ( "encoding/json" "log" "net/http" + + "go-api/utils" ) type ErrorResponse struct { @@ -18,7 +20,7 @@ type SuccessResponse struct { func handler(w http.ResponseWriter, r *http.Request) { log.Println("Running handler...") - code := getResponseCode(r) + code := utils.GetResponseCode(r) w.WriteHeader(code) w.Header().Set("Content-Type", "application/json") @@ -38,7 +40,7 @@ func handler(w http.ResponseWriter, r *http.Request) { func main() { http.HandleFunc("/", handler) - addr := ":" + getPort() + addr := ":" + utils.GetPort() log.Printf("Starting server on %s", addr) diff --git a/go-api/utils.go b/go-api/utils/utils.go similarity index 84% rename from go-api/utils.go rename to go-api/utils/utils.go index 7d88695..bca1150 100644 --- a/go-api/utils.go +++ b/go-api/utils/utils.go @@ -1,4 +1,4 @@ -package main +package utils import ( "log" @@ -7,7 +7,7 @@ import ( "strconv" ) -func getPort() string { +func GetPort() string { port := os.Getenv("PORT") if port != "" { @@ -17,7 +17,7 @@ func getPort() string { return "8080" } -func getResponseCode(r *http.Request) int { +func GetResponseCode(r *http.Request) int { // If the `force-fail` header is set, get and return its value. if failCode := r.Header.Get("force-fail"); failCode != "" { log.Println("`force-fail` header set...")