正如杜松子酒在这里描述的那样
r.Use(static.Serve("/", static.LocalFile("/src", false)))
r.NoRoute(func(c *gin.Context){
c.File("/src/index.html")
})
但我不知道如何将 src 文件夹内联到二进制文件中。
我应该如何进行,或者有没有可以参考的案例项目。
该案在github.com/gin-contrib/static/_example/bindata.
package main
import (
assetfs "github.com/elazarl/go-bindata-assetfs"
"github.com/fidelyiu/yiu-note/core/asset"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"net/http"
"strings"
)
type binaryFileSystem struct {
fs http.FileSystem
}
func (b *binaryFileSystem) Open(name string) (http.File, error) {
return b.fs.Open(name)
}
func (b *binaryFileSystem) Exists(prefix string, filepath string) bool {
if p := strings.TrimPrefix(filepath, prefix); len(p) < len(filepath) {
if _, err := b.fs.Open(p); err != nil {
return false
}
return true
}
return false
}
func BinaryFileSystem(root string) *binaryFileSystem {
fs := &assetfs.AssetFS{
Asset: asset.Asset,
AssetDir: asset.AssetDir,
AssetInfo: asset.AssetInfo,
Prefix: root,
}
return &binaryFileSystem{
fs,
}
}
func main() {
r := gin.Default()
// go-bindata -o=core/asset/asset.go -pkg=asset dist/...
r.Use(static.Serve("/", BinaryFileSystem("dist")))
r.NoRoute(func(c *gin.Context) {
c.File("./dist/index.html")
})
_ = r.Run(":8080")
}