Remove live reload js when not in dev mode

Fixes #16
This commit is contained in:
Balakrishnan Balasubramanian 2023-09-13 14:38:42 -04:00
parent ab636df83d
commit a6abe54dbc
2 changed files with 30 additions and 15 deletions

37
main.go
View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"bytes"
"context" "context"
"crypto/rand" "crypto/rand"
"embed" "embed"
@ -12,6 +13,7 @@ import (
"net/http" "net/http"
"os" "os"
"path" "path"
"regexp"
"sync/atomic" "sync/atomic"
"time" "time"
@ -30,14 +32,14 @@ var (
imagesDirFs fs.FS imagesDirFs fs.FS
listenAddr string listenAddr string
// go:embed web/* //go:embed web
webFS embed.FS webFS embed.FS
) )
func main() { func main() {
flag.StringVar(&imagesDir, "images-dir", "images", "Sets the images dir") flag.StringVar(&imagesDir, "images-dir", "images", "Sets the images dir")
flag.StringVar(&collageDir, "collages-dir", "collages", "Sets the collages 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.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() flag.Parse()
@ -48,7 +50,6 @@ func main() {
collagesPath := "collages" collagesPath := "collages"
addFileServer := func(path, dir string) { addFileServer := func(path, dir string) {
httpFileServer := http.FileServer(http.Dir(dir)) httpFileServer := http.FileServer(http.Dir(dir))
http.Handle("/"+path+"/", http.StripPrefix("/"+path, httpFileServer)) http.Handle("/"+path+"/", http.StripPrefix("/"+path, httpFileServer))
} }
@ -63,14 +64,29 @@ func main() {
httpFileServer.ServeHTTP(w, r) httpFileServer.ServeHTTP(w, r)
}) })
} else { } else {
fs, err := fs.Sub(webFS, "web") indexModTime := time.Now()
indexHTML := func() io.ReadSeeker {
indexHTMLContent, err := webFS.ReadFile("web/index.html")
if err != nil { if err != nil {
panic(err) panic(err)
} }
devOnlyRegex := regexp.MustCompile("\n[^\n]*<!-- DEVONLY[^\n]*")
httpFileServer := http.FileServer(http.FS(fs)) return bytes.NewReader(devOnlyRegex.ReplaceAllLiteral(indexHTMLContent, nil))
http.Handle("/", httpFileServer) }()
httpFileServer := func() http.Handler {
webrootFs, err := fs.Sub(webFS, "web")
if err != nil {
panic(err)
}
return http.FileServer(http.FS(webrootFs))
}()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
http.ServeContent(w, r, "index.html", indexModTime, indexHTML)
} else {
httpFileServer.ServeHTTP(w, r)
}
})
} }
http.HandleFunc("/make-collage", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/make-collage", func(w http.ResponseWriter, r *http.Request) {
@ -105,7 +121,8 @@ func main() {
if err := idle.Wait(30 * time.Minute); err != nil { if err := idle.Wait(30 * time.Minute); err != nil {
slog.Error("Failed to wait for idler", "error", err) slog.Error("Failed to wait for idler", "error", err)
} }
ctx, _ := context.WithTimeout(context.Background(), 1*time.Minute) // Don't want any stuck connections ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute) // Don't want any stuck connections
defer cancel()
if err := server.Shutdown(ctx); err != nil { if err := server.Shutdown(ctx); err != nil {
slog.Error("http server Shutdown failed", "error", err) slog.Error("http server Shutdown failed", "error", err)
} }
@ -152,5 +169,3 @@ func MakeCollage(req *collage.Request) (string, error) {
} }
return collageFile, nil return collageFile, nil
} }
// curl -D - --json @req.json http://localhost:8767/make-collage

View File

@ -3,7 +3,7 @@
<head> <head>
<link rel="stylesheet" href="croppie.css" /> <link rel="stylesheet" href="croppie.css" />
<link rel="icon" href="data:;base64,iVBORw0KGgo=" /> <link rel="icon" href="data:;base64,iVBORw0KGgo=" />
<script src="http://localhost:35729/livereload.js"></script> <!-- DEVONLY --> <script src="http://localhost:35729/livereload.js"></script>
<script src="croppie.js" defer></script> <script src="croppie.js" defer></script>
<script src="index.js" defer></script> <script src="index.js" defer></script>
<link rel="stylesheet" href="index.css" /> <link rel="stylesheet" href="index.css" />