/reload

Live reloading for Go servers

Primary LanguageGoBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

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 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.