内容纲要
使用Go打包静态HTML
package main
import (
"embed"
"io/fs"
"net/http"
"github.com/gin-gonic/gin"
)
//go:embed dist/*
var vfs embed.FS
func main() {
r := gin.Default()
stripped, _ := fs.Sub(vfs, "dist")
r.StaticFS("/", http.FS(stripped))
r.NoRoute(func(c *gin.Context) {
content, _ := vfs.ReadFile("dist/index.html")
c.Writer.Write(content)
})
r.Run(":8080")
}