forked from boyska/circolog
Compare commits
2 commits
8865335515
...
a2303c1e1d
Author | SHA1 | Date | |
---|---|---|---|
a2303c1e1d | |||
ce7e715c2f |
4 changed files with 58 additions and 60 deletions
|
@ -9,13 +9,13 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"time"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var globalOpts struct {
|
var globalOpts struct {
|
||||||
ctlSock string
|
ctlSock string
|
||||||
verbose bool
|
verbose bool
|
||||||
|
debug bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var ctl http.Client
|
var ctl http.Client
|
||||||
|
@ -40,62 +40,24 @@ func init() {
|
||||||
|
|
||||||
//func getCmd(ctlSock string, args []string) error {}
|
//func getCmd(ctlSock string, args []string) error {}
|
||||||
|
|
||||||
func parsePauseOpts(postponeTime string) (string, error) {
|
|
||||||
var waitTime int64
|
|
||||||
var err error
|
|
||||||
L := len(postponeTime)
|
|
||||||
switch unit := postponeTime[L-1]; string(unit) {
|
|
||||||
case "s":
|
|
||||||
waitTime, err = strconv.ParseInt(postponeTime[:L-2], 10, 16)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
case "m":
|
|
||||||
waitTime, err = strconv.ParseInt(postponeTime[:L-2], 10, 16)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
waitTime = waitTime * 60
|
|
||||||
case "h":
|
|
||||||
waitTime, err = strconv.ParseInt(postponeTime[:L-2], 10, 16)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
waitTime = waitTime * 60 * 60
|
|
||||||
case "d":
|
|
||||||
waitTime, err = strconv.ParseInt(postponeTime[:L-2], 10, 16)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
waitTime = waitTime * 60 * 60 * 24
|
|
||||||
case "w":
|
|
||||||
waitTime, err = strconv.ParseInt(postponeTime[:L-2], 10, 16)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
waitTime = waitTime * 60 * 60 * 24 * 7
|
|
||||||
}
|
|
||||||
return string(waitTime), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func pauseCmd(args []string) error {
|
func pauseCmd(args []string) error {
|
||||||
var postBody string
|
var dontChangeAgain time.Duration
|
||||||
var err error
|
|
||||||
flagset := flag.NewFlagSet(args[0], flag.ExitOnError)
|
flagset := flag.NewFlagSet(args[0], flag.ExitOnError)
|
||||||
postponeTime := flagset.String("postpone", "", "How long to wait before untoggling the state")
|
waitTime := flagset.Duration("wait-time", dontChangeAgain, "How long to wait before untoggling the state, defaults to never")
|
||||||
flagset.Parse(args[1:])
|
flagset.Parse(args[1:])
|
||||||
if *postponeTime != "" {
|
postBody := make(map[string][]string)
|
||||||
postBody, err = parsePauseOpts(*postponeTime)
|
if *waitTime != dontChangeAgain {
|
||||||
if err != nil {
|
postBody["waitTime"] = []string{fmt.Sprintf("%s", *waitTime)}
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
if globalOpts.debug {
|
||||||
|
fmt.Println("[DEBUG] postBody:", postBody)
|
||||||
}
|
}
|
||||||
resp, err := ctl.Post("http://unix/pause/toggle", "application/octet-stream", strings.NewReader(postBody))
|
resp, err := ctl.PostForm("http://unix/pause/toggle", postBody)
|
||||||
if globalOpts.verbose {
|
if globalOpts.verbose {
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
bodyBytes, err2 := ioutil.ReadAll(resp.Body)
|
bodyBytes, err := ioutil.ReadAll(resp.Body)
|
||||||
if err2 != nil {
|
if err != nil {
|
||||||
return err2
|
return err
|
||||||
}
|
}
|
||||||
fmt.Println(string(bodyBytes))
|
fmt.Println(string(bodyBytes))
|
||||||
}
|
}
|
||||||
|
@ -151,6 +113,7 @@ func main() {
|
||||||
flag.StringVar(&globalOpts.ctlSock, "ctl-socket", "/tmp/circologd-ctl.sock",
|
flag.StringVar(&globalOpts.ctlSock, "ctl-socket", "/tmp/circologd-ctl.sock",
|
||||||
"Path to a unix domain socket for the control server; leave empty to disable")
|
"Path to a unix domain socket for the control server; leave empty to disable")
|
||||||
flag.BoolVar(&globalOpts.verbose, "verbose", false, "Print more output")
|
flag.BoolVar(&globalOpts.verbose, "verbose", false, "Print more output")
|
||||||
|
flag.BoolVar(&globalOpts.debug, "debug", false, "Print debugging info")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
args := flag.Args()
|
args := flag.Args()
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
|
|
|
@ -6,28 +6,46 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
fractal "git.lattuga.net/blallo/gotools/formatting"
|
fractal "git.lattuga.net/blallo/gotools/formatting"
|
||||||
"git.lattuga.net/boyska/circolog"
|
"git.lattuga.net/boyska/circolog"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
func setupHTTPCtl(hub circolog.Hub, verbose bool) *mux.Router {
|
func setupHTTPCtl(hub circolog.Hub, verbose, debug bool) *mux.Router {
|
||||||
m := mux.NewRouter()
|
m := mux.NewRouter()
|
||||||
m.HandleFunc("/pause/toggle", togglePause(hub, verbose)).Methods("POST")
|
m.HandleFunc("/pause/toggle", togglePause(hub, verbose, debug)).Methods("POST")
|
||||||
m.HandleFunc("/logs/clear", clearQueue(hub, verbose)).Methods("POST")
|
m.HandleFunc("/logs/clear", clearQueue(hub, verbose)).Methods("POST")
|
||||||
m.HandleFunc("/help", printHelp(verbose)).Methods("GET")
|
m.HandleFunc("/help", printHelp(verbose)).Methods("GET")
|
||||||
m.HandleFunc("/echo", echo(verbose)).Methods("GET")
|
m.HandleFunc("/echo", echo(verbose)).Methods("GET")
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
func togglePause(hub circolog.Hub, verbose bool) http.HandlerFunc {
|
func togglePause(hub circolog.Hub, verbose, debug bool) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if verbose {
|
if verbose {
|
||||||
log.Printf("[%s] %s - toggled pause", r.Method, r.RemoteAddr)
|
log.Printf("[%s] %s - toggled pause", r.Method, r.RemoteAddr)
|
||||||
}
|
}
|
||||||
|
r.ParseForm()
|
||||||
|
waitTimePar := r.FormValue("waitTime")
|
||||||
|
var waitTime time.Duration
|
||||||
|
var err error
|
||||||
|
if waitTimePar != "" {
|
||||||
|
waitTime, err = time.ParseDuration(waitTimePar)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("waitTime not understood:", waitTimePar)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if debug {
|
||||||
|
fmt.Println("[DEBUG] waitTime:", waitTime)
|
||||||
|
}
|
||||||
response := make(chan circolog.CommandResponse)
|
response := make(chan circolog.CommandResponse)
|
||||||
hub.Commands <- circolog.HubFullCommand{Command: circolog.CommandPauseToggle, Response: response}
|
hub.Commands <- circolog.HubFullCommand{
|
||||||
|
Command: circolog.CommandPauseToggle,
|
||||||
|
Response: response,
|
||||||
|
Parameters: map[string]interface{}{"waitTime": waitTime},
|
||||||
|
}
|
||||||
resp := <-response
|
resp := <-response
|
||||||
active := resp.Value.(bool)
|
active := resp.Value.(bool)
|
||||||
w.Header().Set("content-type", "application/json")
|
w.Header().Set("content-type", "application/json")
|
||||||
|
|
|
@ -30,6 +30,7 @@ func main() {
|
||||||
querySocket := flag.String("query-socket", "", "Path to a unix domain socket for the HTTP server; recommended for security reasons!")
|
querySocket := flag.String("query-socket", "", "Path to a unix domain socket for the HTTP server; recommended for security reasons!")
|
||||||
ctlSocket := flag.String("ctl-socket", "/tmp/circologd-ctl.sock", "Path to a unix domain socket for the control server; leave empty to disable")
|
ctlSocket := flag.String("ctl-socket", "/tmp/circologd-ctl.sock", "Path to a unix domain socket for the control server; leave empty to disable")
|
||||||
verbose := flag.Bool("verbose", false, "Print more output executing the daemon")
|
verbose := flag.Bool("verbose", false, "Print more output executing the daemon")
|
||||||
|
debug := flag.Bool("debug", false, "Print debugging info executing the daemon")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
interrupt := make(chan os.Signal, 1)
|
interrupt := make(chan os.Signal, 1)
|
||||||
|
@ -86,7 +87,7 @@ func main() {
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
httpCtlServer := http.Server{Handler: setupHTTPCtl(hub, *verbose)}
|
httpCtlServer := http.Server{Handler: setupHTTPCtl(hub, *verbose, *debug)}
|
||||||
if *ctlSocket != "" {
|
if *ctlSocket != "" {
|
||||||
fmt.Printf("Binding address `%s` [http]\n", *ctlSocket)
|
fmt.Printf("Binding address `%s` [http]\n", *ctlSocket)
|
||||||
unixListener, err := net.Listen("unix", *ctlSocket)
|
unixListener, err := net.Listen("unix", *ctlSocket)
|
||||||
|
|
18
hub.go
18
hub.go
|
@ -2,6 +2,8 @@ package circolog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"container/ring"
|
"container/ring"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gopkg.in/mcuadros/go-syslog.v2/format"
|
"gopkg.in/mcuadros/go-syslog.v2/format"
|
||||||
|
@ -36,6 +38,7 @@ const (
|
||||||
// An HubFullCommand is a Command, complete with arguments
|
// An HubFullCommand is a Command, complete with arguments
|
||||||
type HubFullCommand struct {
|
type HubFullCommand struct {
|
||||||
Command HubCommand
|
Command HubCommand
|
||||||
|
Parameters map[string]interface{}
|
||||||
Response chan CommandResponse
|
Response chan CommandResponse
|
||||||
}
|
}
|
||||||
type CommandResponse struct {
|
type CommandResponse struct {
|
||||||
|
@ -127,13 +130,26 @@ func (h *Hub) Run() {
|
||||||
cmd.Response <- CommandResponse{Value: true}
|
cmd.Response <- CommandResponse{Value: true}
|
||||||
}
|
}
|
||||||
if cmd.Command == CommandPauseToggle {
|
if cmd.Command == CommandPauseToggle {
|
||||||
active = !active
|
togglePause(cmd.Parameters["waitTime"].(time.Duration), &active)
|
||||||
cmd.Response <- CommandResponse{Value: active}
|
cmd.Response <- CommandResponse{Value: active}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func togglePause(waitTime time.Duration, status *bool) {
|
||||||
|
var noTime time.Duration
|
||||||
|
if waitTime != noTime {
|
||||||
|
delayedToggle := func() {
|
||||||
|
time.Sleep(waitTime)
|
||||||
|
fmt.Fprintln(os.Stderr, "toggling again")
|
||||||
|
togglePause(noTime, status)
|
||||||
|
}
|
||||||
|
go delayedToggle()
|
||||||
|
}
|
||||||
|
*status = !*status
|
||||||
|
}
|
||||||
|
|
||||||
// Clear removes all elements from the buffer
|
// Clear removes all elements from the buffer
|
||||||
func (h *Hub) clear() {
|
func (h *Hub) clear() {
|
||||||
buf := h.circbuf
|
buf := h.circbuf
|
||||||
|
|
Loading…
Reference in a new issue