lrserver
LiveReload server for Go
Golang package that implements a simple LiveReload server as described in the LiveReload protocol.
Using the recommended default port 35729:
File watching must be implemented by your own application, and reload/alert
requests sent programmatically.
Multiple servers can be instantiated, and each can support multiple connections.
Full Documentation:
Basic Usage
Get Package
go get github.com/jaschaephraim/lrserver
Import Package
import "github.com/jaschaephraim/lrserver"
Instantiate Server
lr := lrserver.New(lrserver.DefaultName, lrserver.DefaultPort)
Start Server
go func() {
err := lr.ListenAndServe()
if err != nil {
}
}()
Send Messages to the Browser
lr.Reload("file")
lr.Alert("message")
Example
import (
"log"
"net/http"
"github.com/fsnotify/fsnotify"
"github.com/jaschaephraim/lrserver"
)
const html = `<!doctype html>
<html>
<head>
<title>Example</title>
</head>
<body>
<script src="http://localhost:35729/livereload.js"></script>
</body>
</html>`
func Example() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatalln(err)
}
defer watcher.Close()
err = watcher.Add("/path/to/watched/dir")
if err != nil {
log.Fatalln(err)
}
lr := lrserver.New(lrserver.DefaultName, lrserver.DefaultPort)
go lr.ListenAndServe()
go func() {
for {
select {
case event := <-watcher.Events:
lr.Reload(event.Name)
case err := <-watcher.Errors:
log.Println(err)
}
}
}()
http.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
rw.Write([]byte(html))
})
http.ListenAndServe(":3000", nil)
}