How do I access Thin::Connection from inside rack middleware? - ruby

I would like to run set_comm_inactivity_timeout(0) on my EM Connection from inside Rack Middleware.
This will allow me to have one timeout for my upgraded web sockets and another for the rails app.
My web sockets are all first in the middleware chain so it is not wreaking havoc with Rack::Lock and such.
I see that some simply go for an infinite timeout in Thin and then inject something like Rack::Timeout after.
Is there a way to access EM::Connection or the signature of the connection from Rack middleware?

Only way to do this is monkey patch thin:
class Thin::Connection
alias :thin_process :process
def process
set_comm_inactivity_timeout(0)
thin_process
end
end

Related

Simplest method of enforcing HTTPS for Heroku Ruby Sinatra app

I have an app I created on Heroku which is written in Ruby (not rails) and Sinatra.
It is hosted on the default herokuapp domain so I can address the app with both HTTP and HTTPS.
The app requests user credentials which I forward on to an HTTPS call so the forwarding part is secure.
I want to ensure my users always connect securely to my app so the credentials aren't passed in clear text.
Despite lots of research, I've not found a solution to this simple requirement.
Is there a simple solution without changing my app to Ruby rails or otherwise?
Thanks,
Alan
I use a helper that looks like this:
def https_required!
if settings.production? && request.scheme == 'http'
headers['Location'] = request.url.sub('http', 'https')
halt 301, "https required\n"
end
end
I can then add it to any single route I want to force to https, or use it in the before filter to force on a set of urls:
before "/admin/*" do
https_required!
end
Redirect in a Before Filter
This is untested, but it should work. If not, or if it needs additional refinement, it should at least give you a reasonable starting point.
before do
redirect request.url.sub('http', 'https') unless request.secure?
end
See Also
Filters
Request Object
RackSsl::Enforcer

Sinatra + Rack::Session::Pool + Moneta

I'm using server side session handling with Moneta in my Sinatra application.
The part of my config.ru looks like that:
require 'rack/session/moneta'
use Rack::Session::Moneta do
use :Expires
adapter :Memory
end
How long does is take for sessions to expire? I couldn't find documentation for it.
I currently delete sessions with
get '/logout'
session.destroy
end
But I believe that only destroys the session cookie on the client side.
How can I find the sessions which are currently active?
There is a variable called #pool in Rack::Session. How can I access it from my Sinatra app?
You would set the expiration time when storing/accessing session keys by adding the expires: n option (set n to 0/false to have disable expiration). Here is the relevant entry in the Moneta README.

How do I set/get session vars in a Rack app?

use Rack::Session::Pool
...
session[:msg]="Hello Rack"
EDIT: The word session doesn't seem to resolve. I included the Session pool middleware in my config.ru, and try to set a variable in an ERB file (I'm using Ruby Serve) and it complains "undefined local variable or method `session'"
Thanks!
session is a method that is part of some web frameworks, for example Sinatra and Rails both have session methods. Plain rack applications don’t have a session method, unless you add one yourself.
The session hash is stored in the rack env hash under the key rack.session, so you can access it like this (assuming you’ve named the rack environment to your app env):
env['rack.session'][:msg]="Hello Rack"
Alternatively, you could use Rack’s built in request object, like this:
request = Rack::Request.new(env)
request.session[:msg]="Hello Rack"
You need to load rack::session module next probably cookie like here
http://rack.rubyforge.org/doc/classes/Rack/Session/Cookie.html
This like explains it with example.

Intercepting mail sent with the Pony gem, in a development environment

I'm using the Pony gem for ruby on rails, and am wondering if there's a way to intercept mail in the development and staging environments (any non-production environment) and send it to a controlled address, like you can do with ActionMailer.
I know with ActionMailer you just can just use an interceptor...
I personally use mailcatcher as an interceptor (I'm not sure if I'm using "interceptor" to mean the same thing you are though). Then you just need to set the default Pony.options :via_options to smtp://127.0.0.1:1025 and the mail will get sent to mailcatcher.
Just patch the mail method in the Pony object for your environment. Something like this...
class Pony
alias_method :original_mail, :mail
def mail(args)
args[:to] = 'intercept#example.com'
original_mail(args)
end
end

From Sinatra Base object. Get port of application including the base object

I have a Sinatra::Base object that I would like to include in all of my web apps. In that base class I have the configure method which is called on start-up.
I would like that configure code to 'register' that service with a centralized database. The information that needs to be sent when registering is the information on how to contact this web-service... things like host and port.
I then plan on having a monitoring service that will spin over all registered services and occasionally ping them to make sure they are still up and running.
In the configure method I am having trouble getting the port information. The 'self.settings.port' variable doesn't seem to work in this method.
a) any ideas on how to get the port? I have the host.
b) is there a sinatra plug-in that already does something like this so I don't have to write it myself? :-)
//in my Sinatra::Base code. lets call it register_me.rb
RegisterMe < Sinatra::Base
configure do
//save host and port information to database
end
get '/check_status'
//return status
end
//in my web service code
require register_me //at this point, sinatra will initialize the RegisterMe object and call configure
post ('/blah')
//example of a method for this particular web service
end
Sinatra::Application.port will return the correct port

Resources