From 68ff9fb91f49631f9fb9e35119fccb66f78d346f Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Tue, 29 Jul 2025 21:04:09 +0200 Subject: Don’t require a rebuild when changing CSS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 9 +++++---- src/app.go | 2 ++ src/http.go | 9 +++++++-- src/templates.go | 5 +++-- src/templates/-base.html.tmpl | 6 +++++- 5 files changed, 22 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index f73567b..86e28b0 100644 --- a/main.go +++ b/main.go @@ -27,7 +27,8 @@ func main() { Try(os.Chdir(filepath.Dir(os.Args[0]))) port := flag.Int("port", 8080, "port number") - debugp := flag.Bool("debug", false, "run in debug mode") + flag.BoolVar(&app.Debugp, "debug", false, + "run in debug mode") flag.BoolVar(&email.Config.Disabled, "no-email", false, "disables email support") flag.StringVar(&email.Config.Host, "smtp-host", "smtp.migadu.com", @@ -46,7 +47,7 @@ func main() { defer func() { if p := recover(); p != nil { - if *debugp { + if app.Debugp { log.Print(p) time.Sleep(1 * time.Second) app.Restart() @@ -63,12 +64,12 @@ func main() { os.Exit(0) }() - if *debugp { + if app.Debugp { go watch.File(Try2(os.Executable()), app.Restart) } i18n.Init() dbx.Init(Try2(os.OpenRoot("src/dbx/sql")).FS()) - app.BuildTemplates(Try2(os.OpenRoot("src/templates")).FS(), *debugp) + app.BuildTemplates(Try2(os.OpenRoot("src/templates")).FS()) app.Run(*port) } diff --git a/src/app.go b/src/app.go index 7b3d905..944b3b6 100644 --- a/src/app.go +++ b/src/app.go @@ -8,6 +8,8 @@ import ( . "git.thomasvoss.com/euro-cash.eu/pkg/try" ) +var Debugp bool + func Restart() { path := Try2(os.Executable()) atexit.Exec() diff --git a/src/http.go b/src/http.go index b0d5bcd..00078c9 100644 --- a/src/http.go +++ b/src/http.go @@ -34,7 +34,11 @@ func Run(port int) { mux.Handle("GET /favicon.ico", fs) mux.Handle("GET /fonts/", fs) mux.Handle("GET /storage/", fs) - mux.Handle("GET /style.min.css", fs) + if Debugp { + mux.Handle("GET /style.css", fs) + } else { + mux.Handle("GET /style.min.css", fs) + } mux.Handle("GET /coins/designs", mwareC(final)) mux.Handle("GET /coins/mintages", mwareM(final)) mux.Handle("GET /collecting/crh", mwareC(final)) @@ -57,7 +61,8 @@ func chain(xs ...middleware) middleware { func firstHandler(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - ctx := context.WithValue(r.Context(), "td", &templateData{}) + ctx := context.WithValue(r.Context(), "td", + &templateData{Debugp: Debugp}) next.ServeHTTP(w, r.WithContext(ctx)) }) } diff --git a/src/templates.go b/src/templates.go index 353c755..dca1144 100644 --- a/src/templates.go +++ b/src/templates.go @@ -15,6 +15,7 @@ import ( ) type templateData struct { + Debugp bool Printer i18n.Printer Code, Type string Mintages dbx.MintageData @@ -35,7 +36,7 @@ var ( } ) -func BuildTemplates(dir fs.FS, debugp bool) { +func BuildTemplates(dir fs.FS) { ents := Try2(fs.ReadDir(dir, ".")) notFoundTmpl = buildTemplate(dir, "-404") errorTmpl = buildTemplate(dir, "-error") @@ -44,7 +45,7 @@ func BuildTemplates(dir fs.FS, debugp bool) { for _, e := range ents { name := e.Name() buildAndSetTemplate(dir, name) - if debugp { + if Debugp { go watch.FileFS(dir, name, func() { defer func() { if p := recover(); p != nil { diff --git a/src/templates/-base.html.tmpl b/src/templates/-base.html.tmpl index 388561b..156fa47 100644 --- a/src/templates/-base.html.tmpl +++ b/src/templates/-base.html.tmpl @@ -3,8 +3,12 @@ + {{ if .Debugp }} + + {{ else }} - {{ .Get "Euro Cash" }} + {{ end }} + {{ .Get "Euro Cash Wiki" }}