circolog/cmd/circologd/http_ctl.go

120 lines
3.5 KiB
Go
Raw Normal View History

package main
import (
"encoding/json"
2018-12-20 09:45:05 +01:00
"fmt"
"log"
"net/http"
2018-12-20 09:45:05 +01:00
"os"
2018-12-24 18:41:06 +01:00
"time"
2018-12-20 09:45:05 +01:00
fractal "git.lattuga.net/blallo/gotools/formatting"
"git.lattuga.net/boyska/circolog"
"github.com/gorilla/mux"
)
2018-12-24 18:41:06 +01:00
func setupHTTPCtl(hub circolog.Hub, verbose, debug bool) *mux.Router {
2018-12-20 09:45:05 +01:00
m := mux.NewRouter()
2018-12-24 18:41:06 +01:00
m.HandleFunc("/pause/toggle", togglePause(hub, verbose, debug)).Methods("POST")
2018-12-25 01:32:54 +01:00
m.HandleFunc("/status", getStatus(hub, verbose, debug)).Methods("GET")
2018-12-20 09:45:05 +01:00
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
}
2018-12-25 01:32:54 +01:00
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})
}
}
2018-12-24 18:41:06 +01:00
func togglePause(hub circolog.Hub, verbose, debug bool) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
2018-12-20 09:45:05 +01:00
if verbose {
log.Printf("[%s] %s - toggled pause", r.Method, r.RemoteAddr)
}
2018-12-24 18:41:06 +01:00
r.ParseForm()
waitTimePar := r.FormValue("waitTime")
var waitTime time.Duration
var err error
if waitTimePar != "" {
waitTime, err = time.ParseDuration(waitTimePar)
if err != nil {
2018-12-25 01:27:27 +01:00
w.WriteHeader(400)
fmt.Fprintln(w, "waitTime not understood:", waitTimePar)
return
2018-12-24 18:41:06 +01:00
}
}
if debug {
fmt.Println("[DEBUG] waitTime:", waitTime)
}
2018-12-24 15:54:22 +01:00
response := make(chan circolog.CommandResponse)
2018-12-24 18:41:06 +01:00
hub.Commands <- circolog.HubFullCommand{
Command: circolog.CommandPauseToggle,
Response: response,
Parameters: map[string]interface{}{"waitTime": waitTime},
}
2018-12-24 15:54:22 +01:00
resp := <-response
active := resp.Value.(bool)
w.Header().Set("content-type", "application/json")
enc := json.NewEncoder(w)
enc.Encode(map[string]interface{}{"paused": !active})
}
}
2018-11-11 19:58:49 +01:00
2018-12-20 09:45:05 +01:00
func clearQueue(hub circolog.Hub, verbose bool) http.HandlerFunc {
2018-11-11 19:58:49 +01:00
return func(w http.ResponseWriter, r *http.Request) {
2018-12-20 09:45:05 +01:00
if verbose {
log.Printf("[%s] %s - cleared queue", r.Method, r.RemoteAddr)
}
2018-12-24 15:54:22 +01:00
response := make(chan circolog.CommandResponse)
hub.Commands <- circolog.HubFullCommand{Command: circolog.CommandClear, Response: response}
resp := <-response
2018-11-11 19:58:49 +01:00
success := resp.Value.(bool)
w.Header().Set("content-type", "application/json")
enc := json.NewEncoder(w)
enc.Encode(map[string]interface{}{"success": success})
}
}
2018-12-20 09:45:05 +01:00
func printHelp(verbose bool) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if verbose {
log.Printf("[%s] %s - asked for help", r.Method, r.RemoteAddr)
}
w.Header().Set("content-type", "application/json")
enc := json.NewEncoder(w)
var pathsWithDocs = map[string]string{
"/pause/toggle": "Toggle the server from pause state (not listening)",
"/logs/clear": "Wipe the buffer from all the messages",
"/help": "This help",
"/echo": "Answers to heartbeat",
}
fractal.EncodeJSON(pathsWithDocs, enc)
if verbose {
errEnc := json.NewEncoder(os.Stderr)
fractal.EncodeJSON(pathsWithDocs, errEnc)
}
}
}
func echo(verbose bool) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Println("Echo")
if verbose {
log.Printf("[%s] %s - asked for echo", r.Method, r.RemoteAddr)
}
w.Header().Set("content-type", "text/plain")
fmt.Fprintln(w, "I am on!")
}
}