Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #55 from vania-pooh/master
Browse files Browse the repository at this point in the history
Showing uptime and last quota reload (fixes #54)
  • Loading branch information
aandryashin authored May 5, 2017
2 parents f7cc335 + fe8628e commit c70822d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ var (
quotaDir string
users string
timeout time.Duration

startTime = time.Now()
lastReloadTime = time.Now()
)

func loadQuotaFiles(quotaDir string) error {
Expand Down Expand Up @@ -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() {
Expand Down
11 changes: 8 additions & 3 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
11 changes: 11 additions & 0 deletions proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit c70822d

Please sign in to comment.