Refactor
Signed-off-by: Oliver Davies <oliver@oliverdavies.uk>
This commit is contained in:
parent
fe620bc9d9
commit
6b5592a10c
1 changed files with 14 additions and 5 deletions
|
@ -7,6 +7,15 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ErrorResponse struct {
|
||||||
|
Message string `json:"message"`
|
||||||
|
StatusCode int `json:"statusCode"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type SuccessResponse struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
}
|
||||||
|
|
||||||
func handler(w http.ResponseWriter, r *http.Request) {
|
func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Println("Running handler...")
|
fmt.Println("Running handler...")
|
||||||
|
|
||||||
|
@ -16,13 +25,13 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
if (code != 200) {
|
if (code != 200) {
|
||||||
json.NewEncoder(w).Encode(map[string]any{
|
json.NewEncoder(w).Encode(ErrorResponse{
|
||||||
"message": "There has been an error.",
|
Message: "There has been an error.",
|
||||||
"statusCode": code,
|
StatusCode: code,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
json.NewEncoder(w).Encode(map[string]string{
|
json.NewEncoder(w).Encode(SuccessResponse{
|
||||||
"name": "Oliver Davies",
|
Name: "Oliver Davies",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue