diff --git a/main.go b/main.go index ccb0330..2082198 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,9 @@ var ( quotaDir string users string timeout time.Duration + + startTime = time.Now() + lastReloadTime = time.Now() ) func loadQuotaFiles(quotaDir string) error { @@ -55,6 +58,7 @@ func updateQuota(quotaName string, browsers Browsers) { defer confLock.Unlock() quota[quotaName] = browsers routes = appendRoutes(routes, &browsers) + lastReloadTime = time.Now() } func init() { diff --git a/proxy.go b/proxy.go index a45b68e..9df9869 100644 --- a/proxy.go +++ b/proxy.go @@ -273,11 +273,16 @@ func proxy(r *http.Request) { r.URL.Path = errPath } -func ping(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("Ok\n")) +func ping(w http.ResponseWriter, _ *http.Request) { + confLock.RLock() + defer confLock.RUnlock() + json.NewEncoder(w).Encode(struct { + Uptime string `json:"uptime"` + LastReloadTime string `json:"lastReloadTime"` + }{time.Since(startTime).String(), lastReloadTime.String()}) } -func err(w http.ResponseWriter, r *http.Request) { +func err(w http.ResponseWriter, _ *http.Request) { reply(w, errMsg("route not found"), http.StatusNotFound) } diff --git a/proxy_test.go b/proxy_test.go index e270961..beada69 100644 --- a/proxy_test.go +++ b/proxy_test.go @@ -83,6 +83,17 @@ func TestPing(t *testing.T) { AssertThat(t, err, Is{nil}) AssertThat(t, rsp, Code{http.StatusOK}) + AssertThat(t, rsp.Body, Is{Not{nil}}) + + var data map[string]string + bt, readErr := ioutil.ReadAll(rsp.Body) + AssertThat(t, readErr, Is{nil}) + jsonErr := json.Unmarshal(bt, &data) + AssertThat(t, jsonErr, Is{nil}) + _, hasUptime := data["uptime"] + AssertThat(t, hasUptime, Is{true}) + _, hasLastReloadTime := data["lastReloadTime"] + AssertThat(t, hasLastReloadTime, Is{true}) } func TestErr(t *testing.T) {