Refactor
Signed-off-by: Oliver Davies <oliver@oliverdavies.uk>
This commit is contained in:
parent
5d3085a311
commit
304584fc66
2 changed files with 34 additions and 28 deletions
|
|
@ -4,8 +4,6 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ErrorResponse struct {
|
type ErrorResponse struct {
|
||||||
|
|
@ -28,7 +26,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
if (code != 200) {
|
if (code != 200) {
|
||||||
json.NewEncoder(w).Encode(ErrorResponse{
|
json.NewEncoder(w).Encode(ErrorResponse{
|
||||||
Message: "There has been an error.",
|
Message: "There has been an error.",
|
||||||
StatusCode: code,
|
StatusCode: 400,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
json.NewEncoder(w).Encode(SuccessResponse{
|
json.NewEncoder(w).Encode(SuccessResponse{
|
||||||
|
|
@ -37,31 +35,6 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 != "" {
|
|
||||||
log.Println("`force-fail` header set...")
|
|
||||||
|
|
||||||
if code, err := strconv.Atoi(failCode); err == nil {
|
|
||||||
log.Printf("Setting the response code to %d...", code)
|
|
||||||
|
|
||||||
return code
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return http.StatusOK
|
|
||||||
}
|
|
||||||
|
|
||||||
func getPort() string {
|
|
||||||
port := os.Getenv("PORT")
|
|
||||||
|
|
||||||
if port != "" {
|
|
||||||
return port
|
|
||||||
}
|
|
||||||
|
|
||||||
return "8080"
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
http.HandleFunc("/", handler)
|
http.HandleFunc("/", handler)
|
||||||
|
|
||||||
|
|
|
||||||
33
go-api/utils.go
Normal file
33
go-api/utils.go
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getPort() string {
|
||||||
|
port := os.Getenv("PORT")
|
||||||
|
|
||||||
|
if port != "" {
|
||||||
|
return port
|
||||||
|
}
|
||||||
|
|
||||||
|
return "8080"
|
||||||
|
}
|
||||||
|
|
||||||
|
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 != "" {
|
||||||
|
log.Println("`force-fail` header set...")
|
||||||
|
|
||||||
|
if code, err := strconv.Atoi(failCode); err == nil {
|
||||||
|
log.Printf("Setting the response code to %d...", code)
|
||||||
|
|
||||||
|
return code
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return http.StatusOK
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue