lrserver

package module
Version: v0.0.0-...-50d19f6 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2017 License: MIT Imports: 11 Imported by: 20

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 := 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 := 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)
}
Output:

Index

Examples

Constants

View Source
const (

	// DefaultName is the livereload Server's default name
	DefaultName string = "LiveReload"

	// DefaultPort is the livereload Server's default server port
	DefaultPort uint16 = 35729
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

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

Server contains a single lrserver instance's data

func New

func New(name string, port uint16) *Server

New creates a new Server instance

func (*Server) Alert

func (s *Server) Alert(msg string)

Alert sends an alert message to the client

func (*Server) Close

func (s *Server) Close() error

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

func (s *Server) Shutdown(ctx context.Context) error

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