在Go程序中捆绑静态资源的最佳方式是什么? [英] What's the best way to bundle static resources in a Go program?

查看:79
本文介绍了在Go程序中捆绑静态资源的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Go中的一个小型Web应用程序,该应用程序旨在用作开发人员计算机上的工具来帮助调试其应用程序/ Web服务。程序的接口是一个不仅包含HTML,还包含一些JavaScript(用于功能),图像和CSS(用于样式)的网页。我正计划开源这个应用程序,所以用户应该能够运行一个Makefile,并且所有的资源都将到达他们需要去的地方。但是,我也希望能够尽可能少地分发可执行文件/依赖关系。 是否有将HTML / CSS / JS与可执行文件捆绑在一起的好方法,因此用户只需要下载并担心一个文件?




现在,在我的应用程序中,提供静态文件看起来有点像这样:

 调用func switchboard(w http.ResponseWriter,r * http.Request){

//剪切动态路由...

//寻找静态资源
uri:= r.URL.RequestURI()
if fp,err:= os.Open(static+ uri); err == nil {
defer fp.Close()
staticHandler(w,r,fp)
return
}

//剪断黑洞路由





$ b

所以非常简单:如果请求的文件存在于我的静态目录中,处理程序,它只是打开文件并尝试在投放前设置好的 Content-Type 。我的想法是,没有理由需要基于真正的文件系统:如果有编译资源,我可以简单地通过请求URI为它们编制索引并将其作为它们提供。



如果没有一个好的方法来做到这一点,或者我试图做到这一点就吠叫错误的树,让我知道。我只是想,最终用户会喜欢尽可能少的文件来管理。



如果有比 go ,请随时添加或让我知道。

解决方案

go-bindata包看起来可能是您感兴趣的内容。



https://github.com/go-bindata/go-bindata



它可以让你将任何静态文件转换为一个可以嵌入代码的函数调用,并在调用时返回文件内容的一个字节片段。

I'm working on a small web application in Go that's meant to be used as a tool on a developer's machine to help debug their applications/web services. The interface to the program is a web page which includes not only the HTML, but some JavaScript (for functionality), images and CSS (for styling). I'm planning on open-sourcing this application, so users should simply be able to run a Makefile and all the resources will go where they need to go. However, I'd also like to be able to simply distribute an executable with as few files/dependencies as possible. Is there a good way to bundle the HTML/CSS/JS with the executable, so users only have to download and worry about one file?


Right now, in my app, serving a static file looks a little like this:

// called via http.ListenAndServe
func switchboard(w http.ResponseWriter, r *http.Request) {

    // snipped dynamic routing...

    // look for static resource
    uri := r.URL.RequestURI()
    if fp, err := os.Open("static" + uri); err == nil {
        defer fp.Close()
        staticHandler(w, r, fp)
        return
    }

    // snipped blackhole route
}

So it's pretty simple: if the requested file exists in my static directory, invoke the handler, which simply opens the file and tries to set a good Content-Type before serving. My thought was that there's no reason this needs to be based on the real filesystem: if there were compiled resources, I could simply index them by the request URI and serve them as such.

If there's not a good way to do this, or I'm barking up the wrong tree by trying to do this, let me know. I just figured the end-user would appreciate as few files as possible to manage.

If there are more appropriate tags than , please feel free to add them or let me know.

解决方案

The go-bindata package looks like it might be what you're interested in.

https://github.com/go-bindata/go-bindata

It will allow you to convert any static file into a function call that can be embedded in your code and will return a byte slice of the file content when called.

这篇关于在Go程序中捆绑静态资源的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆