https://github.com/jaschaephraim/lrserver
LiveReload server for Go [golang]
https://github.com/jaschaephraim/lrserver
Last synced: 8 months ago
JSON representation
LiveReload server for Go [golang]
- Host: GitHub
- URL: https://github.com/jaschaephraim/lrserver
- Owner: jaschaephraim
- License: mit
- Created: 2014-07-15T05:36:53.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2024-03-06T23:32:15.000Z (over 1 year ago)
- Last Synced: 2025-03-30T21:06:57.410Z (8 months ago)
- Language: JavaScript
- Homepage:
- Size: 104 KB
- Stars: 128
- Watchers: 7
- Forks: 13
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go-plus - lrserver - LiveReload server for Go.  (Utilities / Utility/Miscellaneous)
- awesome-go - lrserver - LiveReload server for Go. (Utilities / Utility/Miscellaneous)
- awesome-go - lrserver - LiveReload server for Go. (Utilities / Utility/Miscellaneous)
- awesome-go - lrserver - LiveReload server for Go. - :arrow_down:18 - :star:70 (Utilities / HTTP Clients)
- awesome-go - lrserver - LiveReload server for Go [golang] - ★ 96 (Utilities)
- awesome-go - lrserver - LiveReload server for Go. (Utilities / Advanced Console UIs)
- fucking-awesome-go - lrserver - LiveReload server for Go. (Utilities / Utility/Miscellaneous)
- awesome-go - lrserver - | - | - | (Utilities / HTTP Clients)
- awesome-go-cn - lrserver
- awesome-go-zh - lrserver
- fucking-awesome-go - :octocat: lrserver - LiveReload server for Go :star: 67 :fork_and_knife: 4 (Utilities / Advanced Console UIs)
- awesome-go-cn - lrserver
- awesome-go - lrserver - LiveReload server for Go. (Utilities / <span id="高级控制台用户界面-advanced-console-uis">高级控制台用户界面 Advanced Console UIs</span>)
- awesome-go-cn - lrserver
- awesome-go - lrserver - LiveReload server for Go. (Utilities / HTTP Clients)
- awesome-go-cn - lrserver
- awesome-go - lrserver - LiveReload server for Go. (Utilities / Utility/Miscellaneous)
- awesome-go-extra - lrserver - 07-15T05:36:53Z|2017-11-29T20:31:22Z| (Utilities / Fail injection)
- awesome-go-with-stars - lrserver - LiveReload server for Go. (Utilities / Utility/Miscellaneous)
- awesome-go - jaschaephraim/lrserver
- awesome-go - lrserver - LiveReload server for Go. (Utilities / Utility/Miscellaneous)
- awesome-go - lrserver - LiveReload server for Go. (Utilities / Utility/Miscellaneous)
- awesome-Char - lrserver - LiveReload server for Go. (Utilities / HTTP Clients)
README
# `lrserver` LiveReload server for Go #
Golang package that implements a simple LiveReload server as described in the [LiveReload protocol](http://feedback.livereload.com/knowledgebase/articles/86174-livereload-protocol).
Using the recommended default port 35729:
- `http://localhost:35729/livereload.js` serves the LiveReload client JavaScript (https://github.com/livereload/livereload-js)
- `ws://localhost:35729/livereload` communicates with the client via web socket.
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: [](http://godoc.org/github.com/jaschaephraim/lrserver) ##
## Basic Usage ##
### Get Package ###
```bash
go get github.com/jaschaephraim/lrserver
```
### Import Package ###
```go
import "github.com/jaschaephraim/lrserver"
```
### Instantiate Server ###
```go
lr := lrserver.New(lrserver.DefaultName, lrserver.DefaultPort)
```
### Start Server ###
```go
go func() {
err := lr.ListenAndServe()
if err != nil {
// Handle error
}
}()
```
### Send Messages to the Browser ###
```go
lr.Reload("file")
lr.Alert("message")
```
## Example ##
```go
import (
"log"
"net/http"
"github.com/fsnotify/fsnotify"
"github.com/jaschaephraim/lrserver"
)
// html includes the client JavaScript
const html = `
Example
`
func Example() {
// Create file watcher
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatalln(err)
}
defer watcher.Close()
// Add dir to watcher
err = watcher.Add("/path/to/watched/dir")
if err != nil {
log.Fatalln(err)
}
// Create and start LiveReload server
lr := lrserver.New(lrserver.DefaultName, lrserver.DefaultPort)
go lr.ListenAndServe()
// Start goroutine that requests reload upon watcher event
go func() {
for {
select {
case event := <-watcher.Events:
lr.Reload(event.Name)
case err := <-watcher.Errors:
log.Println(err)
}
}
}()
// Start serving html
http.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
rw.Write([]byte(html))
})
http.ListenAndServe(":3000", nil)
}
```