Possibly parsing LAST_EVENT_ID

This commit is contained in:
Neale Pickett 2015-02-22 16:06:22 -07:00
parent c9a90d8ae0
commit 756d397431
3 changed files with 25 additions and 8 deletions

View File

@ -7,14 +7,17 @@ import (
"io/ioutil"
"os"
"path"
"strconv"
"strings"
"time"
)
const eventIdSep = "/"
type Network struct {
running bool
name string
Name string
currentLog string
lineno int64
@ -25,7 +28,7 @@ type Network struct {
func NewNetwork(basePath string) (*Network) {
return &Network{
running: true,
name: path.Base(basePath),
Name: path.Base(basePath),
basePath: basePath,
}
}
@ -34,13 +37,25 @@ func (nw *Network) Close() {
nw.running = false
}
func (nw *Network) LastEventId() string {
return fmt.Sprintf("%s/%s/%d", nw.name, nw.currentLog, nw.lineno)
func (nw *Network) ReadLastEventId(lastEventId string) {
for _, eventId := range strings.Split(lastEventId, " ") {
parts := strings.Split(eventId, eventIdSep)
if len(parts) != 3 {
continue
}
if parts[0] != nw.Name {
continue
}
nw.currentLog = parts[1]
nw.lineno, _ = strconv.ParseInt(parts[2], 10, 64)
return
}
}
func (nw *Network) SetPosition(filename string, lineno int64) {
nw.currentLog = filename
nw.lineno = lineno
func (nw *Network) LastEventId() string {
parts := []string{nw.Name, nw.currentLog, strconv.FormatInt(nw.lineno, 10)}
return strings.Join(parts, eventIdSep)
}
func (nw *Network) errmsg(err error) []string {

Binary file not shown.

View File

@ -38,13 +38,15 @@ func (h Handler) handleTail(cfg *Config, w http.ResponseWriter, r *http.Request)
w.Header().Set("Content-Type", "text/event-stream")
nws := Networks(cfg.BaseDir)
lastEventId := r.FormValue("HTTP_LAST_EVENT_ID")
updates := make(chan []string, 100)
for _, nw := range nws {
nw.ReadLastEventId(lastEventId)
go nw.Tail(updates)
defer nw.Close()
}
for lines := range updates {
for _, line := range lines {
fmt.Fprintf(w, "data: %s\n", line)