rack

What is the “env” variable in Rack middleware?

大憨熊 提交于 2019-12-18 14:03:08
问题 I know a Rack middleware filter is a Ruby class with an initialize and a call method. I know the call method takes an "env" argument. Something like this: class MyFilter def initialize(app) end def call(env) end end My question is: what exactly is the "env" argument sent to "call"? Is this the same as the Rails ENV environment (ie. development, testing, production). Thanks! 回答1: env is just a hash. Rack itself and various middlewares add values into it. To understand what the various keys are

rails json response with gzip compression

孤者浪人 提交于 2019-12-18 11:19:45
问题 I have an api written in rails which on each request responds with a JSON response. The response could be huge, so i need to compress the JSON response using gzip. Wondering how to do this in rails controller? I have added the line use Rack::Deflater in config.ru Should I also be changing something in the line which renders JSON? render :json => response.to_json() Also, how do i check if the response is in gzip format or not..?? I did a curl request from terminal, I see only the normal plain

rails json response with gzip compression

我怕爱的太早我们不能终老 提交于 2019-12-18 11:19:16
问题 I have an api written in rails which on each request responds with a JSON response. The response could be huge, so i need to compress the JSON response using gzip. Wondering how to do this in rails controller? I have added the line use Rack::Deflater in config.ru Should I also be changing something in the line which renders JSON? render :json => response.to_json() Also, how do i check if the response is in gzip format or not..?? I did a curl request from terminal, I see only the normal plain

How do I set up a Sinatra app under Apache with Passenger?

情到浓时终转凉″ 提交于 2019-12-18 09:59:34
问题 Let's say I have the simplest single-file Sinatra app. The hello world on their homepage will do. I want to run it under Apache with Phusion Passenger, AKA mod_rails. What directory structure do I need? What do I have to put in the vhost conf file? I understand I need a rackup file. What goes in it and why? 回答1: Basic directory structure: app |-- config.ru # <- rackup file |-- hello-app.rb # <- your application |-- public/ # <- static public files (passenger needs this) `-- tmp/ `-- restart

Sinatra and Rack Protection setting

穿精又带淫゛_ 提交于 2019-12-18 03:56:08
问题 I am using Sinatra and CORS to accept a file upload on domain A (hefty.burger.com). Domain B (fizzbuzz.com) has a form that uploads a file to a route on A. I have an options route and a post route, both named '/uploader'. options '/uploader' do headers 'Access-Control-Allow-Origin' => 'http://fizz.buzz.com', 'Access-Control-Allow-Methods' => 'POST' 200 end post '/uploader' do ... content_type :json [{:mary => 'little lamb'}].to_json end The options gets hit first... and it works.. then the

Rack::Request - how do I get all headers?

送分小仙女□ 提交于 2019-12-18 01:26:06
问题 The title is pretty self-explanatory. Is there any way to get the headers (except for Rack::Request.env[] )? 回答1: The HTTP headers are available in the Rack environment passed to your app: HTTP_ Variables: Variables corresponding to the client-supplied HTTP request headers (i.e., variables whose names begin with HTTP_). The presence or absence of these variables should correspond with the presence or absence of the appropriate HTTP header in the request. So the HTTP headers are prefixed with

Mount Sinatra app inside a rails app and sharing layout

懵懂的女人 提交于 2019-12-17 23:03:01
问题 I would like to mount a sinatra application in my rails app. But I would like this one to share the same layout. The iframe could work but do you have any other idea ? Thanks 回答1: You basically need to do two things: You need to tell the Rails router that a certain URL path is to be handled by another Rack app (in your case a Sinata app). This can be done by adding this to your routes.rb: match "/sinatra" => MySinatraApp, :anchor => false Having done that, you can create your app like so:

How do I access the Rack environment from within Rails?

蓝咒 提交于 2019-12-17 17:37:20
问题 I have a Rack application that looks like this: class Foo def initialize(app) @app = app end def call(env) env["hello"] = "world" @app.call(env) end end After hooking my Rack application into Rails, how do I get access to env["hello"] from within Rails? Update : Thanks to Gaius for the answer. Rack and Rails let you store things for the duration of the request, or the duration of the session: # in middleware def call(env) Rack::Request.new(env)["foo"] = "bar" # sticks around for one request

Sinatra/Rack `ERROR URI::InvalidURIError: bad URI(is not URI?)` on redirect

我怕爱的太早我们不能终老 提交于 2019-12-14 02:33:46
问题 I've just started my first Sinatra project, a simple TV-show management web app, and wanted to have beautiful URLs. So when a user types in the search box and submits, I don't want to have a /?search=foobar -style URL and want do redirect them to /search/foobar . This also enables me to separate the get '/search/:name route from the main get '/' route. I've implemented the redirect using a before filter and properly escaped the params[] variable: before do if params.has_key? 'search' redirect

Ruby on Rails: patching Rack and deploying it

非 Y 不嫁゛ 提交于 2019-12-14 00:38:06
问题 I need to patch Rack with the following patch: http://github.com/rack/rack/commit/dae12e088592ee69545b5f2f81b87f4959859164 What's the best practice for doing this? Should I gem unpack, apply the patch, then repack the gem and ship it with capistrano to make sure my version of the gem gets to the destination server? can I just pop rack into vendor/plugins/ and rely on it being loaded first prior to the system wide gem? I want to take the path of least resistance and easily be able to deploy