mirror of https://github.com/nealey/spongy
Possibly parsing LAST_EVENT_ID
This commit is contained in:
parent
c9a90d8ae0
commit
756d397431
|
@ -7,14 +7,17 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const eventIdSep = "/"
|
||||||
|
|
||||||
type Network struct {
|
type Network struct {
|
||||||
running bool
|
running bool
|
||||||
|
|
||||||
name string
|
Name string
|
||||||
currentLog string
|
currentLog string
|
||||||
lineno int64
|
lineno int64
|
||||||
|
|
||||||
|
@ -25,7 +28,7 @@ type Network struct {
|
||||||
func NewNetwork(basePath string) (*Network) {
|
func NewNetwork(basePath string) (*Network) {
|
||||||
return &Network{
|
return &Network{
|
||||||
running: true,
|
running: true,
|
||||||
name: path.Base(basePath),
|
Name: path.Base(basePath),
|
||||||
basePath: basePath,
|
basePath: basePath,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,13 +37,25 @@ func (nw *Network) Close() {
|
||||||
nw.running = false
|
nw.running = false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (nw *Network) LastEventId() string {
|
func (nw *Network) ReadLastEventId(lastEventId string) {
|
||||||
return fmt.Sprintf("%s/%s/%d", nw.name, nw.currentLog, nw.lineno)
|
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) {
|
func (nw *Network) LastEventId() string {
|
||||||
nw.currentLog = filename
|
parts := []string{nw.Name, nw.currentLog, strconv.FormatInt(nw.lineno, 10)}
|
||||||
nw.lineno = lineno
|
return strings.Join(parts, eventIdSep)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (nw *Network) errmsg(err error) []string {
|
func (nw *Network) errmsg(err error) []string {
|
||||||
|
|
Binary file not shown.
|
@ -38,9 +38,11 @@ func (h Handler) handleTail(cfg *Config, w http.ResponseWriter, r *http.Request)
|
||||||
w.Header().Set("Content-Type", "text/event-stream")
|
w.Header().Set("Content-Type", "text/event-stream")
|
||||||
nws := Networks(cfg.BaseDir)
|
nws := Networks(cfg.BaseDir)
|
||||||
|
|
||||||
|
lastEventId := r.FormValue("HTTP_LAST_EVENT_ID")
|
||||||
updates := make(chan []string, 100)
|
updates := make(chan []string, 100)
|
||||||
|
|
||||||
for _, nw := range nws {
|
for _, nw := range nws {
|
||||||
|
nw.ReadLastEventId(lastEventId)
|
||||||
go nw.Tail(updates)
|
go nw.Tail(updates)
|
||||||
defer nw.Close()
|
defer nw.Close()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue