如何在Gin中使用History模式Vue构建单个二进制文件

huangapple go评论143阅读模式
英文:

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 &quot;github.com/elazarl/go-bindata-assetfs&quot;
	&quot;github.com/fidelyiu/yiu-note/core/asset&quot;
	&quot;github.com/gin-contrib/static&quot;
	&quot;github.com/gin-gonic/gin&quot;
	&quot;net/http&quot;
	&quot;strings&quot;
)

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) &lt; len(filepath) {
		if _, err := b.fs.Open(p); err != nil {
			return false
		}
		return true
	}
	return false
}

func BinaryFileSystem(root string) *binaryFileSystem {
	fs := &amp;assetfs.AssetFS{
		Asset:     asset.Asset,
		AssetDir:  asset.AssetDir,
		AssetInfo: asset.AssetInfo,
		Prefix:    root,
	}
	return &amp;binaryFileSystem{
		fs,
	}
}

func main() {
	r := gin.Default()
	// go-bindata -o=core/asset/asset.go -pkg=asset dist/...
	r.Use(static.Serve(&quot;/&quot;, BinaryFileSystem(&quot;dist&quot;)))
	r.NoRoute(func(c *gin.Context) {
		c.File(&quot;./dist/index.html&quot;)
	})
	_ = r.Run(&quot;:8080&quot;)
}

huangapple
  • 本文由 发表于 2021年8月8日 12:39:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/68697833.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定