Skip to content

Commit

Permalink
respect event ids
Browse files Browse the repository at this point in the history
  • Loading branch information
smn committed Sep 30, 2015
1 parent 601af1d commit cddd0f6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
10 changes: 6 additions & 4 deletions logdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ func (l LogDriver) Tail(filepath string, offset int64) (t *tail.Tail, err error)
ReOpen: true,
Logger: l.Logger,
}
if offset != 0 {
if offset > 0 {
config.Location = &tail.SeekInfo{offset, os.SEEK_SET}
} else if offset < 0 {
config.Location = &tail.SeekInfo{offset, os.SEEK_END}
} else {
config.Location = &tail.SeekInfo{0, os.SEEK_END}
}
Expand All @@ -78,9 +80,9 @@ func (l LogDriver) StartServer(address string) {
func (l LogDriver) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// get the params from the URL path
params := mux.Vars(r)
n := r.FormValue("n")
n := r.Header.Get("Last-Event-ID")
if n == "" {
n = r.Header.Get("Last-Event-ID")
n = r.FormValue("n")
if n == "" {
n = "0"
}
Expand Down Expand Up @@ -130,7 +132,7 @@ func (l LogDriver) ServeHTTP(w http.ResponseWriter, r *http.Request) {
go func() {
for line := range tail.Lines {
offset, _ := tail.Tell()
fmt.Fprint(w, "event:log\n")
fmt.Fprint(w, "event: log\n")
fmt.Fprintf(w, "id: %d\n", offset)
fmt.Fprintf(w, "data: %s\n", line.Text)
fmt.Fprint(w, "retry: 0\n")
Expand Down
12 changes: 7 additions & 5 deletions logdriver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestTail(t *testing.T) {
filePath := lt.CreateFile("test.txt", "foo\n")

ld := NewLogDriver("test_tail_file", []string{"*"}, tail.DiscardingLogger)
tail, _ := ld.Tail(filePath, 0)
tail, _ := ld.Tail(filePath, -12)

lt.AppendFile("test.txt", "bar\nbaz\n")
done := make(chan bool)
Expand Down Expand Up @@ -133,16 +133,18 @@ func TestServeHTTP(t *testing.T) {

ld := NewLogDriver(lt.path, []string{"*"}, tail.DiscardingLogger)

r, _ := http.NewRequest("GET", "http://localhost:3000/tail/foo.txt", nil)
r, _ := http.NewRequest("GET", "http://localhost:3000/tail/foo.txt?n=-12", nil)
w := NewClosableRecorder()

router := ld.NewRouter()
go router.ServeHTTP(w, r)

lt.AppendFile("foo.txt", "bar\nbaz\n")
// NOTE: I'm doing something awfully wrong here
<-time.After(100 * time.Millisecond)
go lt.AssertRecorderOutput(w, []string{"data: foo", "data: bar", "data: baz"}, w.closer)
go lt.AssertRecorderOutput(w, []string{
"event: log\nid: 4\ndata: foo\nretry: 0",
"event: log\nid: 12\ndata: bar\nretry: 0",
"event: log\nid: 12\ndata: baz\nretry: 0",
}, w.closer)

}

Expand Down

0 comments on commit cddd0f6

Please sign in to comment.