Refactor
Signed-off-by: Oliver Davies <oliver@oliverdavies.uk>
This commit is contained in:
parent
df64a01ebe
commit
ffc74d88ed
1 changed files with 9 additions and 7 deletions
|
@ -10,22 +10,24 @@ import (
|
|||
func handler(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println("Running handler...")
|
||||
|
||||
// If the `force-fail` header is set, get its value and use it as the status
|
||||
// code.
|
||||
w.WriteHeader(getResponseCode(r))
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]any{})
|
||||
}
|
||||
|
||||
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 != "" {
|
||||
fmt.Println("`force-fail` header set...")
|
||||
|
||||
if code, err := strconv.Atoi(failCode); err == nil {
|
||||
fmt.Printf("Setting the response code to %d...", code)
|
||||
|
||||
w.WriteHeader(code)
|
||||
return code
|
||||
}
|
||||
} else {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]any{})
|
||||
return http.StatusOK
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue