add basic circolog-tail client

This commit is contained in:
boyska 2018-10-25 13:35:21 +02:00
parent 369e16d6c3
commit a5999adb8d
2 changed files with 69 additions and 2 deletions

69
cmd/circolog-tail/main.go Normal file
View file

@ -0,0 +1,69 @@
package main
import (
"flag"
"fmt"
"log"
"net/url"
"os"
"os/signal"
"time"
"github.com/gorilla/websocket"
)
func main() {
addr := flag.String("addr", "localhost:9080", "http service address")
flag.Parse()
fmt.Println("vim-go")
u := url.URL{Scheme: "ws", Host: *addr, Path: "/ws"}
log.Printf("connecting to %s", u.String())
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
log.Fatal("dial:", err)
}
defer c.Close()
done := make(chan struct{})
go func() {
defer close(done)
for {
_, message, err := c.ReadMessage()
if err != nil {
log.Println("close:", err)
return
}
fmt.Println(string(message))
}
}()
for {
select {
case <-done:
return
case <-interrupt:
log.Println("interrupt")
// Cleanly close the connection by sending a close message and then waiting (with timeout) for the
// server to close the connection.
err := c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {
log.Println("write close:", err)
return
}
select {
case <-done:
log.Println("Successfully close")
case <-time.After(1 * time.Second):
log.Println("Forced close")
}
return
}
}
}

View file

@ -2,7 +2,6 @@ package circolog
import ( import (
"bytes" "bytes"
"fmt"
"text/template" "text/template"
"time" "time"
@ -34,7 +33,6 @@ func init() {
// FormatSyslog format a message in the typical format used in /var/log/messages // FormatSyslog format a message in the typical format used in /var/log/messages
func FormatSyslog(msg format.LogParts) string { func FormatSyslog(msg format.LogParts) string {
fmt.Printf("%#v\n", msg["timestamp"])
var buf bytes.Buffer var buf bytes.Buffer
syslogTmpl.Execute(&buf, msg) syslogTmpl.Execute(&buf, msg)
return buf.String() return buf.String()