From 20269bf94e34b65bb6c714a2380ddfc6f04dd0f8 Mon Sep 17 00:00:00 2001 From: boyska Date: Tue, 25 Dec 2018 01:32:54 +0100 Subject: [PATCH] get status --- cmd/circologd/http_ctl.go | 15 +++++++++++++++ hub.go | 12 +++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/cmd/circologd/http_ctl.go b/cmd/circologd/http_ctl.go index 8915172..26f0c8b 100644 --- a/cmd/circologd/http_ctl.go +++ b/cmd/circologd/http_ctl.go @@ -16,12 +16,27 @@ import ( func setupHTTPCtl(hub circolog.Hub, verbose, debug bool) *mux.Router { m := mux.NewRouter() m.HandleFunc("/pause/toggle", togglePause(hub, verbose, debug)).Methods("POST") + m.HandleFunc("/status", getStatus(hub, verbose, debug)).Methods("GET") m.HandleFunc("/logs/clear", clearQueue(hub, verbose)).Methods("POST") m.HandleFunc("/help", printHelp(verbose)).Methods("GET") m.HandleFunc("/echo", echo(verbose)).Methods("GET") return m } +func getStatus(hub circolog.Hub, verbose, debug bool) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + response := make(chan circolog.CommandResponse) + hub.Commands <- circolog.HubFullCommand{ + Command: circolog.CommandStatus, + Response: response, + } + resp := <-response + w.Header().Set("content-type", "application/json") + enc := json.NewEncoder(w) + enc.Encode(map[string]interface{}{"status": resp.Value}) + } +} + func togglePause(hub circolog.Hub, verbose, debug bool) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if verbose { diff --git a/hub.go b/hub.go index 9305b0a..209f0ab 100644 --- a/hub.go +++ b/hub.go @@ -33,6 +33,7 @@ type HubCommand int const ( CommandClear = iota CommandPauseToggle = iota + CommandStatus = iota ) // An HubFullCommand is a Command, complete with arguments @@ -125,17 +126,22 @@ func (h *Hub) Run() { } } case cmd := <-h.Commands: - if cmd.Command == CommandClear { + switch cmd.Command { + case CommandClear: h.clear() cmd.Response <- CommandResponse{Value: true} - } - if cmd.Command == CommandPauseToggle { + case CommandPauseToggle: togglePause(cmd.Parameters["waitTime"].(time.Duration), &active) if active { fmt.Print("un") } fmt.Println("paused") cmd.Response <- CommandResponse{Value: active} + case CommandStatus: + cmd.Response <- CommandResponse{Value: map[string]interface{}{ + "size": h.circbuf.Len(), + "paused": !active, + }} } } }