From a6abe54dbc7846063300ffea10a3ce8dacf6fffd Mon Sep 17 00:00:00 2001 From: Balakrishnan Balasubramanian Date: Wed, 13 Sep 2023 14:38:42 -0400 Subject: [PATCH] Remove live reload js when not in dev mode Fixes #16 --- main.go | 43 +++++++++++++++++++++++++++++-------------- web/index.html | 2 +- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index 2545ef3..5925a39 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "context" "crypto/rand" "embed" @@ -12,6 +13,7 @@ import ( "net/http" "os" "path" + "regexp" "sync/atomic" "time" @@ -30,14 +32,14 @@ var ( imagesDirFs fs.FS listenAddr string - // go:embed web/* + //go:embed web webFS embed.FS ) func main() { flag.StringVar(&imagesDir, "images-dir", "images", "Sets the images dir") flag.StringVar(&collageDir, "collages-dir", "collages", "Sets the collages dir") - flag.BoolVar(&localAssets, "local-assets", false, "Serve local assets for testing") + flag.BoolVar(&localAssets, "local-assets", false, "Serve local assets during development") flag.StringVar(&listenAddr, "addr", "127.0.0.1:8767", "Web listen address, see https://pkg.go.dev/go.balki.me/anyhttp#readme-address-syntax") flag.Parse() @@ -48,7 +50,6 @@ func main() { collagesPath := "collages" addFileServer := func(path, dir string) { - httpFileServer := http.FileServer(http.Dir(dir)) http.Handle("/"+path+"/", http.StripPrefix("/"+path, httpFileServer)) } @@ -63,14 +64,29 @@ func main() { httpFileServer.ServeHTTP(w, r) }) } else { - fs, err := fs.Sub(webFS, "web") - - if err != nil { - panic(err) - } - - httpFileServer := http.FileServer(http.FS(fs)) - http.Handle("/", httpFileServer) + indexModTime := time.Now() + indexHTML := func() io.ReadSeeker { + indexHTMLContent, err := webFS.ReadFile("web/index.html") + if err != nil { + panic(err) + } + devOnlyRegex := regexp.MustCompile("\n[^\n]*