Go实战--golang中使用echo嵌入静态资源(labstack/echo、GeertJohan/go.rice)
2017-11-23 12:09
906 查看
生命不止,继续 go go go !!!
使用 Go 开发应用的时候,有时会遇到需要读取静态资源的情况。比如开发 Web 应用,程序需要加载模板文件生成输出的 HTML。在程序部署的时候,除了发布应用可执行文件外,还需要发布依赖的静态资源文件。这给发布过程添加了一些麻烦。既然发布单独一个可执行文件是非常简单的操作,就有人会想办法把静态资源文件打包进 Go 的程序文件中。
参考地址:
http://fuxiaohei.me/2016/10/1/go-binary-embed-asset.html
文中提到了:
go-bindata
go.rice
esc
本片博客只会介绍go.rice,其余的会之后进行介绍的。
What’s an Embedded Resource?
An embedded resource in a application is a file that is included as part of the application. The file is not compiled, but is accessable from the code at run-time. Embedded resources can be any file type.
Languages as JAVA and C# support resources out of box. However, this is not the case for Golang. In order to emebed resource, we need to develop our own solution. Thankfully, there are couple of tools that are doing this for us.
参考地址:
http://blog.ralch.com/tutorial/golang-embedded-resources/
github地址:
https://github.com/GeertJohan/go.rice
Star: 1107
获取:
FindBox
funcation to access a particular resource bundler (directory).
The function is finding the correct absolute path for your resource files.
Embedded resource as source code
作为源码嵌入资源
命令:
生成文件:
Embedded resource as an archive
appends a resource as a zip file to already built executable
以zip的形式附加到已经存在的可执行文件
Embedded resource as an syso resource
This is experimental method that generates .syso file that is compiled by Go compiler. The following command generates the coff syso resource files per directory:
跟main.go同一级,新建一个文件夹app,放入文件file.txt
执行:
生成了 rice-box.go:
执行:
生成文件:embed_resources.exe
运行embed_resources.exe
删除app文件夹下的file.txt
浏览器访问http://localhost:1323/
可以看到file.txt文件
使用 Go 开发应用的时候,有时会遇到需要读取静态资源的情况。比如开发 Web 应用,程序需要加载模板文件生成输出的 HTML。在程序部署的时候,除了发布应用可执行文件外,还需要发布依赖的静态资源文件。这给发布过程添加了一些麻烦。既然发布单独一个可执行文件是非常简单的操作,就有人会想办法把静态资源文件打包进 Go 的程序文件中。
参考地址:
http://fuxiaohei.me/2016/10/1/go-binary-embed-asset.html
文中提到了:
go-bindata
go.rice
esc
本片博客只会介绍go.rice,其余的会之后进行介绍的。
What’s an Embedded Resource?
An embedded resource in a application is a file that is included as part of the application. The file is not compiled, but is accessable from the code at run-time. Embedded resources can be any file type.
Languages as JAVA and C# support resources out of box. However, this is not the case for Golang. In order to emebed resource, we need to develop our own solution. Thankfully, there are couple of tools that are doing this for us.
参考地址:
http://blog.ralch.com/tutorial/golang-embedded-resources/
go.rice
go.rice is a Go package that makes working with resources such as html,js,css,images,templates, etc very easy.github地址:
https://github.com/GeertJohan/go.rice
Star: 1107
获取:
go get github.com/GeertJohan/go.rice go get github.com/GeertJohan/go.rice/rice
FindBox
funcation to access a particular resource bundler (directory).
The function is finding the correct absolute path for your resource files.
// find a rice.Box templateBox, err := rice.FindBox("your-resource-directory") if err != nil { log.Fatal(err) } // get file contents as string tmpl, err := templateBox.String("your_asset.tmpl") if err != nil { log.Fatal(err) }
Embedded resource as source code
作为源码嵌入资源
命令:
rice embed-go
生成文件:
<directory-name>.rice-box.go
Embedded resource as an archive
appends a resource as a zip file to already built executable
以zip的形式附加到已经存在的可执行文件
Embedded resource as an syso resource
This is experimental method that generates .syso file that is compiled by Go compiler. The following command generates the coff syso resource files per directory:
rice embed-syso
go build -o <program> rice append --exec <program>
echo中使用go.rice
代码main.go:package main import ( "net/http" "github.com/GeertJohan/go.rice" "github.com/labstack/echo" ) func main() { e := echo.New() // the file server for rice. "app" is the folder where the files come from. assetHandler := http.FileServer(rice.MustFindBox("app").HTTPBox()) // serves the index.html from rice e.GET("/", echo.WrapHandler(assetHandler)) // servers other static files e.GET("/static/*", echo.WrapHandler(http.StripPrefix("/static/", assetHandler))) e.Logger.Fatal(e.Start(":1323")) }
跟main.go同一级,新建一个文件夹app,放入文件file.txt
执行:
rice embed-go
生成了 rice-box.go:
package main import ( "github.com/GeertJohan/go.rice/embedded" "time" ) func init() { // define files file2 := &embedded.EmbeddedFile{ Filename: "file.txt", FileModTime: time.Unix(1511406219, 0), Content: string(""), } // define dirs dir1 := &embedded.EmbeddedDir{ Filename: "", DirModTime: time.Unix(1511406219, 0), ChildFiles: []*embedded.EmbeddedFile{ file2, // "file.txt" }, } // link ChildDirs dir1.ChildDirs = []*embedded.EmbeddedDir{} // register embeddedBox embedded.RegisterEmbeddedBox(`app`, &embedded.EmbeddedBox{ Name: `app`, Time: time.Unix(1511406219, 0), Dirs: map[string]*embedded.EmbeddedDir{ "": dir1, }, Files: map[string]*embedded.EmbeddedFile{ "file.txt": file2, }, }) }
执行:
go build
生成文件:embed_resources.exe
运行embed_resources.exe
删除app文件夹下的file.txt
浏览器访问http://localhost:1323/
可以看到file.txt文件
相关文章推荐
- Go实战--golang中使用echo框架中的HTTP/2、Server Push(labstack/echo、golang.org/x/net/http2)
- Go实战--golang中使用echo框架中JSONP(labstack/echo)
- Go实战--golang中使用echo框架、MongoDB、JWT搭建REST API(labstack/echo、gopkg.in/mgo.v2、dgrijalva/jwt-go)
- Go实战--golang中使用echo框架中的cors(labstack/echo、rs/cors)
- Go实战--golang中使用echo和MySQL搭建api(labstack/echo、go-sql-driver/mysql)
- golang实战使用gin+xorm搭建go语言web框架restgo详解5.3 资源控制器
- golang实战使用gin+xorm搭建go语言web框架restgo详解5.6 控制器参数校验
- golang实战使用gin+xorm搭建go语言web框架restgo详解5.7 控制器数据响应
- Go实战--golang中使用redis(redigo和go-redis/redis这个已测试)
- Go实战--golang中使用go-spew(davecgh/go-spew)
- golang实战使用gin+xorm搭建go语言web框架restgo详解5.9 控制器controller编程示例
- Go实战--golang中使用JWT(JSON Web Token)
- golang实战使用gin+xorm搭建go语言web框架restgo详解6.1 模型M和Orm
- go实战(5):引入bootstrap,设置静态资源
- golang实战使用gin+xorm搭建go语言web框架restgo详解1.1 go语言的困境
- golang实战使用gin+xorm搭建go语言web框架restgo详解6.4 推荐编程方式
- golang实战使用gin+xorm搭建go语言web框架restgo详解1.2 我要做什么
- golang实战使用gin+xorm搭建go语言web框架restgo详解7 视图层V
- golang实战使用gin+xorm搭建go语言web框架restgo详解2 框架基本架构
- golang实战使用gin+xorm搭建go语言web框架restgo详解8 关于模板