From 763ad185a46461932ed39d8a141b47c675e4d044 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Sat, 20 Sep 2025 23:49:24 +0100 Subject: [PATCH] Make the port number configurable Signed-off-by: Oliver Davies --- go-api/main.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/go-api/main.go b/go-api/main.go index 8f34af6..ffc3e3d 100644 --- a/go-api/main.go +++ b/go-api/main.go @@ -3,7 +3,9 @@ package main import ( "encoding/json" "fmt" + "log" "net/http" + "os" "strconv" ) @@ -51,8 +53,24 @@ func getResponseCode(r *http.Request) int { return http.StatusOK } +func getPort() string { + port := os.Getenv("PORT") + + if port != "" { + return port + } + + return "8080" +} + func main() { http.HandleFunc("/", handler) - http.ListenAndServe(":8080", nil) + addr := ":" + getPort() + + fmt.Printf("Starting server on %s", addr) + + if err := http.ListenAndServe(addr, nil); err != nil { + log.Fatal(err) + } }