Browse Source

get status

boyska 5 years ago
parent
commit
20269bf94e
2 changed files with 24 additions and 3 deletions
  1. 15 0
      cmd/circologd/http_ctl.go
  2. 9 3
      hub.go

+ 15 - 0
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 {

+ 9 - 3
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,
+				}}
 			}
 		}
 	}