Go | Go 語言打包靜態文件以及如何與Gin一起使用Go-bindata


Go | Go 語言打包靜態文件以及如何與Gin一起使用Go-bindata

系列文章目錄

第一章 Go 語言打包靜態文件以及如何與Gin一起使用Go-bindata


Table of Contents


前言

前幾天,開始學習用 Go 語言開發一個內部項目來幫助解決測試環境中的一些不便利的問題。因為開發的小項目中存在一些靜態文件和配置文件,第一打包的時候發現並沒有將靜態文件打包進入可執行文件,這樣在發布的時候又需要手動拷貝一下靜態文件,這樣就很麻煩了。在一些網上資料上發現了 go-bindata。這樣我還是只分發一個文件就可以了,不過打包出來的文件會大一點。

一、go-bindata是什么?

his package converts any file into managable Go source code. Useful for embedding binary data into a go program. The file data is optionally gzip compressed before being converted to a raw byte slice.

It comes with a command line tool in the go-bindata sub directory. This tool offers a set of command line options, used to customize the output being generated.

go-bindata 將任何文件封裝在一個 Go 語言的 Source Code 里面,文件數據在轉換為原始字節時可以選擇使用 gzip 壓縮,同時提供了統一的接口,幫助獲取原始的文件數據

二、使用步驟

1. 安裝

go get -u github.com/jteeuwen/go-bindata/...

2. 使用

使用 go-bindata --help 可以查看具體的使用方式

go-bindata --help
Usage: go-bindata [options] <input directories>

  -debug
        Do not embed the assets, but provide the embedding API. Contents will still be loaded from disk.
  -dev
        Similar to debug, but does not emit absolute paths. Expects a rootDir variable to already exist in the generated code's package.
  -fs
        Whether generate instance http.FileSystem interface code.
  -ignore value
        Regex pattern to ignore
  -mode uint
        Optional file mode override for all files.
  -modtime int
        Optional modification unix timestamp override for all files.
  -nocompress
        Assets will *not* be GZIP compressed when this flag is specified.
  -nomemcopy
        Use a .rodata hack to get rid of unnecessary memcopies. Refer to the documentation to see what implications this carries.
  -nometadata
        Assets will not preserve size, mode, and modtime info.
  -o string
        Optional name of the output file to be generated. (default "./bindata.go")
  -pkg string
        Package name to use in the generated code. (default "main")
  -prefix string
        Optional path prefix to strip off asset names.
  -tags string
        Optional set of build tags to include.
  -version
        Displays version information.

這里項目的大概結構如下

├── conf
│   └── app.ini
├── main.go
├── routers
│   └──  router.go
├── setting
│   └── setting.go
└── template
    ├── css
    │   └── chunk-vendors.c6d02872.css
    ├── favicon.ico
    ├── fonts
    │   └──  element-icons.535877f5.woff
    ├── img
    │   └──  Avatar.41ba4b7a.jpg
    ├── index.html
    └── js
        ├── app.40872d4f.js

為了查看方便和限於篇幅,刪除了一些無關的文件

在打包的時候想將 /conf/template,打包進生成的文件中

最基本的使用方式是 go-bindata <input directories> 這里我們參數全部使用默認的,這樣將創建 bindata.go 且生成的文件放在根目錄的 main 包下

go-bindata template/... conf/...

如果要修改最終生成文件名和包名可以使用 -o-pkg 參數,這樣就在 asset 下生成 asset.go 文件且包為 asset

go-bindata -o=asset/asset.go -pkg=asset template/... conf/...

3. 讀取文件

通過上述方式生成的 asset.go 文件中包含 3 個獲取文件信息的方法

  • func Asset(name string) ([]byte, error) 根據文件名加載文件返回 byte
  • func AssetNames() []string 返回所有的文件列表
  • func AssetInfo(name string) (os.FileInfo, error) 返回文件信息

如果你查看源文件,可以查看 _bindata 中維護了文件的信息

var _bindata = map[string]func() (*asset, error){
	"template/css/app.ee8ee5dd.css":              templateCssAppEe8ee5ddCss,
	"template/favicon.ico":                       templateFaviconIco,
	"template/fonts/element-icons.535877f5.woff": templateFontsElementIcons535877f5Woff,
	"template/img/Avatar.41ba4b7a.jpg":           templateImgAvatar41ba4b7aJpg,
	"template/index.html":                        templateIndexHtml,
	"template/js/app.40872d4f.js":                templateJsApp40872d4fJs,
	"conf/app.ini":                               confAppIni,
}

如果我們想讀取 conf/app.ini 的文件,可以用使用

conf_ini, _ := asset.Asset("conf/app.ini")

這樣簡單的操作就完成了

三、和 Gin 一起使用

在正常使用 Gin 時,我們一般這樣配置靜態資源的使用

r := gin.Default()

r.LoadHTMLGlob("template/index.html")
r.Static("/css", "./template/css")
r.Static("/fonts", "./template/fonts")
r.Static("/img", "./template/img")
r.Static("/js", "./template/js")
r.StaticFile("/favicon.ico", "./template/favicon.ico")

// 首頁
r.GET("/", func(c *gin.Context) {
	c.HTML(http.StatusOK, "index.html", nil)
})

為了將使用打包好的靜態資源,我們需要換種方式進行打包和配置

1. 使用 go-bindata-assetfs 進行打包

Serve embedded files from go-bindata with net/http.

go-bindata-assetfs 實現了 http.FileSystem 幫助我們更好的在 http 服務上使用生成的文件

2. 安裝 go-bindata-assetfs

這個需要和 go-bindata 一起安裝,如果已經安裝了 go-bindata 則不需要再次安裝

go get github.com/go-bindata/go-bindata/...
go get github.com/elazarl/go-bindata-assetfs/...

3. 打包文件

這里我們只需將 go-bindata 換成 go-bindata-assetfs 即可,兩者的使用方式相同

go-bindata-assetfs -o=asset/asset.go -pkg=asset template/... conf/...

4. 重新配置

r := gin.Default()

fsCss := assetfs.AssetFS{Asset: asset.Asset, AssetDir: asset.AssetDir, AssetInfo: asset.AssetInfo, Prefix: "dist/css", Fallback: "index.html"}
fsFonts := assetfs.AssetFS{Asset: asset.Asset, AssetDir: asset.AssetDir, AssetInfo: asset.AssetInfo, Prefix: "dist/fonts", Fallback: "index.html"}
fsImg := assetfs.AssetFS{Asset: asset.Asset, AssetDir: asset.AssetDir, AssetInfo: asset.AssetInfo, Prefix: "dist/img", Fallback: "index.html"}
fsJs := assetfs.AssetFS{Asset: asset.Asset, AssetDir: asset.AssetDir, AssetInfo: asset.AssetInfo, Prefix: "dist/js", Fallback: "index.html"}
fs := assetfs.AssetFS{Asset: asset.Asset, AssetDir: asset.AssetDir, AssetInfo: asset.AssetInfo, Prefix: "dist", Fallback: "index.html"}

r.StaticFS("/css", &fsCss)
r.StaticFS("/fonts", &fsFonts)
r.StaticFS("/img", &fsImg)
r.StaticFS("/js", &fsJs)
r.StaticFS("/favicon.ico", &fs)

r.GET("/", func(c *gin.Context) {
    c.Writer.WriteHeader(200)
    indexHtml, _ := asset.Asset("dist/index.html")
    _, _ = c.Writer.Write(indexHtml)
    c.Writer.Header().Add("Accept", "text/html")
    c.Writer.Flush()
})

關於

fs := assetfs.AssetFS{Asset: asset.Asset, AssetDir: asset.AssetDir, AssetInfo: asset.AssetInfo, Prefix: "template", Fallback: "index.html"}

其中:

Prefix: "template" 是為了在查詢文件時去除 template 前綴,例如原文件路徑為 "template/css/app.ee8ee5dd.css" => /css/app.ee8ee5dd.css 方便和前端請求對應

Fallback: "index.html" 意思為如何查詢不到則默認返回 index.html 文件,因為配置了前綴,這里返回的應該是 template/index.html

5. 日常開發

在日常開發中,沒有必要沒改動一下靜態文件就要重新生成 asset.go,此時我們可以使用 -debug 模式生成 asset.go 文件,這樣訪問文件還是使用的真實文件

go-bindata-assetfs -debug -o=asset/asset.go -pkg=asset template/... conf/...

總結

通過 go-bindatago-bindata-assetfs 的使用,我們可以將靜態文件進行打包,最終提供單個分發文件,簡化部署和使用。

題外

提一下 go-bindata 項目之前的一些周折。

如果你搜索 go-bindata 的文章,會發現早期的文章指向的項目地址往往是:https://github.com/jteeuwen/go-bindata 。那是最早的項目地址,jteeuwen 是原作者 Jim Teeuwen 的賬號。

但不知道什么時候,因為什么原因,原作者把項目關閉了,連 jteeuwen 這個賬號都刪除了。(從現存線索推斷,大約是 2018 年的事)

現在原地址也有一個項目,但已經 不是原項目 ,也 不再維護 了。那是有人發現 go-bindata 刪除后,為了讓依賴它的項目不會報錯,重新注冊了 jteeuwen 這個賬號,重新 fork 了這個項目 (真正原項目已刪,是從一個 fork 那里 fork 的) 。因為初衷是讓某個項目能夠繼續工作(據說是已經沒法修改的私人項目,所以也不能指向新的地址),並沒有打算繼續維護,也不想冒充原項目,所以這個項目設為了 archived (read only)。詳情可以參考以下討論:

https://github.com/jteeuwen/go-bindata/issues/5

https://github.com/jteeuwen/discussions/issues

現在給出的項目地址,不確定跟原作者有沒有關系——估計是沒有的。那它不過是眾多 fork 的其中一個。選它僅僅因為它最活躍、關注人數最多。這可能跟它掛在了同名 organization 下有一定關系,也可能里面有某個大牛。

理由並不重要,只需要知道它最活躍是一個共識,就夠了。

題外這段引用 https://jaycechant.info/2020/go-bindata-golang-static-resources-embedding/

參考


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM