Merge branch 'master' of /home/neale/projects/net/wirc

This commit is contained in:
Neale Pickett 2014-08-13 04:57:43 +00:00
commit 2faee3164e
2 changed files with 27 additions and 14 deletions

View File

@ -1,9 +1,2 @@
all: wirc.cgi wirc all:
GOPATH=$(CURDIR) go build -v all
export GOPATH = $(CURDIR)
%:
go build $@
wirc.cgi:
go build $@

View File

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"github.com/go-fsnotify/fsnotify"
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
@ -28,24 +29,37 @@ func ReadString(fn string) string {
return strings.TrimSpace(string(octets)) 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")) f, err := os.Open(path.Join(ServerDir, "log"))
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
defer f.Close() defer f.Close()
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
for { for {
printid := false printid := false
_, err = f.Seek(int64(pos), 0) newpos, err := f.Seek(pos, 0)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
if newpos < pos {
// File has been truncated!
pos = 0
f.Seek(0, 0)
}
bf := bufio.NewScanner(f) bf := bufio.NewScanner(f)
for bf.Scan() { for bf.Scan() {
t := bf.Text() 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) fmt.Fprintf(w, "data: %s\n", t)
printid = true printid = true
} }
@ -56,7 +70,13 @@ func tail(w http.ResponseWriter, pos int) {
break break
} }
w.(http.Flusher).Flush() 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")) handleCommand(w, r.Form.Get("text"), r.FormValue("target"))
default: default:
w.Header().Set("Content-Type", "text/event-stream") 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) tail(w, id)
} }
} }