0

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?

Dmitri Goldring
  • 4,176
  • 2
  • 24
  • 28
CrazyCrow
  • 4,125
  • 1
  • 28
  • 39
  • 1
    One of the reasons go veterans get so frustrated by questions about these "easy" frameworks is questions like this. It's simple and obvious if you aren't using martini. In fact, supporting range requests, conditional get, compression negotiation, etc... requires almost no effort on your part. As an observer, these frameworks only seem to make things harder and less efficient. – Dustin Mar 09 '14 at 17:18
  • @Dustin that is generally true, but still some stuff is missing. Look at Gorilla for example. They added automatic schema parsing. That's extremely useful. I've personally extended it to add input validation (they didn't want it as part of gorilla.schema and I needed it), but what I like about Gorilla is that you simply extend the standard http lib a bit - only as much as you need. – Not_a_Golfer Mar 09 '14 at 22:49
  • gorilla is very much not a framework, but some packages to add functionality when you need it. – Dustin Mar 11 '14 at 02:18

1 Answers1

2

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

Not_a_Golfer
  • 47,012
  • 14
  • 126
  • 92