package api import ( "embed" "io/fs" "net/http" "strings" "github.com/gin-gonic/gin" ) //go:embed static/* var staticFS embed.FS func (s *Server) serveSPA(c *gin.Context) { if strings.HasPrefix(c.Request.URL.Path, "/api/") { c.JSON(http.StatusNotFound, gin.H{"error": "not found"}) return } sub, err := fs.Sub(staticFS, "static") if err != nil { c.String(http.StatusInternalServerError, "static fs error") return } path := strings.TrimPrefix(c.Request.URL.Path, "/") if path == "" { path = "index.html" } data, err := fs.ReadFile(sub, path) if err != nil { data, err = fs.ReadFile(sub, "index.html") if err != nil { c.String(http.StatusNotFound, "not found") return } } contentType := "text/html; charset=utf-8" switch { case strings.HasSuffix(path, ".js"): contentType = "application/javascript" case strings.HasSuffix(path, ".css"): contentType = "text/css" case strings.HasSuffix(path, ".svg"): contentType = "image/svg+xml" } c.Data(http.StatusOK, contentType, data) }