diff options
author | Alan Pearce | 2024-06-11 10:07:25 +0200 |
---|---|---|
committer | Alan Pearce | 2024-06-11 10:07:25 +0200 |
commit | 66b66e6118dd43ccbd1e85e33d44a02de7a2b812 (patch) | |
tree | 32987723187ddac9d3a8aa8187aa20080aa5c91d /internal/server/server.go | |
parent | e89fbd7ef3e418629ea2533cf4fa9707fe51950f (diff) | |
download | website-66b66e6118dd43ccbd1e85e33d44a02de7a2b812.tar.lz website-66b66e6118dd43ccbd1e85e33d44a02de7a2b812.tar.zst website-66b66e6118dd43ccbd1e85e33d44a02de7a2b812.zip |
server: automatically build, re-build and reload in dev
Diffstat (limited to 'internal/server/server.go')
-rw-r--r-- | internal/server/server.go | 54 |
1 files changed, 45 insertions, 9 deletions
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, }, |