lrserver

package module
v0.0.0-...-5d77a03 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2015 License: MIT Imports: 10 Imported by: 0

README

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: GoDoc

Basic Usage

Get Package
go get github.com/jaschaephraim/lrserver
Import Package
import "github.com/jaschaephraim/lrserver"
Instantiate Server
lr, err := lrserver.New(lrserver.DefaultName, lrserver.DefaultPort)
Start Server
go func() {
    err = lr.ListenAndServe()
    if err != nil {
        // Handle error
    }
}()
Send Messages to the Browser
lr.Reload("file")
lr.Alert("message")

Example

import (
    "log"
    "net/http"

    "github.com/jaschaephraim/lrserver"
    "gopkg.in/fsnotify.v1"
)

// html includes the client JavaScript
const html = `<!doctype html>
<html>
<head>
  <title>Example</title>
</head>
<body>
  <script src="http://localhost:35729/livereload.js"></script>
</body>
</html>`

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)
}

Documentation

Overview

Package lrserver implements a basic LiveReload server.

(See 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, and:

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.

Example
package main

import (
	"log"
	"net/http"

	"github.com/jaschaephraim/lrserver"
	"gopkg.in/fsnotify.v1"
)

// html includes the client JavaScript
const html = `<!doctype html>
<html>
<head>
  <title>Example</title>
</head>
<body>
  <script src="http://localhost:35729/livereload.js"></script>
</body>
</html>`

func main() {
	// Create file watcher
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		log.Fatalln(err)
	}
	defer watcher.Close()

	// Watch dir
	err = watcher.Add("/path/to/watched/dir")
	if err != nil {
		log.Fatalln(err)
	}

	// Start LiveReload server
	lr, err := lrserver.New(lrserver.DefaultName, lrserver.DefaultPort)
	if err != nil {
		log.Fatalln(err)
	}
	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)
}
Output:

Index

Examples

Constants

View Source
const (
	DefaultName string = "LiveReload"
	DefaultHost string = ""
	DefaultPort uint16 = 35729
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

type Server struct {
	// contains filtered or unexported fields
}

func New

func New(name string, host string, port uint16) (*Server, error)

New ...

func (*Server) Addr

func (s *Server) Addr() string

Addr get the host:port that the server is listening on

func (*Server) Alert

func (s *Server) Alert(msg string)

Alert sends an alert message to the client

func (*Server) ErrorLog

func (s *Server) ErrorLog() *log.Logger

ErrorLog gets the server's error logger, which writes to os.Stderr by default

func (*Server) Host

func (s *Server) Host() string

Host gets the host that the server is listening on

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

func (*Server) LiveCSS

func (s *Server) LiveCSS() bool

LiveCSS gets the live CSS preference

func (*Server) Name

func (s *Server) Name() string

Name gets the server name

func (*Server) Port

func (s *Server) Port() uint16

Port gets the port that the server is listening on

func (*Server) Reload

func (s *Server) Reload(file string)

Reload sends a reload message to the client

func (*Server) SetErrorLog

func (s *Server) SetErrorLog(l *log.Logger)

SetErrorLog sets the server's error logger, which can be set to nil

func (*Server) SetLiveCSS

func (s *Server) SetLiveCSS(n bool)

SetLiveCSS sets the live CSS preference

func (*Server) SetStatusLog

func (s *Server) SetStatusLog(l *log.Logger)

SetStatusLog sets the server's status logger, which can be set to nil

func (*Server) StatusLog

func (s *Server) StatusLog() *log.Logger

StatusLog gets the server's status logger, which writes to os.Stdout by default

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL