Refactor
Signed-off-by: Oliver Davies <oliver@oliverdavies.uk>
This commit is contained in:
parent
304584fc66
commit
a0cb9d8075
3 changed files with 10 additions and 5 deletions
3
go-api/go.mod
Normal file
3
go-api/go.mod
Normal file
|
@ -0,0 +1,3 @@
|
|||
module go-api
|
||||
|
||||
go 1.24.4
|
|
@ -4,6 +4,8 @@ import (
|
|||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"go-api/utils"
|
||||
)
|
||||
|
||||
type ErrorResponse struct {
|
||||
|
@ -18,7 +20,7 @@ type SuccessResponse struct {
|
|||
func handler(w http.ResponseWriter, r *http.Request) {
|
||||
log.Println("Running handler...")
|
||||
|
||||
code := getResponseCode(r)
|
||||
code := utils.GetResponseCode(r)
|
||||
|
||||
w.WriteHeader(code)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
@ -38,7 +40,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
|||
func main() {
|
||||
http.HandleFunc("/", handler)
|
||||
|
||||
addr := ":" + getPort()
|
||||
addr := ":" + utils.GetPort()
|
||||
|
||||
log.Printf("Starting server on %s", addr)
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package utils
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
@ -7,7 +7,7 @@ import (
|
|||
"strconv"
|
||||
)
|
||||
|
||||
func getPort() string {
|
||||
func GetPort() string {
|
||||
port := os.Getenv("PORT")
|
||||
|
||||
if port != "" {
|
||||
|
@ -17,7 +17,7 @@ func getPort() string {
|
|||
return "8080"
|
||||
}
|
||||
|
||||
func getResponseCode(r *http.Request) int {
|
||||
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...")
|
Loading…
Add table
Add a link
Reference in a new issue