feat(backend): serve static frontend files

This commit is contained in:
2025-03-11 18:50:10 +01:00
parent 9088916f92
commit bbf83ef811
4 changed files with 55 additions and 17 deletions

26
backend/api/static.go Normal file
View File

@ -0,0 +1,26 @@
package api
import (
"embed"
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
func SPAMiddleware(fs embed.FS, prefix string, notFoundPath string) gin.HandlerFunc {
fileServer := http.FileServerFS(fs)
return func(c *gin.Context) {
if strings.HasPrefix(c.Request.URL.Path, "/api/") {
c.Next()
return
}
c.Request.URL.Path = prefix + c.Request.URL.Path
_, err := fs.Open(c.Request.URL.Path)
if err != nil {
c.Request.URL.Path = prefix + notFoundPath
}
fileServer.ServeHTTP(c.Writer, c.Request)
}
}