Golang从内存提供静态文件 [英] Golang serve static files from memory

查看:202
本文介绍了Golang从内存提供静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于在Go中提供文件的简单问题。有很大的节省时间的FileServer处理程序,但对于我的用例,我只有2或3个文件(js和css)与我的应用程序一起使用,我不想使部署复杂化,因此不得不考虑这些问题。

您是否认为有一种简单的方法可以将这些文件构建到二进制文件中,并从那里为它们提供服务。例如,base64将文件的数据编码为常量,并从常量中为文件提供服务。这将以最简单的形式工作,但我不想经历自己做文件服务器所做的所有事情(标题,过期,MIME类型等)。那么有没有一种简单的方法可以将这些静态文件以某种形式烘焙到二进制文件中,并以这种方式为它们提供服务?

解决方案

go.rice 软件包为您处理这个问题 - 在您的二进制文件中嵌入资源,并提供一个http.FileSystem实现。


I have a quick question about serving files in Go. There is the great timesaving FileServer handler, but for my use case, I only have 2 or 3 files (js and css) that go with my app and I dont want to complicate the deployment to have to think about those.

Do you think there is an easy way to build those couple of files into the binary and serve them from there. For example base64 encode the data of the files as constants and server the files from the constants. This would work in its most simple form, but I dont want to go through the pain of doing everything that a file server does (headers, expiries, mime-types, etc) on my own. So would there be an easy way to bake those static files into the binary in some form and serve them that way?

解决方案

The "go.rice" package takes care of this for you - embedding resources in your binaries, and providing an http.FileSystem implementation.

这篇关于Golang从内存提供静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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