mirror of
https://github.com/maputnik/editor.git
synced 2026-06-08 08:17:27 +00:00
Switch filewatch implementation to fsnotify
This commit is contained in:
@@ -3,6 +3,7 @@ os:
|
|||||||
- osx
|
- osx
|
||||||
language: go
|
language: go
|
||||||
install:
|
install:
|
||||||
|
- go get github.com/fsnotify/fsnotify
|
||||||
- go get github.com/gorilla/handlers
|
- go get github.com/gorilla/handlers
|
||||||
- go get github.com/gorilla/mux
|
- go get github.com/gorilla/mux
|
||||||
- go get github.com/gorilla/websocket
|
- go get github.com/gorilla/websocket
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ Install the 3rd party dependencies.
|
|||||||
go get github.com/gorilla/handlers
|
go get github.com/gorilla/handlers
|
||||||
go get github.com/gorilla/mux
|
go get github.com/gorilla/mux
|
||||||
go get github.com/gorilla/websocket
|
go get github.com/gorilla/websocket
|
||||||
|
go get github.com/fsnotify/fsnotify
|
||||||
go get github.com/urfave/cli
|
go get github.com/urfave/cli
|
||||||
go get github.com/elazarl/go-bindata-assetfs/...
|
go get github.com/elazarl/go-bindata-assetfs/...
|
||||||
go get github.com/jteeuwen/go-bindata/...
|
go get github.com/jteeuwen/go-bindata/...
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ install:
|
|||||||
- set PATH=%GOPATH%\bin;c:\go\bin;%PATH%
|
- set PATH=%GOPATH%\bin;c:\go\bin;%PATH%
|
||||||
- go version
|
- go version
|
||||||
- go env
|
- go env
|
||||||
|
- go get github.com/fsnotify/fsnotify
|
||||||
- go get github.com/gorilla/handlers
|
- go get github.com/gorilla/handlers
|
||||||
- go get github.com/gorilla/mux
|
- go get github.com/gorilla/mux
|
||||||
- go get github.com/gorilla/websocket
|
- go get github.com/gorilla/websocket
|
||||||
|
|||||||
+35
-88
@@ -4,103 +4,55 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"strconv"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
|
"github.com/fsnotify/fsnotify"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Adapted from https://github.com/gorilla/websocket
|
|
||||||
// Copyright (c) 2013 The Gorilla WebSocket Authors
|
|
||||||
// https://github.com/gorilla/websocket/blob/master/examples/filewatch/main.go
|
|
||||||
|
|
||||||
const (
|
|
||||||
// Time allowed to write the file to the client.
|
|
||||||
writeWait = 10 * time.Second
|
|
||||||
|
|
||||||
// Time allowed to read the next pong message from the client.
|
|
||||||
pongWait = 60 * time.Second
|
|
||||||
|
|
||||||
// Send pings to client with this period. Must be less than pongWait.
|
|
||||||
pingPeriod = (pongWait * 9) / 10
|
|
||||||
|
|
||||||
// Poll file for changes with this period.
|
|
||||||
filePeriod = 10 * time.Second
|
|
||||||
)
|
|
||||||
|
|
||||||
var upgrader = websocket.Upgrader{
|
var upgrader = websocket.Upgrader{
|
||||||
ReadBufferSize: 1024,
|
ReadBufferSize: 1024,
|
||||||
WriteBufferSize: 1024,
|
WriteBufferSize: 1024,
|
||||||
|
CheckOrigin: func(r *http.Request) bool { return true },
|
||||||
}
|
}
|
||||||
|
|
||||||
func readFileIfModified(filename string, lastMod time.Time) ([]byte, time.Time, error) {
|
func writer(ws *websocket.Conn, filename string) {
|
||||||
fi, err := os.Stat(filename)
|
watcher, err := fsnotify.NewWatcher()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, lastMod, err
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
if !fi.ModTime().After(lastMod) {
|
defer watcher.Close()
|
||||||
return nil, lastMod, nil
|
|
||||||
}
|
|
||||||
p, err := ioutil.ReadFile(filename)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fi.ModTime(), err
|
|
||||||
}
|
|
||||||
return p, fi.ModTime(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func reader(ws *websocket.Conn) {
|
done := make(chan bool)
|
||||||
defer ws.Close()
|
go func() {
|
||||||
ws.SetReadLimit(512)
|
for {
|
||||||
ws.SetReadDeadline(time.Now().Add(pongWait))
|
select {
|
||||||
ws.SetPongHandler(func(string) error { ws.SetReadDeadline(time.Now().Add(pongWait)); return nil })
|
case event := <-watcher.Events:
|
||||||
for {
|
if event.Op&fsnotify.Write == fsnotify.Write {
|
||||||
_, _, err := ws.ReadMessage()
|
log.Println("Modified file:", event.Name)
|
||||||
if err != nil {
|
var p []byte
|
||||||
break
|
var err error
|
||||||
|
|
||||||
|
p, err = ioutil.ReadFile(filename)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if p != nil {
|
||||||
|
if err := ws.WriteMessage(websocket.TextMessage, p); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case err := <-watcher.Errors:
|
||||||
|
log.Println("Watch error:", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func writer(ws *websocket.Conn, filename string, lastMod time.Time) {
|
|
||||||
lastError := ""
|
|
||||||
pingTicker := time.NewTicker(pingPeriod)
|
|
||||||
fileTicker := time.NewTicker(filePeriod)
|
|
||||||
defer func() {
|
|
||||||
pingTicker.Stop()
|
|
||||||
fileTicker.Stop()
|
|
||||||
ws.Close()
|
|
||||||
}()
|
}()
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-fileTicker.C:
|
|
||||||
var p []byte
|
|
||||||
var err error
|
|
||||||
|
|
||||||
p, lastMod, err = readFileIfModified(filename, lastMod)
|
if err = watcher.Add(filename); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
if err != nil {
|
|
||||||
if s := err.Error(); s != lastError {
|
|
||||||
lastError = s
|
|
||||||
p = []byte(lastError)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
lastError = ""
|
|
||||||
}
|
|
||||||
|
|
||||||
if p != nil {
|
|
||||||
ws.SetWriteDeadline(time.Now().Add(writeWait))
|
|
||||||
if err := ws.WriteMessage(websocket.TextMessage, p); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case <-pingTicker.C:
|
|
||||||
ws.SetWriteDeadline(time.Now().Add(writeWait))
|
|
||||||
if err := ws.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
<-done
|
||||||
}
|
}
|
||||||
|
|
||||||
func ServeWebsocketFileWatcher(filename string, w http.ResponseWriter, r *http.Request) {
|
func ServeWebsocketFileWatcher(filename string, w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -112,11 +64,6 @@ func ServeWebsocketFileWatcher(filename string, w http.ResponseWriter, r *http.R
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var lastMod time.Time
|
writer(ws, filename)
|
||||||
if n, err := strconv.ParseInt(r.FormValue("lastMod"), 16, 64); err == nil {
|
defer ws.Close()
|
||||||
lastMod = time.Unix(0, n)
|
|
||||||
}
|
|
||||||
|
|
||||||
go writer(ws, filename, lastMod)
|
|
||||||
reader(ws)
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user