2017-06-22 23:37:46 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2017-07-26 14:01:09 +02:00
|
|
|
"os"
|
|
|
|
"regexp"
|
2017-06-23 15:32:08 +02:00
|
|
|
|
|
|
|
"github.com/jaschaephraim/lrserver"
|
2017-06-22 23:37:46 +02:00
|
|
|
)
|
|
|
|
|
2017-06-23 15:32:08 +02:00
|
|
|
// liveReloadScriptTag is inserted into the HTML page.
|
2017-06-22 23:37:46 +02:00
|
|
|
var liveReloadScriptTag = []byte(`<script src="http://localhost:35729/livereload.js"></script>`)
|
|
|
|
|
2017-07-26 14:01:09 +02:00
|
|
|
// startLiveReloader starts the Live Reload server as a go routine, and returns immediately
|
|
|
|
func (s *Server) startLiveReloader() error {
|
|
|
|
lr := lrserver.New(lrserver.DefaultName, lrserver.DefaultPort)
|
|
|
|
s.lr = lr
|
|
|
|
lr.SetStatusLog(nil)
|
|
|
|
lr.ErrorLog().SetOutput(outputFilter{os.Stdout})
|
|
|
|
go lr.ListenAndServe() // nolint: errcheck
|
2017-06-23 15:32:08 +02:00
|
|
|
return nil
|
2017-06-22 23:37:46 +02:00
|
|
|
}
|
|
|
|
|
2017-06-22 23:53:46 +02:00
|
|
|
// NewLiveReloadInjector returns a writer that injects the Live Reload JavaScript
|
2017-06-22 23:37:46 +02:00
|
|
|
// into its wrapped content.
|
|
|
|
func NewLiveReloadInjector(w io.Writer) io.Writer {
|
|
|
|
return TagInjector{w, liveReloadScriptTag}
|
|
|
|
}
|
2017-07-26 14:01:09 +02:00
|
|
|
|
|
|
|
// Remove the lines that match the exclusion pattern.
|
|
|
|
// TODO submit an upstream PR to make this unnecessary
|
|
|
|
type outputFilter struct{ w *os.File }
|
|
|
|
|
|
|
|
var excludeRE = regexp.MustCompile(`websocket: close 1006 \(abnormal closure\): unexpected EOF`)
|
|
|
|
|
|
|
|
func (w outputFilter) Write(b []byte) (int, error) {
|
|
|
|
if excludeRE.Match(b) {
|
|
|
|
return len(b), nil
|
|
|
|
}
|
|
|
|
return w.w.Write(b)
|
|
|
|
}
|