The file struct:
dist
├── assets
│ ├── index-BNdx8Y0l.js
│ └── index-DVw0mqUT.css
├── favicon.ico
└── index.html
The code:
package main
import (
"embed"
"fmt"
"github.com/gin-gonic/gin"
"io/fs"
"net/http"
"strings"
)
//go:embed dist/*
var staticFS embed.FS
func main() {
r := gin.Default()
distFS, _ := fs.Sub(staticFS, "dist")
assetsFs, _ := fs.Sub(distFS, "assets")
r.StaticFS("/assets", http.FS(assetsFs))
r.Use(func(c *gin.Context) {
if strings.HasPrefix(c.Request.URL.Path, "/assets/") {
c.Header("Cache-Control", "public, max-age=31536000, immutable")
}
c.Next()
})
r.StaticFileFS("/favicon.ico", "favicon.ico", http.FS(distFS))
r.NoRoute(func(c *gin.Context) {
c.FileFromFS("/", http.FS(distFS))
})
r.Run()
}