mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-26 06:37:07 +00:00
fix(app): serve the dashboard for client-side routes
A reload or shared link such as /packages?verdict=held returned 404 because the static handler had no fallback. Page loads for paths outside the API and proxy prefixes now receive index.html.
This commit is contained in:
+1
-2
@@ -17,7 +17,6 @@ import (
|
|||||||
|
|
||||||
"github.com/gofiber/fiber/v3"
|
"github.com/gofiber/fiber/v3"
|
||||||
"github.com/gofiber/fiber/v3/middleware/adaptor"
|
"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/analytics"
|
||||||
"github.com/lukaszraczylo/gohoarder/pkg/auth"
|
"github.com/lukaszraczylo/gohoarder/pkg/auth"
|
||||||
"github.com/lukaszraczylo/gohoarder/pkg/cache"
|
"github.com/lukaszraczylo/gohoarder/pkg/cache"
|
||||||
@@ -595,7 +594,7 @@ func (a *App) setupServer() error {
|
|||||||
frontendDir := "frontend/dist"
|
frontendDir := "frontend/dist"
|
||||||
if _, err := os.Stat(frontendDir); err == nil {
|
if _, err := os.Stat(frontendDir); err == nil {
|
||||||
log.Info().Str("dir", frontendDir).Msg("Serving frontend static files")
|
log.Info().Str("dir", frontendDir).Msg("Serving frontend static files")
|
||||||
a.app.Use("/", static.New(frontendDir))
|
a.app.Use("/", spaStatic(frontendDir))
|
||||||
} else {
|
} else {
|
||||||
log.Warn().Msg("Frontend dist directory not found, frontend won't be served")
|
log.Warn().Msg("Frontend dist directory not found, frontend won't be served")
|
||||||
a.app.Get("/", func(c fiber.Ctx) error {
|
a.app.Get("/", func(c fiber.Ctx) error {
|
||||||
|
|||||||
@@ -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)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -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("<html>shell</html>"), 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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user