From 432d46d15f9de8d564fd5e84cd775a96a2d98765 Mon Sep 17 00:00:00 2001 From: boyska Date: Wed, 22 Aug 2018 20:56:43 +0200 Subject: [PATCH] small cleanup --- main.go | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/main.go b/main.go index bf3f199..160ea17 100644 --- a/main.go +++ b/main.go @@ -20,13 +20,27 @@ func syslogdHandler(channel syslog.LogPartsChannel) { } } +func httpHandler(w http.ResponseWriter, r *http.Request) { + circbuf.Do(func(x interface{}) { + if x == nil { + return + } + logmsg := x.(format.LogParts) + if logmsg["message"] == nil { + return + } + c := logmsg["message"].(string) + w.Write([]byte(c)) + w.Write([]byte("\n")) + }) +} func main() { syslogSocketPath := flag.String("syslogd-socket", "", "The socket to listen to syslog addresses") // dumpSocketPath := flag.String("dump-socket", "/run/buffer.sock", "The socket that user will connect to in order to receive logs") bufsize := flag.Int("buffer-size", 1000, "Number of messages to keep") flag.Parse() - channel := make(syslog.LogPartsChannel) + channel := make(chan format.LogParts) handler := syslog.NewChannelHandler(channel) server := syslog.NewServer() @@ -34,28 +48,14 @@ func main() { server.SetHandler(handler) if *syslogSocketPath != "" { server.ListenUnixgram(*syslogSocketPath) + } else { + server.ListenUDP("127.0.0.1:9514") } - server.ListenUDP("127.0.0.1:9514") circbuf = ring.New(*bufsize) server.Boot() - go syslogdHandler(channel) - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - circbuf.Do(func(x interface{}) { - if x == nil { - return - } - logmsg := x.(format.LogParts) - if logmsg["message"] == nil { - return - } - c := logmsg["message"].(string) - w.Write([]byte(c)) - w.Write([]byte("\n")) - }) - }) - + http.HandleFunc("/", httpHandler) http.ListenAndServe(":9080", nil) server.Wait()