Skip to content

Instantly share code, notes, and snippets.

@takeru
Forked from woodie/README.markdown
Created March 16, 2010 04:22
Show Gist options
  • Save takeru/333639 to your computer and use it in GitHub Desktop.
Save takeru/333639 to your computer and use it in GitHub Desktop.

If we can create a DeferredDispatcher that is safe and generic, spin-up won’t be a show-stopper, and any app can take advantage of it. Adding this to appengine-rack.rb means people won’t need an additional require, but we will be stuck with it.

I have a sample app here: mwrc-demo.appspot.com

When we have app with multiple map sections (see below) that all call the same dispatcher, will the instance variables still work the way we want?

module AppEngine
module Rack
# ...
class DeferredDispatcher
def initialize args
@args = args
@rack_request_counter = 0
@rack_app = nil
end
def call env
@rack_request_counter += 1
if @rack_request_counter <= 1
return redirect_or_error(env)
else
unless @rack_app
require @args[:require]
@rack_app = Object.module_eval(@args[:dispatch]).new
if @args[:three_part]
return redirect_or_error(env)
end
end
return @rack_app.call(env)
end
end
def redirect_or_error(env)
if env["REQUEST_METHOD"].eql?("GET")
redir_url = env["REQUEST_URI"] +
(env["QUERY_STRING"].eql?("") ? "?" : "&") +
"_spinup_=#{Time.now.to_i}"
res = ::Rack::Response.new("*", 302)
res["Location"] = redir_url
res.finish
else
::Rack::Response.new("Service Unavailable", 503).finish
end
end
end
# ...
end
end
require 'appengine-rack'
AppEngine::Rack.configure_app(
:application => 'complex-app',
:precompilation_enabled => true,
:sessions_enabled => true,
:version => "1")
AppEngine::Rack.app.resource_files.exclude :rails_excludes
ENV['RAILS_ENV'] = AppEngine::Rack.environment
#map '/admin' do
# use AppEngine::Rack::AdminRequired
# run AppEngine::Rack::DeferredDispatcher.new(
# :require => File.expand_path('../config/environment', __FILE__),
# :dispatch => 'ActionController::Dispatcher')
#end
#
#map '/' do
# use AppEngine::Rack::LoginRequired
# run AppEngine::Rack::DeferredDispatcher.new(
# :require => File.expand_path('../config/environment', __FILE__),
# :dispatch => 'ActionController::Dispatcher')
#end
deferred_dispatcher = AppEngine::Rack::DeferredDispatcher.new(
:require => File.expand_path('../config/environment', __FILE__),
:dispatch => 'ActionController::Dispatcher')
map '/admin' do
use AppEngine::Rack::AdminRequired
run deferred_dispatcher
end
map '/' do
use AppEngine::Rack::LoginRequired
run deferred_dispatcher
end
require 'appengine-rack'
AppEngine::Rack.configure_app(
:application => 'simple-app',
:precompilation_enabled => true,
:sessions_enabled => true,
:version => "1")
AppEngine::Rack.app.resource_files.exclude :rails_excludes
ENV['RAILS_ENV'] = AppEngine::Rack.environment
run AppEngine::Rack::DeferredDispatcher.new(
:require => File.expand_path('../config/environment', __FILE__),
:dispatch => 'ActionController::Dispatcher')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment