英文:
Go huge file download and passing to client (proxifying)
问题
我有一个基于Martini的小应用程序,面临一个无法解决的问题。
我想添加一个应用程序功能,允许用户通过对HTTP头进行一些更改从第三方服务器获取文件。类似代理的功能。这些文件相当大(每个文件200+Mb),我希望这些文件能够“流式传输”到客户端。所谓“流式传输”,是指文件数据在应用程序接收到第一批字节后立即返回给客户端,而不需要将整个文件保存在磁盘和/或内存中。
在Martini和Go中是否可能实现这一点?
英文:
I have a small Martini-based application and am faced with an issue that I can't solve.
I want to add an application feature that would allow the user to get files from a 3rd server with some changes in HTTP headers. Some kind of proxy. The files are quite big (200+Mb each) and I want these files to be "streamed" to the client. By "stream" I mean that file data should start to return to the client right after the first bytes were recieved by the application without keeping the whole file on disk and/or in memory.
Is this possible with Martini and Go?
答案1
得分: 2
是的,一般情况下使用Go是可以实现的,但我对Martini并不太熟悉。调用远程文件后的HTTP响应返回一个Reader
接口,而你的请求处理程序有一个Writer
接口。这意味着你可以读取数据流并写入数据流。你只需要处理你想要的数据,并将转发的数据流“补丁”到请求流中。
Go甚至在标准库中内置了一个ReverseProxy
实用程序:
http://golang.org/pkg/net/http/httputil/#ReverseProxy
如果你愿意,你可以混合使用Martini和标准的http库。
[编辑] 根据Martini文档的阅读,你可以像标准库一样添加原始的http处理程序,这意味着你确实可以这样做:https://github.com/codegangsta/martini#service-injection
英文:
Yes, it is possible in general with Go, I'm not that familiar with Martini specifically. The http response from calling the remote file returns a Reader
interface, and your request handler has a Writer
interface. This means you can read a stream of data, and write a stream of data. Making your responsibility only to manipulate what you want, and "patch" the forwarded stream to the request stream.
Go even has a ReverseProxy
utility built into the standard library:
http://golang.org/pkg/net/http/httputil/#ReverseProxy
You can probably mix Martini and the standard http library if you want.
[EDIT] Reading the martini docs, you can add raw http handlers like the standrad library has, meaning you can indeed do that: https://github.com/codegangsta/martini#service-injection
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论