From 695a99daded6c93d9c0f90af2cef1a133fbcdd43 Mon Sep 17 00:00:00 2001 From: Dan Sosedoff Date: Sat, 3 Dec 2022 18:27:46 -0600 Subject: [PATCH] Enable dev assets mode with PGWEB_ASSETS_DEVMODE env var --- pkg/api/api.go | 4 ++-- static/data.go | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/pkg/api/api.go b/pkg/api/api.go index fafec07..ae76567 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -63,7 +63,7 @@ func GetHome(prefix string) http.Handler { if prefix != "" { prefix = "/" + prefix } - return http.StripPrefix(prefix, http.FileServer(http.FS(static.Static))) + return http.StripPrefix(prefix, static.GetHandler()) } func GetAssets(prefix string) http.Handler { @@ -72,7 +72,7 @@ func GetAssets(prefix string) http.Handler { } else { prefix = "/static/" } - return http.StripPrefix(prefix, http.FileServer(http.FS(static.Static))) + return http.StripPrefix(prefix, static.GetHandler()) } // GetSessions renders the number of active sessions diff --git a/static/data.go b/static/data.go index 462a3aa..cee10cd 100644 --- a/static/data.go +++ b/static/data.go @@ -1,7 +1,22 @@ package static -import "embed" +import ( + "embed" + "net/http" + "os" +) //go:embed img/* js/* css/* fonts/* //go:embed index.html -var Static embed.FS \ No newline at end of file +var assets embed.FS + +func GetFilesystem() http.FileSystem { + if os.Getenv("PGWEB_ASSETS_DEVMODE") == "1" { + return http.Dir("./static") + } + return http.FS(assets) +} + +func GetHandler() http.Handler { + return http.FileServer(GetFilesystem()) +}