Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Rack::Sendfile middleware #1280

Merged
merged 1 commit into from
Feb 29, 2016

Conversation

lfidnl
Copy link
Contributor

@lfidnl lfidnl commented Feb 19, 2016

Hello.

I've added support of Rack::Sendfile middleware. Before this fixRack::Sendfile does not apply strategy of adding X-Accel-Redirect (or X-Sendfile) header, because Grape returns simple Rack::Response which doesn't respond to to_path method. So I've implemented simple SendfileReponse class which responds to to_path.

@dblock
Copy link
Member

dblock commented Feb 20, 2016

Interesting. Lets start with these:

  • The test should use Rack::Sendfile, otherwise it's just expecting some contract to be fulfilled.
  • Fix build, add to CHANGELOG.

@lfidnl
Copy link
Contributor Author

lfidnl commented Feb 24, 2016

Hi! Sorry, for long response.

I wrote test spec/grape/middleware/formatter_spec.rb:286 because condition with Grape::Util::FileResponse wasn't covered. Additionaly, as I understood, I should write some integration test with using Rake::Sendfile middleware, right?

@dblock
Copy link
Member

dblock commented Feb 24, 2016

Yes, please and thank you @lfidnl. I am glad this gets cleaned up and tested.

@lfidnl
Copy link
Contributor Author

lfidnl commented Feb 24, 2016

@dblock, I've added test, update CHANGELOG and fix build. I hope, I done everything well :)

@@ -2328,6 +2328,21 @@ class API < Grape::API
end
```

For using Rack::Sendfile middleware, specify `to_path` method in your
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this is not true, no? The code change makes it that I don't have to worry about file streamers or any other thing, I can just plop Rack::Sendfile in there and it will "just work", right? Maybe just say that this is compatible with Rack::Sendfile and add a note on what that does?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dblock , thanks for comment! I mean that agrument of file dsl method should respond to to_path method (of couse use Rack::Sendfile shoule be applied in API class).
So it can be FileSteamer with realized to_path method or other object with to_path method. I uses "file streamer" notion for supporting article in the same style.
I think that adding information about Rack::Sendfile will be overkill. I can replace information with this:

For using Rack::Sendfile middleware, specify to_path method in your file streamer class which returns path of served file:

class FileStreamer
  # ...

  def to_path
    @file_path
  end

  # ...
end

Note: don't forget turn on Rack::Sendfile middleware in your API:

class API < Grape::API
  use Rack::Sendfile
end

If you want to just add Rack::Sendfile and I starts to work see my comment on below :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I get it. I think it should look like the text above, which is If you want a file-like object to be streamed using Rack::Chunked, use stream.

Maybe

If you want to take advantage of Rack::Sendfile, which intercepts responses whose body is being served from a file and replaces it with a server specific X-Sendfile header, ...

Update it as you see fit and I'll merge.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I'll update readme during for couple days

@dblock
Copy link
Member

dblock commented Feb 25, 2016

Tests are good. See my above note on README, squash commits please.

@lfidnl
Copy link
Contributor Author

lfidnl commented Feb 25, 2016

I think we must rewrite Grape's api for serving file like in Rails framework:

get '/file' do
  file '/path/to/file'
end

file method will create "file streamer" object and if Rack::Sendfile middleware turns on, it will works throurg middleware, otherwise it will stream file through Grape.
It will simplify using of Grape.

Lets create new issue or I'll try to provide propose with Pull Request after closing this PL :)

@dblock
Copy link
Member

dblock commented Feb 26, 2016

👍 on extending file to take a path

@lfidnl
Copy link
Contributor Author

lfidnl commented Feb 28, 2016

README updated

dblock added a commit that referenced this pull request Feb 29, 2016
@dblock dblock merged commit d78422d into ruby-grape:master Feb 29, 2016
@dblock
Copy link
Member

dblock commented Feb 29, 2016

Merged, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants