reCoreD-UI/server/static.go
2024-04-07 10:08:02 +08:00

36 lines
598 B
Go

package server
import (
"embed"
"io/fs"
"net/http"
"strings"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
//go:generate cp -r ../web/dist ./
//go:embed dist
var staticFiles embed.FS
func staticFileHandler() gin.HandlerFunc {
sf, err := fs.Sub(staticFiles, "dist")
if err != nil {
logrus.Fatal("compile error: ", err)
}
fs := http.FileServer(http.FS(sf))
return func(ctx *gin.Context) {
defer ctx.Abort()
filename := strings.TrimLeft(ctx.Request.RequestURI, "/")
if filename == "" {
filename = "index.html"
}
fs.ServeHTTP(ctx.Writer, ctx.Request)
}
}