diff --git a/frontend/src/components/Stats.spec.ts b/frontend/src/components/Insights.spec.ts similarity index 100% rename from frontend/src/components/Stats.spec.ts rename to frontend/src/components/Insights.spec.ts diff --git a/frontend/src/components/Stats.vue b/frontend/src/components/Insights.vue similarity index 100% rename from frontend/src/components/Stats.vue rename to frontend/src/components/Insights.vue diff --git a/frontend/src/components/Dashboard.spec.ts b/frontend/src/components/Overview.spec.ts similarity index 100% rename from frontend/src/components/Dashboard.spec.ts rename to frontend/src/components/Overview.spec.ts diff --git a/frontend/src/components/Dashboard.vue b/frontend/src/components/Overview.vue similarity index 100% rename from frontend/src/components/Dashboard.vue rename to frontend/src/components/Overview.vue diff --git a/frontend/src/components/VulnerablePackages.spec.ts b/frontend/src/components/Security.spec.ts similarity index 100% rename from frontend/src/components/VulnerablePackages.spec.ts rename to frontend/src/components/Security.spec.ts diff --git a/frontend/src/components/VulnerablePackages.vue b/frontend/src/components/Security.vue similarity index 100% rename from frontend/src/components/VulnerablePackages.vue rename to frontend/src/components/Security.vue diff --git a/pkg/app/app.go b/pkg/app/app.go index a3929f1..b8d3dec 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -17,7 +17,6 @@ import ( "github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3/middleware/adaptor" - "github.com/gofiber/fiber/v3/middleware/static" "github.com/lukaszraczylo/gohoarder/pkg/analytics" "github.com/lukaszraczylo/gohoarder/pkg/auth" "github.com/lukaszraczylo/gohoarder/pkg/cache" @@ -595,7 +594,7 @@ func (a *App) setupServer() error { frontendDir := "frontend/dist" if _, err := os.Stat(frontendDir); err == nil { log.Info().Str("dir", frontendDir).Msg("Serving frontend static files") - a.app.Use("/", static.New(frontendDir)) + a.app.Use("/", spaStatic(frontendDir)) } else { log.Warn().Msg("Frontend dist directory not found, frontend won't be served") a.app.Get("/", func(c fiber.Ctx) error { diff --git a/pkg/app/spa.go b/pkg/app/spa.go new file mode 100644 index 0000000..afb70b0 --- /dev/null +++ b/pkg/app/spa.go @@ -0,0 +1,37 @@ +package app + +import ( + "os" + "path/filepath" + "strings" + + "github.com/gofiber/fiber/v3" + "github.com/gofiber/fiber/v3/middleware/static" +) + +// A page load sends Accept: text/html; a missing asset does not, and version segments such as v1.0.0 have dots. +// Paths that belong to the server; an unknown one stays a 404 instead of the SPA shell. +var nonSPAPrefixes = []string{"/api/", "/go/", "/npm/", "/pypi/", "/ws"} + +// spaStatic serves dir and falls back to index.html for client-side routes, so reload and shared links work. +func spaStatic(dir string) fiber.Handler { + return static.New(dir, static.Config{ + NotFoundHandler: func(c fiber.Ctx) error { + p := c.Path() + if (c.Method() != fiber.MethodGet && c.Method() != fiber.MethodHead) || !strings.Contains(c.Get(fiber.HeaderAccept), "text/html") { + return fiber.ErrNotFound + } + for _, prefix := range nonSPAPrefixes { + if strings.HasPrefix(p, prefix) { + return fiber.ErrNotFound + } + } + index, err := os.ReadFile(filepath.Join(dir, "index.html")) // #nosec G304 -- fixed file under the configured frontend dir + if err != nil { + return fiber.ErrNotFound + } + c.Status(fiber.StatusOK).Type("html") + return c.Send(index) + }, + }) +} diff --git a/pkg/app/spa_test.go b/pkg/app/spa_test.go new file mode 100644 index 0000000..0f7f68b --- /dev/null +++ b/pkg/app/spa_test.go @@ -0,0 +1,53 @@ +package app + +import ( + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "testing" + + "github.com/gofiber/fiber/v3" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func spaApp(t *testing.T) *fiber.App { + t.Helper() + dir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(dir, "index.html"), []byte("shell"), 0o600)) + require.NoError(t, os.WriteFile(filepath.Join(dir, "app.js"), []byte("js"), 0o600)) + app := fiber.New() + app.Use("/", spaStatic(dir)) + return app +} + +func TestSPAStaticFallback(t *testing.T) { + app := spaApp(t) + tests := []struct { + name string + method string + path string + want int + html bool + }{ + {"asset", http.MethodGet, "/app.js", http.StatusOK, false}, + {"client route", http.MethodGet, "/packages", http.StatusOK, true}, + {"deep client route", http.MethodGet, "/package/go/github.com/a/b/v1.0.0", http.StatusOK, true}, + {"unknown api path", http.MethodGet, "/api/nope", http.StatusNotFound, true}, + {"missing asset", http.MethodGet, "/missing.js", http.StatusNotFound, false}, + {"post is not a page", http.MethodPost, "/packages", http.StatusNotFound, true}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + req := httptest.NewRequest(tc.method, tc.path, nil) + if tc.html { + req.Header.Set(fiber.HeaderAccept, "text/html,application/xhtml+xml") + } + resp, err := app.Test(req) + require.NoError(t, err) + defer func() { _ = resp.Body.Close() }() + assert.Equal(t, tc.want, resp.StatusCode) + }) + } +}