2018-11-11 19:51:21 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2018-12-20 09:45:05 +01:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2018-11-11 19:51:21 +01:00
|
|
|
"net/http"
|
2018-12-20 09:45:05 +01:00
|
|
|
"os"
|
2018-11-11 19:51:21 +01:00
|
|
|
|
2018-12-20 09:45:05 +01:00
|
|
|
fractal "git.lattuga.net/blallo/gotools/formatting"
|
2018-11-11 19:51:21 +01:00
|
|
|
"git.lattuga.net/boyska/circolog"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
2018-12-20 09:45:05 +01:00
|
|
|
func setupHTTPCtl(hub circolog.Hub, verbose bool) *mux.Router {
|
|
|
|
m := mux.NewRouter()
|
|
|
|
m.HandleFunc("/pause/toggle", togglePause(hub, verbose)).Methods("POST")
|
|
|
|
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-11-11 19:51:21 +01:00
|
|
|
}
|
|
|
|
|
2018-12-20 09:45:05 +01:00
|
|
|
func togglePause(hub circolog.Hub, verbose bool) http.HandlerFunc {
|
2018-11-11 19:51:21 +01:00
|
|
|
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-11-11 19:51:21 +01:00
|
|
|
hub.Commands <- circolog.HubFullCommand{Command: circolog.CommandPauseToggle}
|
|
|
|
resp := <-hub.Responses
|
|
|
|
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-11-11 19:58:49 +01:00
|
|
|
hub.Commands <- circolog.HubFullCommand{Command: circolog.CommandClear}
|
|
|
|
resp := <-hub.Responses
|
|
|
|
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!")
|
|
|
|
}
|
|
|
|
}
|