lab/go-api/utils/utils.go
Oliver Davies 2b310791c5 Change PORT to API_PORT
Signed-off-by: Oliver Davies <oliver@oliverdavies.uk>
2025-09-22 10:46:02 +01:00

33 lines
548 B
Go

package utils
import (
"log"
"net/http"
"os"
"strconv"
)
func GetPort() string {
port := os.Getenv("API_PORT")
if port != "" {
return port
}
return "8080"
}
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...")
if code, err := strconv.Atoi(failCode); err == nil {
log.Printf("Setting the response code to %d...", code)
return code
}
}
return http.StatusOK
}