2025-09-20 23:01:29 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2025-09-20 23:22:30 +01:00
|
|
|
"strconv"
|
2025-09-20 23:01:29 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func handler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Println("Running handler...")
|
|
|
|
|
2025-09-20 23:33:48 +01:00
|
|
|
code := getResponseCode(r)
|
|
|
|
|
|
|
|
w.WriteHeader(code)
|
2025-09-20 23:27:59 +01:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2025-09-20 23:33:48 +01:00
|
|
|
|
|
|
|
if (code != 200) {
|
2025-09-20 23:37:37 +01:00
|
|
|
json.NewEncoder(w).Encode(map[string]any{
|
|
|
|
"message": "There has been an error.",
|
|
|
|
"statusCode": code,
|
|
|
|
})
|
2025-09-20 23:33:48 +01:00
|
|
|
} else {
|
|
|
|
json.NewEncoder(w).Encode(map[string]string{
|
|
|
|
"name": "Oliver Davies",
|
|
|
|
})
|
|
|
|
}
|
2025-09-20 23:27:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func getResponseCode(r *http.Request) int {
|
|
|
|
// If the `force-fail` header is set, get and return its value.
|
2025-09-20 23:22:30 +01:00
|
|
|
if failCode := r.Header.Get("force-fail"); failCode != "" {
|
|
|
|
fmt.Println("`force-fail` header set...")
|
|
|
|
|
|
|
|
if code, err := strconv.Atoi(failCode); err == nil {
|
|
|
|
fmt.Printf("Setting the response code to %d...", code)
|
2025-09-20 23:01:29 +01:00
|
|
|
|
2025-09-20 23:27:59 +01:00
|
|
|
return code
|
2025-09-20 23:22:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-09-20 23:27:59 +01:00
|
|
|
return http.StatusOK
|
2025-09-20 23:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
http.HandleFunc("/", handler)
|
|
|
|
|
|
|
|
http.ListenAndServe(":8080", nil)
|
|
|
|
}
|