Signed-off-by: Oliver Davies <oliver@oliverdavies.uk>
This commit is contained in:
Oliver Davies 2025-09-21 00:07:50 +01:00
parent 304584fc66
commit a0cb9d8075
3 changed files with 10 additions and 5 deletions

3
go-api/go.mod Normal file
View file

@ -0,0 +1,3 @@
module go-api
go 1.24.4

View file

@ -4,6 +4,8 @@ import (
"encoding/json" "encoding/json"
"log" "log"
"net/http" "net/http"
"go-api/utils"
) )
type ErrorResponse struct { type ErrorResponse struct {
@ -18,7 +20,7 @@ type SuccessResponse struct {
func handler(w http.ResponseWriter, r *http.Request) { func handler(w http.ResponseWriter, r *http.Request) {
log.Println("Running handler...") log.Println("Running handler...")
code := getResponseCode(r) code := utils.GetResponseCode(r)
w.WriteHeader(code) w.WriteHeader(code)
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
@ -38,7 +40,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
func main() { func main() {
http.HandleFunc("/", handler) http.HandleFunc("/", handler)
addr := ":" + getPort() addr := ":" + utils.GetPort()
log.Printf("Starting server on %s", addr) log.Printf("Starting server on %s", addr)

View file

@ -1,4 +1,4 @@
package main package utils
import ( import (
"log" "log"
@ -7,7 +7,7 @@ import (
"strconv" "strconv"
) )
func getPort() string { func GetPort() string {
port := os.Getenv("PORT") port := os.Getenv("PORT")
if port != "" { if port != "" {
@ -17,7 +17,7 @@ func getPort() string {
return "8080" 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 the `force-fail` header is set, get and return its value.
if failCode := r.Header.Get("force-fail"); failCode != "" { if failCode := r.Header.Get("force-fail"); failCode != "" {
log.Println("`force-fail` header set...") log.Println("`force-fail` header set...")