diff --git a/main.go b/main.go index c146b9d..dd3122b 100644 --- a/main.go +++ b/main.go @@ -1,23 +1,17 @@ package main import ( - "fmt" "log" "net/http" - - "github.com/gorilla/mux" ) func main() { - router := mux.NewRouter().StrictSlash(true) - router.HandleFunc("/", HandleRoot) + // Create a new router + router := NewRouter() + // Listen and serve the http requests if err := http.ListenAndServe(":8080", router); err != nil { log.Fatal(err) } } - -func HandleRoot(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "Hello World") -} diff --git a/router.go b/router.go new file mode 100644 index 0000000..80461c3 --- /dev/null +++ b/router.go @@ -0,0 +1,32 @@ +package main + +import ( + "encoding/json" + "net/http" + + "github.com/gorilla/mux" +) + +func NewRouter() (router *mux.Router) { + // Create a new router + router = mux.NewRouter().StrictSlash(true) + + // Set up the handlers + router.HandleFunc("/status", HandleStatus) + + return +} + +// RouterStatus is a struct that contains information on the status of the server +type RouterStatus struct { + Ready bool `json:"ready"` +} + +// HandleStatus handles HTTP requests to the /status endpoint +func HandleStatus(w http.ResponseWriter, r *http.Request) { + var status = RouterStatus{ + Ready: true, + } + + json.NewEncoder(w).Encode(status) +} diff --git a/router_test.go b/router_test.go new file mode 100644 index 0000000..86714e3 --- /dev/null +++ b/router_test.go @@ -0,0 +1,22 @@ +package main + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "testing" +) + +func TestHandleStatus(t *testing.T) { + request, _ := http.NewRequest(http.MethodGet, "/status", nil) + response := httptest.NewRecorder() + + HandleStatus(response, request) + + var status RouterStatus + json.NewDecoder(response.Body).Decode(&status) + + if status.Ready != true { + t.Errorf("got false for /status") + } +}