Add websocket filewatcher

This commit is contained in:
lukasmartinelli
2016-09-27 21:30:00 +02:00
parent 8b722fc967
commit 0421a7f099
2 changed files with 152 additions and 1 deletions
+30 -1
View File
@@ -6,6 +6,7 @@ import (
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/maputnik/desktop/filewatch"
"github.com/urfave/cli"
)
@@ -14,11 +15,39 @@ func main() {
app.Name = "maputnik"
app.Usage = "Server for integrating Maputnik locally"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "file, f",
Usage: "Allow access to JSON style from web client",
},
cli.BoolFlag{
Name: "watch",
Usage: "Notify web client about JSON style file changes",
},
}
app.Action = func(c *cli.Context) error {
gui := http.FileServer(assetFS())
router := mux.NewRouter().StrictSlash(true)
//router.Path("/api/v1/apps").Methods("GET").HandlerFunc(foo)
// Register websocket to notify we clients about file changes
filename := c.String("file")
if filename != "" {
/*
router.Path("/files").Methods("GET").HandlerFunc(listFiles)
router.Path("/files/{filename}").Methods("PUT").HandlerFunc(saveFile)
*/
if c.Bool("watch") {
router.Path("/ws").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
filewatch.ServeWebsocketFileWatcher(filename, w, r)
})
}
}
router.PathPrefix("/").Handler(http.StripPrefix("/", gui))
loggedRouter := handlers.LoggingHandler(os.Stdout, router)