Git Product home page Git Product logo

lrserver's Introduction

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

lrserver's People

Contributors

jaschaephraim avatar sowiner avatar gregmagolan avatar zhuharev avatar cryptix avatar nochso avatar deankarn avatar

Forkers

sacules

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.