chore: serve static files from dir on dev
This commit is contained in:
parent
4181264a68
commit
f25244b016
2 changed files with 11 additions and 2 deletions
|
|
@ -33,9 +33,15 @@ func SetupRoutes(a *app.App) http.Handler {
|
||||||
)
|
)
|
||||||
|
|
||||||
// Static assets (bypass router groups — registered directly on mux)
|
// Static assets (bypass router groups — registered directly on mux)
|
||||||
sub, _ := fs.Sub(assets.AssetsFS, ".")
|
var assetsFS http.FileSystem
|
||||||
|
if a.Cfg.IsProduction() {
|
||||||
|
sub, _ := fs.Sub(assets.AssetsFS, ".")
|
||||||
|
assetsFS = http.FS(sub)
|
||||||
|
} else {
|
||||||
|
assetsFS = http.Dir("./assets")
|
||||||
|
}
|
||||||
r.Mux().Handle("GET /assets/",
|
r.Mux().Handle("GET /assets/",
|
||||||
middleware.CacheStatic(http.StripPrefix("/assets/", http.FileServer(http.FS(sub)))),
|
middleware.CacheStatic(http.StripPrefix("/assets/", http.FileServer(assetsFS))),
|
||||||
)
|
)
|
||||||
|
|
||||||
// Public pages
|
// Public pages
|
||||||
|
|
|
||||||
|
|
@ -220,6 +220,9 @@ func TestSetupRoutes_NotFound(t *testing.T) {
|
||||||
func TestSetupRoutes_StaticAssets(t *testing.T) {
|
func TestSetupRoutes_StaticAssets(t *testing.T) {
|
||||||
testutil.ForEachDB(t, func(t *testing.T, dbi testutil.DBInfo) {
|
testutil.ForEachDB(t, func(t *testing.T, dbi testutil.DBInfo) {
|
||||||
a := newTestApp(dbi)
|
a := newTestApp(dbi)
|
||||||
|
// Force the embedded-FS branch so the test is independent of CWD;
|
||||||
|
// in dev we serve from ./assets on disk (see SetupRoutes).
|
||||||
|
a.Cfg.AppEnv = "production"
|
||||||
handler := SetupRoutes(a)
|
handler := SetupRoutes(a)
|
||||||
|
|
||||||
req := httptest.NewRequest(http.MethodGet, "/assets/css/output.css", nil)
|
req := httptest.NewRequest(http.MethodGet, "/assets/css/output.css", nil)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue