livereload

package module
v0.0.0-...-5434e40 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2023 License: MIT Imports: 8 Imported by: 0

README

livereload

Livereload is a Go package that helps you reload your page in the browser when you code is rerun during development

Usage

Copy js/livereload.js to the folder where your assets are stored then add a script tag for it in

<head>
	<meta charset="UTF-8">
	<meta name="viewport"
		content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">

	<script src="/public/js/livereload.js"></script>
</head>

Next use livereload in your Go project

package main

import (
	"fmt"
	"net/http"

	"github.com/mayowa/livereload"
)

func main() {
	mux := http.NewServeMux()
	fs := http.FileServer(http.Dir("assets/"))
	mux.Handle("/public/", http.StripPrefix("/public/", fs))
	
	livereload.HandleServerMux(mux, nil)
	
	mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
		html := `
		<head>
            <meta charset="UTF-8">
            <meta name="viewport"
                content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        
            <script src="/public/js/livereload.js"></script>
        </head>
		<body>
		  <h1>Hello World</h1>
		</body>
		`
		fmt.Fprintln(w, html)
	})
}

Livereload comes with an adapter for Echo

package main

import (
	"net/http"

	"github.com/labstack/echo/v4"
	lre "github.com/mayowa/livereload/echo"
)

func main() {
	e := echo.New()
	e.Static("/public", "public")

	lre.HandleEcho(e, nil)

	e.GET("/", func(c echo.Context) error {
		html := `
		<head>
            <meta charset="UTF-8">
            <meta name="viewport"
                content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        
            <script src="/public/js/livereload.js"></script>
        </head>
		<body>
		  <h1>Hello World</h1>
		</body>
		`
		return c.HTML(http.StatusOK, html)
	})
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var HandlerPath = "/__livereload__"

Functions

func HandleServerMux

func HandleServerMux(mux *http.ServeMux, options *Options)

func Reload

func Reload()

func ReloadHandler

func ReloadHandler(w http.ResponseWriter, r *http.Request, options *Options) error

Types

type FileInfo

type FileInfo struct {
	Name         string
	LastModified time.Time
	Delay        time.Duration
}

type FileWatcher

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

func NewFileWatcher

func NewFileWatcher(files []*FileInfo) *FileWatcher

func (*FileWatcher) AddFile

func (w *FileWatcher) AddFile(name string, delay time.Duration)

func (*FileWatcher) Run

func (w *FileWatcher) Run(ctx context.Context, outbox chan string)

Run loops forever until it finds a modified file then it sends "reload" to the supplied channel run will exit if the supplied context is done

type Logger

type Logger interface {
	Info(args ...interface{})
	Error(args ...interface{})
}

type Options

type Options struct {
	Logger Logger
	Files  []*FileInfo
}

type ReloadServer

type ReloadServer struct {
	Options *Options
}

func (*ReloadServer) ServeHTTP

func (s *ReloadServer) ServeHTTP(w http.ResponseWriter, r *http.Request)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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