英文:
How to build a single binary in Gin with the History mode Vue
问题
根据Gin的描述,您想将"src"文件夹嵌入到二进制文件中,但不知道如何操作。您想知道应该如何进行操作,或者是否有一个示例项目可以参考。
以下是翻译好的内容:
正如Gin在这里所描述的那样:
r.Use(static.Serve("/", static.LocalFile("/src", false)))
r.NoRoute(func(c *gin.Context){
c.File("/src/index.html")
})
但是我不知道如何将"src"文件夹嵌入到二进制文件中。
我应该如何进行操作,或者是否有一个示例项目可以参考?
英文:
Just as Gin describe here
r.Use(static.Serve("/", static.LocalFile("/src", false)))
r.NoRoute(func(c *gin.Context){
c.File("/src/index.html")
})
But I don't know how to inline the src folder into the binary file.
How should I proceed, or is there a case project I can refer to.
答案1
得分: 0
该案例位于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")
}
英文:
The case is in 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")
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论