rack

Where do you put your Rack middleware files and requires?

蓝咒 提交于 2019-11-28 17:26:05
I'm in the process of refactoring some logic built into a Rails application into middleware, and one annoyance I've run into is a seeming lack of convention for where to put them. Currently I've settled on app/middleware but I could just as easily move it to vendor/middleware or maybe vendor/plugins/middleware ... The biggest problem is having to require the individual files at the top of config/environment.rb require "app/middleware/system_message" require "app/middleware/rack_backstage" or else I get uninitialized constant errors on the config.middleware.use lines. That could get messy very

How to dump an HTTP request from within Sinatra?

断了今生、忘了曾经 提交于 2019-11-28 17:08:49
问题 Is there a way to dump all incoming requests to a Sinatra application in the exact way the application receives the data? Maybe some sort of Rack middleware? 回答1: I run thin with the -D and -V flags when I want to debug 'things': $ thin start -p 3000 -R config.ru -D -V -D, --debug Set debbuging on -V, --trace Set tracing on (log raw request/response) If you are trying to get the raw output from a request, use the request method like: # app running on http://example.com/example get '/foo' do

Mount Sinatra app inside a rails app and sharing layout

喜欢而已 提交于 2019-11-28 16:24:49
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 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: class MySinatraApp < Sinatra::Base get "/" do "Hello Sinatra World" end end The second step now is to tell your

Can't get rack-cors working in rails application

笑着哭i 提交于 2019-11-28 16:24:40
I wanted to implement CORS in my rails application, so I googled rack-cors gem for it. And I did everything as was said in README, that is updated Gemfile accordingly and updated application.rb like this: module YourApp class Application < Rails::Application # ... config.middleware.use Rack::Cors do allow do origins '*' resource '*', :headers => :any, :methods => [:get, :post, :options] end end end end But it didn't work. No matter what I did, in the browser console I kept getting message: XMLHttpRequest cannot load https://somewebsite.com. Origin http://0.0.0.0:3000 is not allowed by Access

How to redirect without www using Rails 3 / Rack?

倖福魔咒の 提交于 2019-11-28 16:22:01
I understand there are a lot of questions that answer this. I'm familiar with .htaccess and nginx.conf methods, but I do not have access to such traditional configuration methods on heroku. Simone Carletti gave this answer that leverages Rails 2.x Metals, but I'm using Rails 3 and this isn't compatible. Redirect non-www requests to www urls in Rails Please note: I'm not looking for a simple before_filter in my ApplicationController. I'd like to accomplish a rewrite similar to Simone's. I believe this is job for the webserver or middleware like Rack at the very least, so I'd like to leave this

Use different Procfile in development and production

不问归期 提交于 2019-11-28 15:28:57
问题 I have a homemade Sinatra application for which I intend to use Heroku to host it. I use foreman and shotgun in development, with the following Procfile: web: shotgun config.ru -s thin -o 0.0.0.0 -p $PORT -E $RACK_ENV It works great with both development and production. But the thing is, I don't want to use shotgun in production since it's too slow. Can we use separate Procfile configurations for both dev and prod? 回答1: You could use two Procfiles (e.g. Procfile and Procfile.dev ) and use

Testing Rack Routing Using rSpec

余生颓废 提交于 2019-11-28 10:35:10
I have a rule in my routes.rb: constraints AssetRestrictor do match '*seopath' => SeoDispatcher end Then in lib/seo_dispatcher.rb, I have this: class SeoDispatcher AD_KEY = "action_dispatch.request.path_parameters" def self.call(env) seopath = env[AD_KEY][:seopath] if seopath params = seopath.split('/') # get array of path components env[AD_KEY][:id] = params.last # the real page name is the last element env[AD_KEY][:category] = params.first if params.length > 1 end Rails.logger.debug "routing to show #{env[AD_KEY]}" PagesController.action(:show).call(env) # TODO error handling for invalid

How can I use views and layouts with Ruby and ERB (not Rails)?

萝らか妹 提交于 2019-11-28 06:27:29
问题 How can I use views and layouts with Ruby and ERB (not Rails)? Today i'm using this code to render my view: def render(template_path, context = self) template = File.read(template_path) ERB.new(template).result(context.get_binding) end This works very well, but how can I implement the same function, but to render the template inside a layout? I want to call render_with_layout(template_path, context = self), and so that it will have a default layout. 回答1: Since you tagged it with Sinatra I

How do I access the Rack environment from within Rails?

大憨熊 提交于 2019-11-28 04:31:21
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 env["rack.session"] ||= {} env["rack.session"]["hello"] = "world" # sticks around for duration of

Any success with Sinatra working together with EventMachine WebSockets?

半腔热情 提交于 2019-11-28 02:57:39
I have been using Sinatra for sometime now and I would like to add some realtime features to my web-app by pushing the data via websockets. I have successfully used the gem 'em-websocket' on its own, but have not been able to write one ruby file that has a sinatra web server AND a web-socket server. I've tried spinning the run! or start! methods off in separate threads with no success. Has anyone gotten this to work? I want to have them in the same file as I can then share variables between the two servers. Thanks! Konstantin Haase Did not try it, but should not be too hard: require 'em