From f983f3517bf637590aef245bdadf55ddcfbf2c6d Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Tue, 12 Aug 2014 22:30:47 -0600 Subject: [PATCH 1/2] Skeleton makefile --- Makefile | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 9072141..98d0a4c 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,2 @@ -all: wirc.cgi wirc - -export GOPATH = $(CURDIR) - -%: - go build $@ - -wirc.cgi: - go build $@ +all: + GOPATH=$(CURDIR) go build -v all From e88261c81a5cfb325efe93655890f0b6d656820b Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Tue, 12 Aug 2014 22:59:18 -0600 Subject: [PATCH 2/2] Maybe inotify? --- src/wirc.cgi/wirc.cgi.go | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/wirc.cgi/wirc.cgi.go b/src/wirc.cgi/wirc.cgi.go index b86efed..3504dff 100644 --- a/src/wirc.cgi/wirc.cgi.go +++ b/src/wirc.cgi/wirc.cgi.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "github.com/go-fsnotify/fsnotify" "io/ioutil" "log" "os" @@ -28,24 +29,37 @@ func ReadString(fn string) string { return strings.TrimSpace(string(octets)) } -func tail(w http.ResponseWriter, pos int) { +func tail(w http.ResponseWriter, pos int64) { f, err := os.Open(path.Join(ServerDir, "log")) if err != nil { log.Fatal(err) } defer f.Close() + watcher, err := fsnotify.NewWatcher() + if err != nil { + log.Fatal(err) + } + defer watcher.Close() + for { printid := false - _, err = f.Seek(int64(pos), 0) + newpos, err := f.Seek(pos, 0) if err != nil { log.Fatal(err) } + + if newpos < pos { + // File has been truncated! + pos = 0 + f.Seek(0, 0) + } + bf := bufio.NewScanner(f) for bf.Scan() { t := bf.Text() - pos += len(t) + 1 // XXX: this breaks if we ever see \r\n + pos += int64(len(t)) + 1 // XXX: this breaks if we ever see \r\n fmt.Fprintf(w, "data: %s\n", t) printid = true } @@ -56,7 +70,13 @@ func tail(w http.ResponseWriter, pos int) { break } w.(http.Flusher).Flush() - time.Sleep(350 * time.Millisecond) + + select { + case _ = <-watcher.Events: + // Somethin' happened! + case err := <-watcher.Errors: + log.Fatal(err) + } } } @@ -104,7 +124,7 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { handleCommand(w, r.Form.Get("text"), r.FormValue("target")) default: w.Header().Set("Content-Type", "text/event-stream") - id, _ := strconv.Atoi(os.Getenv("HTTP_LAST_EVENT_ID")) + id, _ := strconv.ParseInt(os.Getenv("HTTP_LAST_EVENT_ID"), 0, 64) tail(w, id) } }