about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAlan Pearce2024-06-11 10:07:25 +0200
committerAlan Pearce2024-06-11 10:07:25 +0200
commit66b66e6118dd43ccbd1e85e33d44a02de7a2b812 (patch)
tree32987723187ddac9d3a8aa8187aa20080aa5c91d
parente89fbd7ef3e418629ea2533cf4fa9707fe51950f (diff)
downloadwebsite-66b66e6118dd43ccbd1e85e33d44a02de7a2b812.tar.lz
website-66b66e6118dd43ccbd1e85e33d44a02de7a2b812.tar.zst
website-66b66e6118dd43ccbd1e85e33d44a02de7a2b812.zip
server: automatically build, re-build and reload in dev
-rw-r--r--cmd/server/main.go15
-rw-r--r--go.mod2
-rw-r--r--go.sum7
-rw-r--r--internal/server/dev.go71
-rw-r--r--internal/server/server.go54
-rw-r--r--modd.conf8
-rw-r--r--nix/gomod2nix.toml6
7 files changed, 144 insertions, 19 deletions
diff --git a/cmd/server/main.go b/cmd/server/main.go
index 464c438..51a9e52 100644
--- a/cmd/server/main.go
+++ b/cmd/server/main.go
@@ -28,7 +28,16 @@ func main() {
 		}
 		panic("parsing runtime configuration" + err.Error())
 	}
-	log.Configure(runtimeConfig.Production)
+	log.Configure(!runtimeConfig.Development)
+
+	if runtimeConfig.Development {
+		tmpdir, err := os.MkdirTemp("", "website")
+		if err != nil {
+			log.Fatal("could not create temporary directory to build website: %v", err)
+		}
+		runtimeConfig.Root = tmpdir
+		defer os.RemoveAll(tmpdir)
+	}
 
 	c := make(chan os.Signal, 2)
 	signal.Notify(c, os.Interrupt)
@@ -52,9 +61,7 @@ func main() {
 		defer wg.Done()
 		sErr <- sv.Start()
 	}()
-	if !runtimeConfig.InDevServer {
-		log.Info("server listening", "address", sv.Addr)
-	}
+	log.Info("server listening", "address", sv.Addr)
 
 	err = <-sErr
 	if err != nil {
diff --git a/go.mod b/go.mod
index a9faab2..5495a00 100644
--- a/go.mod
+++ b/go.mod
@@ -14,6 +14,8 @@ require (
 	github.com/crewjam/csp v0.0.2
 	github.com/deckarep/golang-set/v2 v2.6.0
 	github.com/fatih/structtag v1.2.0
+	github.com/fsnotify/fsnotify v1.7.0
+	github.com/osdevisnot/sorvor v0.4.4
 	github.com/otiai10/copy v1.14.0
 	github.com/pkg/errors v0.9.1
 	github.com/snabb/sitemap v1.0.4
diff --git a/go.sum b/go.sum
index 1500ab2..c65524b 100644
--- a/go.sum
+++ b/go.sum
@@ -38,8 +38,11 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/deckarep/golang-set/v2 v2.6.0 h1:XfcQbWM1LlMB8BsJ8N9vW5ehnnPVIw0je80NsVHagjM=
 github.com/deckarep/golang-set/v2 v2.6.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
+github.com/evanw/esbuild v0.14.11/go.mod h1:GG+zjdi59yh3ehDn4ZWfPcATxjPDUH53iU4ZJbp7dkY=
 github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
 github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
+github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
+github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
 github.com/go-test/deep v1.1.0 h1:WOcxcdHcvdgThNXjw0t76K42FXTU7HpNQWHpA2HHNlg=
 github.com/go-test/deep v1.1.0/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
 github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
@@ -60,6 +63,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
 github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
 github.com/microcosm-cc/bluemonday v1.0.26 h1:xbqSvqzQMeEHCqMi64VAs4d8uy6Mequs3rQ0k/Khz58=
 github.com/microcosm-cc/bluemonday v1.0.26/go.mod h1:JyzOCs9gkyQyjs+6h10UEVSe02CGwkhd72Xdqh78TWs=
+github.com/osdevisnot/sorvor v0.4.4 h1:hcMWsWOKpUtDUE3F7dra1Jf12ftLHfgDcxlyPeVlz0Y=
+github.com/osdevisnot/sorvor v0.4.4/go.mod h1:D/j+vvJEmjIXndJf37uwFWD0Hjcq9DiGojyt4yMo7H0=
 github.com/otiai10/copy v1.14.0 h1:dCI/t1iTdYGtkvCuBG2BgR6KZa83PTclw4U5n2wAllU=
 github.com/otiai10/copy v1.14.0/go.mod h1:ECfuL02W+/FkTWZWgQqXPWZgW9oeKCSQ5qVfSc4qc4w=
 github.com/otiai10/mint v1.5.1 h1:XaPLeE+9vGbuyEHem1JNk3bYc7KKqyI/na0/mLd/Kks=
@@ -126,6 +131,8 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
 golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.0.0-20210908233432-aa78b53d3365/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.0.0-20220111092808-5a964db01320/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
diff --git a/internal/server/dev.go b/internal/server/dev.go
new file mode 100644
index 0000000..f7ebb82
--- /dev/null
+++ b/internal/server/dev.go
@@ -0,0 +1,71 @@
+package server
+
+import (
+	"fmt"
+	"io/fs"
+	"log/slog"
+	"os"
+	"path/filepath"
+	"time"
+	"website/internal/log"
+
+	"github.com/fsnotify/fsnotify"
+	"github.com/pkg/errors"
+)
+
+type FileWatcher struct {
+	*fsnotify.Watcher
+}
+
+func NewFileWatcher() (*FileWatcher, error) {
+	watcher, err := fsnotify.NewWatcher()
+	if err != nil {
+		return nil, errors.WithMessage(err, "could not create watcher")
+	}
+
+	return &FileWatcher{watcher}, nil
+}
+
+func (watcher FileWatcher) AddRecursive(from string) error {
+	log.Debug("walking directory tree", "root", from)
+	err := filepath.WalkDir(from, func(path string, entry fs.DirEntry, err error) error {
+		if err != nil {
+			return errors.WithMessagef(err, "could not walk directory %s", path)
+		}
+		if entry.IsDir() {
+			log.Debug("adding directory to watcher", "path", path)
+			if err = watcher.Add(path); err != nil {
+				return errors.WithMessagef(err, "could not add directory %s to watcher", path)
+			}
+		}
+
+		return nil
+	})
+
+	return errors.WithMessage(err, "error walking directory tree")
+}
+
+func (watcher FileWatcher) Start(callback func(string)) {
+	for {
+		select {
+		case event := <-watcher.Events:
+			if event.Has(fsnotify.Create) || event.Has(fsnotify.Rename) {
+				f, err := os.Stat(event.Name)
+				if err != nil {
+					slog.Error(fmt.Sprintf("error handling %s event: %v", event.Op.String(), err))
+				} else if f.IsDir() {
+					err = watcher.Add(event.Name)
+					if err != nil {
+						slog.Error(fmt.Sprintf("error adding new folder to watcher: %v", err))
+					}
+				}
+			}
+			if event.Has(fsnotify.Rename) || event.Has(fsnotify.Write) {
+				callback(event.Name)
+				time.Sleep(500 * time.Millisecond)
+			}
+		case err := <-watcher.Errors:
+			slog.Error(fmt.Sprintf("error in watcher: %v", err))
+		}
+	}
+}
diff --git a/internal/server/server.go b/internal/server/server.go
index 31db347..4692965 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -9,10 +9,12 @@ import (
 	"slices"
 	"time"
 
+	"website/internal/builder"
 	cfg "website/internal/config"
 	"website/internal/log"
 	"website/internal/website"
 
+	"github.com/osdevisnot/sorvor/pkg/livereload"
 	"github.com/pkg/errors"
 	"golang.org/x/net/http2"
 	"golang.org/x/net/http2/h2c"
@@ -25,8 +27,7 @@ var (
 )
 
 type Config struct {
-	Production    bool   `conf:"default:false"`
-	InDevServer   bool   `conf:"default:false"`
+	Development   bool   `conf:"default:false,flag:dev"`
 	Root          string `conf:"default:website"`
 	ListenAddress string `conf:"default:localhost"`
 	Port          string `conf:"default:3000,short:p"`
@@ -68,12 +69,43 @@ func New(runtimeConfig *Config) (*Server, error) {
 	}
 
 	listenAddress := net.JoinHostPort(runtimeConfig.ListenAddress, runtimeConfig.Port)
+	top := http.NewServeMux()
 
-	if !runtimeConfig.Production {
+	if runtimeConfig.Development {
 		applyDevModeOverrides(config, listenAddress)
+		builderConfig := builder.IOConfig{
+			Source:      "content",
+			Destination: runtimeConfig.Root,
+			BaseURL:     config.BaseURL,
+			Development: true,
+		}
+		builder.BuildSite(builderConfig)
+
+		liveReload := livereload.New()
+		top.Handle("/_/reload", liveReload)
+		liveReload.Start()
+		fw, err := NewFileWatcher()
+		if err != nil {
+			return nil, errors.WithMessage(err, "could not create file watcher")
+		}
+		for _, dir := range []string{"content", "static", "templates"} {
+			err := fw.AddRecursive(dir)
+			if err != nil {
+				return nil, errors.WithMessagef(
+					err,
+					"could not add directory %s to file watcher",
+					dir,
+				)
+			}
+		}
+		go fw.Start(func(filename string) {
+			log.Debug("file updated", "filename", filename)
+			builder.BuildSite(builderConfig)
+			liveReload.Reload()
+		})
 	}
 
-	top := http.NewServeMux()
+	loggingMux := http.NewServeMux()
 	mux, err := website.NewMux(config, runtimeConfig.Root)
 	if err != nil {
 		return nil, errors.Wrap(err, "could not create website mux")
@@ -81,13 +113,19 @@ func New(runtimeConfig *Config) (*Server, error) {
 	log.Debug("binding main handler to", "host", listenAddress)
 	hostname := config.BaseURL.Hostname()
 
-	top.Handle(hostname+"/", mux)
+	loggingMux.Handle(hostname+"/", mux)
 
-	top.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
+	loggingMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
 		newURL := config.BaseURL.JoinPath(r.URL.String())
 		http.Redirect(w, r, newURL.String(), 301)
 	})
 
+	top.Handle("/",
+		serverHeaderHandler(
+			wrapHandlerWithLogging(loggingMux),
+		),
+	)
+
 	top.HandleFunc("/health", func(w http.ResponseWriter, _ *http.Request) {
 		w.WriteHeader(http.StatusNoContent)
 	})
@@ -97,9 +135,7 @@ func New(runtimeConfig *Config) (*Server, error) {
 			Addr:              listenAddress,
 			ReadHeaderTimeout: 1 * time.Minute,
 			Handler: http.MaxBytesHandler(h2c.NewHandler(
-				serverHeaderHandler(
-					wrapHandlerWithLogging(top),
-				),
+				top,
 				&http2.Server{
 					IdleTimeout: 15 * time.Minute,
 				},
diff --git a/modd.conf b/modd.conf
index a76738f..1c46a59 100644
--- a/modd.conf
+++ b/modd.conf
@@ -1,7 +1,3 @@
-config.toml content/** static/** cmd/build/* internal/** {
-    prep: go run ./cmd/build --base-url http://localhost:3000 --dev
-}
-
-config.toml website/** cmd/server/* "internal/{config,log,server,website}/**" {
-    daemon: go run ./cmd/server
+config.toml cmd/server/* "internal/**" {
+    daemon: go run ./cmd/server --dev
 }
diff --git a/nix/gomod2nix.toml b/nix/gomod2nix.toml
index 234e43e..089a876 100644
--- a/nix/gomod2nix.toml
+++ b/nix/gomod2nix.toml
@@ -59,6 +59,9 @@ schema = 3
   [mod."github.com/fatih/structtag"]
     version = "v1.2.0"
     hash = "sha256-Y2pjiEmMsxfUH8LONU2/f8k1BibOHeLKJmi4uZm/SSU="
+  [mod."github.com/fsnotify/fsnotify"]
+    version = "v1.7.0"
+    hash = "sha256-MdT2rQyQHspPJcx6n9ozkLbsktIOJutOqDuKpNAtoZY="
   [mod."github.com/golang/groupcache"]
     version = "v0.0.0-20210331224755-41bb18bfe9da"
     hash = "sha256-7Gs7CS9gEYZkbu5P4hqPGBpeGZWC64VDwraSKFF+VR0="
@@ -68,6 +71,9 @@ schema = 3
   [mod."github.com/microcosm-cc/bluemonday"]
     version = "v1.0.26"
     hash = "sha256-ZX4QUWHVEoGBeTHfPcLD5XoiubeO8GhkdqkC4Me8nRE="
+  [mod."github.com/osdevisnot/sorvor"]
+    version = "v0.4.4"
+    hash = "sha256-BhyO7bvwxIdEV+c6Eo1uqahhcgsHiS8nJpg2aT8t+8s="
   [mod."github.com/otiai10/copy"]
     version = "v1.14.0"
     hash = "sha256-xsaL1ddkPS544y0Jv7u/INUALBYmYq29ddWvysLXk4A="