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"
|
||||
)
|
||||
|
||||
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) {
|
||||
fmt.Println("Running handler...")
|
||||
|
||||
|
@ -16,13 +25,13 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
|||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
if (code != 200) {
|
||||
json.NewEncoder(w).Encode(map[string]any{
|
||||
"message": "There has been an error.",
|
||||
"statusCode": code,
|
||||
json.NewEncoder(w).Encode(ErrorResponse{
|
||||
Message: "There has been an error.",
|
||||
StatusCode: code,
|
||||
})
|
||||
} else {
|
||||
json.NewEncoder(w).Encode(map[string]string{
|
||||
"name": "Oliver Davies",
|
||||
json.NewEncoder(w).Encode(SuccessResponse{
|
||||
Name: "Oliver Davies",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue