Documentation ¶
Overview ¶
package livereload handles Server-sent events and injection of the livereload script into upstream HTML pages.
To get started, create a Handler struct, serve it as you wish and call ForwardMessages to trigger reloads.
Example ¶
package main import ( "net/http" "time" "code.pfad.fr/devf/livereload" ) func main() { // Handler wiring livereloadHandler := &livereload.Handler{ // Wire your own http.Handler here (eg http.FileServer) Upstream: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // The Content-Type must contain text/html for the livereload script to be injected w.Header().Set("Content-Type", "text/html; charset=utf-8") w.WriteHeader(200) }), LogRequest: livereload.SingleLineRequestLogger(80, false), LogSlowResponse: livereload.SlowResponseLogger(time.Second), } s := http.Server{ Handler: livereloadHandler, Addr: ":1234", } go s.ListenAndServe() defer s.Close() http.Get("http://localhost:1234/") // a livereload script will be injected in HTML responses // Reload wiring (usually connected to os.Signal or os.Stdin) messages := make(chan string) go func() { messages <- "reload" // trigger a reload to all connected browsers close(messages) }() livereloadHandler.ForwardMessages(messages, 5*time.Millisecond, time.Second) }
Output: 200 GET / 0s
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SingleLineRequestLogger ¶
func SingleLineRequestLogger(lineWidthMax int, withColors bool) func(int, *http.Request, time.Duration)
SingleLineRequestLogger will print a colored line for each request, for instance:
200 GET /style.css 300µs Code Method RequestURI ResponseTime
func SlowResponseLogger ¶
SlowResponseLogger will print a colored line for each request when the headers weren't sent within the timeout, for instance:
slow response (>1s) GET /devf/style.css RedMessage Method RequestURI
Types ¶
type FallbackFS ¶ added in v0.4.0
FallbackFS will try to Open a file from each of the underlying fs, until succesful.
type Handler ¶
type Handler struct { // Required Upstream http.Handler // If set, LogRequest will be called after an upstream request has been handled. LogRequest func(statusCode int, r *http.Request, duration time.Duration) // If set, the channel returned by LogSlowResponse will be closed when the // response headers for the corresponding request are sent. LogSlowResponse func(r *http.Request) chan<- struct{} // contains filtered or unexported fields }
Handler is a wrapper to manage livereloading. Only the Upstream field is required. See SingleLineRequestLogger and SlowResponseLogger for the optional LogRequest and LogSlowResponse fields.
func (*Handler) ForwardMessages ¶
func (h *Handler) ForwardMessages(messages <-chan string, initialRefreshDelay, minRefreshInterval time.Duration)
ForwardMessages forwards the non-empty messages to the webpage, to trigger a reload. This method returns when the message channel is closed.
- if initialRefreshDelay is set (5ms for instance), it will delay the first message (in case of initial message burst).
- if minRefreshInterval is set (1s for instance), it will delay further message forwarding (in case of repeated messages).