reload

package module
v0.0.0-...-0ea1f3b Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2024 License: BSD-3-Clause Imports: 7 Imported by: 0

README

Live Reload for Go

Go Reference

This package provides very simple live reloading for Go web servers. It is intended purely for development, and is not specialized to any web or client framework. The reload is literally an automated push of the refresh button in the browser, nothing more.

To use, serve the reloader from some URL in your web server:

rl := &reload.Reloader{}
mux.Handle("/.magic/live", rl)

Then reference it through a script tag in your HTML:

<!DOCTYPE html>
<html>
  <head>
    <script defer src="/.magic/live"></script>
  </head>
  <body>
    <p>
      This page will refresh when the server changes, or when
      something visits /.magic/reload.
    </p>
  </body>
</html>

Every page that loads the script will reload itself whenever the server restarts, or when the server invokes Reload on the live handler. For example, you can use the latter to make a magic URL that reloads all clients:

mux.HandleFunc("/.magic/reload", func(w http.ResponseWriter, r *http.Request) {
  rl.Reload()
})

Credit

Thank you to Andy Dote and his blog post about hot reloading in Go. I had the exact problem his post solves, and this library is a mild variation on the solution he presents there.

Documentation

Overview

Package reload provides an HTTP handler that implements simple live reloading for Go web servers.

To use, register this handler in your HTTP server, then load it via a <script> tag in your HTML. The page will be reloaded whenever the server restarts, and when explicitly requested by invoking the handler's Reload function.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Reloader

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

Reloader is a live-reloading HTTP handler.

func (*Reloader) Reload

func (rl *Reloader) Reload()

Reload triggers an immediate reload of all clients.

func (*Reloader) ServeHTTP

func (rl *Reloader) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP serves the live reloading script and websocket handler.

Jump to

Keyboard shortcuts

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