贴吧网站开发需求分析,北京网站建设V芯ee8888e,重庆网站备案流程,平躺设计家官网现在的很多程序都会提供一个 Dashboard 类似的页面用于查看程序状态并进行一些管理的功能#xff0c;通常都不会很复杂#xff0c;但是其中用到的图片和网页的一些静态资源#xff0c;如果需要用户额外存放在一个目录#xff0c;也不是很方便#xff0c;如果能打包进程序发…现在的很多程序都会提供一个 Dashboard 类似的页面用于查看程序状态并进行一些管理的功能通常都不会很复杂但是其中用到的图片和网页的一些静态资源如果需要用户额外存放在一个目录也不是很方便如果能打包进程序发布的二进制文件中用户下载以后可以直接使用就方便很多。
最近在阅读 InfluxDB 的源码发现里面提供了一个 admin 管理的页面可以通过浏览器来执行一些命令以及查看程序运行的信息。但是我运行的时候只运行了一个 influxd 的二进制文件并没有看到 css, html 等文件。
原来 InfluxDB 中使用了 statik 这个工具将静态资源都编译进了二进制文件中这样用户只需要运行这个程序即可而不需要管静态资源文件存放的位置。
go get -d github.com/rakyll/statik go install github.com/rakyll/statik
首先要声明一般在 main.go 文件中这样直观一些
package main//go:generate statik -src../../web/public -dest../../internal -ffunc main() {}这段声明的意思是使用 statik 命令将目录../../web/public下的文件和目录编译到一个go文件中此文件为../../internal/statik/statik.go会自动创建之。
go:generate指令需要手动执行来到根目录下go generate ./…
statik.go文件内容为
// Code generated by statik. DO NOT EDIT.package statikimport (github.com/rakyll/statik/fs
)func init() {data : ..............fs.Register(data)
}将文件信息注册到 fs 之后就可以实例化出来一个 http.FileSystem 对象
var statikFS http.FileSystemstatikFS, err fs.New()然后就可以指定文件名来读取文件内容了
file, err : statikFS.Open(/index.html)这里返回的 file 类型是http.File它服务于文件系统 FileSystem 因此它是只读的没有提供写的接口。
// A File is returned by a FileSystems Open method and can be
// served by the FileServer implementation.
//
// The methods should behave the same as those on an *os.File.
type File interface {io.Closerio.Readerio.SeekerReaddir(count int) ([]fs.FileInfo, error)Stat() (fs.FileInfo, error)
}在vue项目中我们只需要返回 index.html 的内容即可具体实现如下
m.Get(/, func(ctx *macaron.Context) {file, err : statikFS.Open(/index.html)if err ! nil {logger.Error(读取首页文件失败: %s, err)ctx.WriteHeader(http.StatusInternalServerError)return}io.Copy(ctx.Resp, file)})当然 public 目录中的应该是打包好的 vue 项目。