mirror of
https://github.com/truewhile/MeBox.git
synced 2026-09-29 19:36:36 +08:00
refactor: split modules and harden scraping workflows
This commit is contained in:
+5
-5
@@ -218,14 +218,14 @@ func serveSPA(r *gin.Engine, webDir string) {
|
||||
assets.Static("/", filepath.Join(webDir, "assets"))
|
||||
brand := r.Group("/brand")
|
||||
brand.Use(func(c *gin.Context) {
|
||||
c.Header("Cache-Control", "public, max-age=86400")
|
||||
setNoCacheHeaders(c)
|
||||
c.Next()
|
||||
})
|
||||
brand.Static("/", filepath.Join(webDir, "brand"))
|
||||
for _, icon := range []string{"/favicon.ico", "/favicon.svg"} {
|
||||
iconPath := filepath.Join(webDir, strings.TrimPrefix(icon, "/"))
|
||||
r.GET(icon, serveNoCacheFile(iconPath))
|
||||
r.HEAD(icon, serveNoCacheFile(iconPath))
|
||||
for _, rootFile := range []string{"/favicon.ico", "/favicon.svg", "/artwork-cache-sw.js"} {
|
||||
filePath := filepath.Join(webDir, strings.TrimPrefix(rootFile, "/"))
|
||||
r.GET(rootFile, serveNoCacheFile(filePath))
|
||||
r.HEAD(rootFile, serveNoCacheFile(filePath))
|
||||
}
|
||||
r.NoRoute(func(c *gin.Context) {
|
||||
path := c.Request.URL.Path
|
||||
|
||||
@@ -64,6 +64,9 @@ func TestServeSPAServesAssetsImmutableAndBypassesAPIRoutes(t *testing.T) {
|
||||
if err := os.WriteFile(filepath.Join(webDir, "brand", "mediastationgo-logo.svg"), []byte("<svg></svg>"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(webDir, "artwork-cache-sw.js"), []byte("self.addEventListener('fetch', () => {})"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
router := gin.New()
|
||||
serveSPA(router, webDir)
|
||||
@@ -84,10 +87,26 @@ func TestServeSPAServesAssetsImmutableAndBypassesAPIRoutes(t *testing.T) {
|
||||
if brandResp.Code != http.StatusOK {
|
||||
t.Fatalf("brand asset status = %d, want 200", brandResp.Code)
|
||||
}
|
||||
if got := brandResp.Header().Get("Cache-Control"); !strings.Contains(got, "no-store") {
|
||||
t.Fatalf("brand asset Cache-Control = %q, want no-store", got)
|
||||
}
|
||||
if strings.Contains(brandResp.Body.String(), "index") {
|
||||
t.Fatalf("brand asset should not serve SPA index: %q", brandResp.Body.String())
|
||||
}
|
||||
|
||||
swReq := httptest.NewRequest(http.MethodGet, "/artwork-cache-sw.js", nil)
|
||||
swResp := httptest.NewRecorder()
|
||||
router.ServeHTTP(swResp, swReq)
|
||||
if swResp.Code != http.StatusOK {
|
||||
t.Fatalf("service worker status = %d, want 200", swResp.Code)
|
||||
}
|
||||
if got := swResp.Header().Get("Cache-Control"); !strings.Contains(got, "no-store") {
|
||||
t.Fatalf("service worker Cache-Control = %q, want no-store", got)
|
||||
}
|
||||
if strings.Contains(swResp.Body.String(), "index") {
|
||||
t.Fatalf("service worker should not serve SPA index: %q", swResp.Body.String())
|
||||
}
|
||||
|
||||
for _, path := range []string{
|
||||
"/api/missing",
|
||||
"/emby",
|
||||
|
||||
Reference in New Issue
Block a user