2024-02-14 17:16:41 +01:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-02-28 14:26:59 +01:00
|
|
|
"strings"
|
2024-02-19 22:03:06 +01:00
|
|
|
"sync/atomic"
|
2024-02-23 17:37:55 +01:00
|
|
|
"time"
|
2024-02-14 17:16:41 +01:00
|
|
|
|
2024-02-24 16:39:45 +01:00
|
|
|
"github.com/alecthomas/kong"
|
2024-02-16 19:29:43 +01:00
|
|
|
"github.com/facundoolano/jorge/config"
|
|
|
|
"github.com/facundoolano/jorge/site"
|
2024-02-14 17:16:41 +01:00
|
|
|
"github.com/fsnotify/fsnotify"
|
|
|
|
)
|
|
|
|
|
2024-02-24 16:39:45 +01:00
|
|
|
type Serve struct {
|
|
|
|
ProjectDir string `arg:"" name:"path" optional:"" default:"." help:"Path to the website project to serve."`
|
2024-03-02 14:38:53 +01:00
|
|
|
Host string `short:"H" default:"localhost" help:"Host to run the server on."`
|
2024-02-24 16:39:45 +01:00
|
|
|
Port int `short:"p" default:"4001" help:"Port to run the server on."`
|
|
|
|
NoReload bool `help:"Disable live reloading."`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cmd *Serve) Run(ctx *kong.Context) error {
|
|
|
|
config, err := config.LoadDev(cmd.ProjectDir, cmd.Host, cmd.Port, !cmd.NoReload)
|
2024-02-16 16:39:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-14 17:16:41 +01:00
|
|
|
|
2024-02-24 16:39:45 +01:00
|
|
|
if _, err := os.Stat(config.SrcDir); os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf("missing src directory")
|
|
|
|
}
|
|
|
|
|
2024-02-14 17:16:41 +01:00
|
|
|
// watch for changes in src and layouts, and trigger a rebuild
|
2024-03-04 03:19:09 +01:00
|
|
|
watcher, broker, err := runWatcher(config)
|
2024-02-14 17:16:41 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer watcher.Close()
|
|
|
|
|
|
|
|
// serve the target dir with a file server
|
2024-02-19 22:03:06 +01:00
|
|
|
fs := http.FileServer(HTMLFileSystem{http.Dir(config.TargetDir)})
|
2024-03-04 03:19:09 +01:00
|
|
|
http.Handle("/", fs)
|
2024-02-14 17:16:41 +01:00
|
|
|
|
2024-02-19 22:03:06 +01:00
|
|
|
if config.LiveReload {
|
|
|
|
// handle client requests to listen to server-sent events
|
|
|
|
http.Handle("/_events/", makeServerEventsHandler(broker))
|
|
|
|
}
|
|
|
|
|
2024-02-17 21:09:19 +01:00
|
|
|
addr := fmt.Sprintf("%s:%d", config.ServerHost, config.ServerPort)
|
|
|
|
return http.ListenAndServe(addr, nil)
|
2024-02-14 17:16:41 +01:00
|
|
|
}
|
|
|
|
|
2024-02-19 22:03:06 +01:00
|
|
|
// Return an http.HandlerFunc that establishes a server-sent event stream with clients,
|
|
|
|
// subscribes to site rebuild events received through the given event broker
|
|
|
|
// and forwards them to the client.
|
|
|
|
func makeServerEventsHandler(broker *EventBroker) http.HandlerFunc {
|
|
|
|
return func(res http.ResponseWriter, req *http.Request) {
|
|
|
|
res.Header().Set("Content-Type", "text/event-stream")
|
|
|
|
res.Header().Set("Connection", "keep-alive")
|
|
|
|
res.Header().Set("Cache-Control", "no-cache")
|
|
|
|
res.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
|
|
|
|
id, events := broker.subscribe()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-events:
|
|
|
|
// send an event to the connected client.
|
|
|
|
// data\n\n just means send an empty, unnamed event
|
|
|
|
// since we only need to support the single reload operation.
|
2024-02-28 14:26:59 +01:00
|
|
|
fmt.Fprint(res, "retry: 1000\n")
|
2024-02-19 22:03:06 +01:00
|
|
|
fmt.Fprint(res, "data\n\n")
|
|
|
|
res.(http.Flusher).Flush()
|
|
|
|
case <-req.Context().Done():
|
|
|
|
broker.unsubscribe(id)
|
|
|
|
return
|
|
|
|
}
|
2024-02-14 17:16:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-19 22:03:06 +01:00
|
|
|
// Sets up a watcher that will publish changes in the site source files
|
|
|
|
// to the returned event broker.
|
2024-03-04 03:19:09 +01:00
|
|
|
func runWatcher(config *config.Config) (*fsnotify.Watcher, *EventBroker, error) {
|
2024-02-14 17:16:41 +01:00
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
2024-02-19 22:03:06 +01:00
|
|
|
return nil, nil, err
|
2024-02-14 17:16:41 +01:00
|
|
|
}
|
2024-03-04 03:19:09 +01:00
|
|
|
defer watchProjectFiles(watcher, config)
|
2024-02-14 17:16:41 +01:00
|
|
|
|
2024-02-19 22:03:06 +01:00
|
|
|
broker := newEventBroker()
|
|
|
|
|
2024-02-23 17:37:55 +01:00
|
|
|
// the rebuild is handled after some delay to prevent bursts of events to trigger repeated rebuilds
|
|
|
|
// which can cause the browser to refresh while another unfinished build is in progress (refreshing to
|
|
|
|
// a missing file). The initial build is done immediately.
|
|
|
|
rebuildAfter := time.AfterFunc(0, func() {
|
|
|
|
rebuildSite(config, watcher, broker)
|
|
|
|
})
|
|
|
|
|
2024-02-14 17:16:41 +01:00
|
|
|
go func() {
|
2024-03-04 03:19:09 +01:00
|
|
|
for event := range watcher.Events {
|
|
|
|
// chmod events are noisy, ignore them.
|
|
|
|
// Also ignore dot file events, which are usually spurious (e.g .DS_Store, emacs temp files)
|
|
|
|
isDotFile := strings.HasPrefix(filepath.Base(event.Name), ".")
|
|
|
|
if event.Has(fsnotify.Chmod) || isDotFile {
|
|
|
|
continue
|
2024-02-14 17:16:41 +01:00
|
|
|
}
|
2024-03-04 03:19:09 +01:00
|
|
|
|
|
|
|
// Schedule a rebuild to trigger after a delay. If there was another one pending
|
|
|
|
// it will be canceled.
|
|
|
|
fmt.Printf("\nfile %s changed\n", event.Name)
|
|
|
|
rebuildAfter.Stop()
|
|
|
|
rebuildAfter.Reset(100 * time.Millisecond)
|
2024-02-14 17:16:41 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-02-19 22:03:06 +01:00
|
|
|
return watcher, broker, err
|
2024-02-14 17:16:41 +01:00
|
|
|
}
|
|
|
|
|
2024-03-04 03:19:09 +01:00
|
|
|
// Configure the given watcher to notify for changes in the project source files
|
|
|
|
func watchProjectFiles(watcher *fsnotify.Watcher, config *config.Config) error {
|
2024-02-29 23:34:33 +01:00
|
|
|
watcher.Add(config.LayoutsDir)
|
|
|
|
watcher.Add(config.DataDir)
|
|
|
|
watcher.Add(config.IncludesDir)
|
2024-02-14 17:16:41 +01:00
|
|
|
// fsnotify watches all files within a dir, but non recursively
|
|
|
|
// this walks through the src dir and adds watches for each found directory
|
2024-02-29 23:34:33 +01:00
|
|
|
return filepath.WalkDir(config.SrcDir, func(path string, entry fs.DirEntry, err error) error {
|
2024-02-14 17:16:41 +01:00
|
|
|
if entry.IsDir() {
|
|
|
|
watcher.Add(path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2024-02-19 22:03:06 +01:00
|
|
|
|
2024-02-23 17:37:55 +01:00
|
|
|
func rebuildSite(config *config.Config, watcher *fsnotify.Watcher, broker *EventBroker) {
|
|
|
|
fmt.Printf("building site\n")
|
|
|
|
|
|
|
|
// since new nested directories could be triggering this change, and we need to watch those too
|
|
|
|
// and since re-watching files is a noop, I just re-add the entire src everytime there's a change
|
2024-03-04 03:19:09 +01:00
|
|
|
if err := watchProjectFiles(watcher, config); err != nil {
|
2024-02-23 17:37:55 +01:00
|
|
|
fmt.Println("couldn't add watchers:", err)
|
|
|
|
}
|
|
|
|
|
2024-03-04 03:19:09 +01:00
|
|
|
if err := site.Build(*config); err != nil {
|
2024-02-23 17:37:55 +01:00
|
|
|
fmt.Println("build error:", err)
|
|
|
|
return
|
2024-02-19 22:03:06 +01:00
|
|
|
}
|
|
|
|
|
2024-02-23 17:37:55 +01:00
|
|
|
broker.publish("rebuild")
|
|
|
|
|
|
|
|
fmt.Println("done\nserving at", config.SiteUrl)
|
2024-02-19 22:03:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tweaks the http file system to construct a server that hides the .html suffix from requests.
|
|
|
|
// Based on https://stackoverflow.com/a/57281956/993769
|
|
|
|
type HTMLFileSystem struct {
|
2024-03-04 03:19:09 +01:00
|
|
|
dirFS http.Dir
|
2024-02-19 22:03:06 +01:00
|
|
|
}
|
|
|
|
|
2024-03-04 03:19:09 +01:00
|
|
|
func (htmlFS HTMLFileSystem) Open(name string) (http.File, error) {
|
2024-02-19 22:03:06 +01:00
|
|
|
// Try name as supplied
|
2024-03-04 03:19:09 +01:00
|
|
|
f, err := htmlFS.dirFS.Open(name)
|
2024-02-19 22:03:06 +01:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// Not found, try with .html
|
2024-03-04 03:19:09 +01:00
|
|
|
if f, err := htmlFS.dirFS.Open(name + ".html"); err == nil {
|
2024-02-19 22:03:06 +01:00
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return f, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// The event broker allows the file watcher to publish site rebuild events
|
|
|
|
// and register http clients to listen for them, in order to trigger browser refresh
|
|
|
|
// events after the the site has been rebuilt.
|
|
|
|
type EventBroker struct {
|
|
|
|
inEvents chan string
|
|
|
|
inSubscriptions chan Subscription
|
|
|
|
subscribers map[uint64]chan string
|
|
|
|
idgen atomic.Uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
type Subscription struct {
|
|
|
|
id uint64
|
|
|
|
outEvents chan string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newEventBroker() *EventBroker {
|
|
|
|
broker := EventBroker{
|
|
|
|
inEvents: make(chan string),
|
|
|
|
inSubscriptions: make(chan Subscription),
|
|
|
|
subscribers: map[uint64]chan string{},
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <-broker.inSubscriptions:
|
|
|
|
if msg.outEvents != nil {
|
|
|
|
// subscribe
|
|
|
|
broker.subscribers[msg.id] = msg.outEvents
|
|
|
|
} else {
|
|
|
|
// unsubscribe
|
|
|
|
close(broker.subscribers[msg.id])
|
|
|
|
delete(broker.subscribers, msg.id)
|
|
|
|
}
|
|
|
|
case msg := <-broker.inEvents:
|
|
|
|
// send the event to all the subscribers
|
|
|
|
for _, outEvents := range broker.subscribers {
|
|
|
|
outEvents <- msg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return &broker
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds a subscription to this broker events, returning a subscriber id
|
|
|
|
// (useful for unsubscribing later) and a channel where events will be delivered.
|
|
|
|
func (broker *EventBroker) subscribe() (uint64, <-chan string) {
|
|
|
|
id := broker.idgen.Add(1)
|
|
|
|
outEvents := make(chan string)
|
|
|
|
broker.inSubscriptions <- Subscription{id, outEvents}
|
|
|
|
return id, outEvents
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the subscriber with the given id from the broker,
|
|
|
|
// closing its associated channel.
|
|
|
|
func (broker *EventBroker) unsubscribe(id uint64) {
|
|
|
|
broker.inSubscriptions <- Subscription{id: id, outEvents: nil}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Publish an event to all the broker subscribers.
|
|
|
|
func (broker *EventBroker) publish(event string) {
|
|
|
|
broker.inEvents <- event
|
|
|
|
}
|