require 'rubygems'
require 'thin'
require "action_controller"
require 'dispatcher'
# Rails pwned
ActionController::Base.session = { :session_key => "_myapp_session", :secret => "some secret phrase of at least 30 characters" }
ActionController::Dispatcher.unprepared = false
Dependencies.mechanism = :require
ActionView::Base.cache_template_loading = true
def routes(&block)
ActionController::Routing::Routes.draw do |map|
map.instance_eval(&block)
end
end
def session(key, secret)
ActionController::Base.session = { :session_key => key, :secret => secret }
end
class ActionController::Dispatcher
def prepare_application
end
end
def controller(name, &block)
# And this my friend, is called fucking around.
ActionController::Routing.controller_paths << name
klass = Object.const_set("#{name.camelize}Controller", Class.new(ActionController::Base))
klass.class_eval(&block)
end
# Me wants views
ActionController::Base.view_paths = [File.join(File.dirname(__FILE__), "views")]
# Make Thin Happy
class LifoAdapter
def initialize(options={})
@file_server = Rack::File.new(::File.join("public"))
end
# TODO refactor this in File#can_serve?(path) ??
def file_exist?(path)
full_path = ::File.join(@file_server.root, Rack::Utils.unescape(path))
::File.file?(full_path) && ::File.readable?(full_path)
end
def serve_file(env)
@file_server.call(env)
end
def call(env)
path = env['PATH_INFO'].chomp('/')
cached_path = (path.empty? ? 'index' : path) + ActionController::Base.page_cache_extension
if file_exist?(path) # Serve the file if it's there
serve_file(env)
elsif file_exist?(cached_path) # Serve the page cache if it's there
env['PATH_INFO'] = cached_path
serve_file(env)
else
rack_response = Rack::Response.new
rack_request = Rack::Request.new(env)
cgi = Rack::Adapter::Rails::CGIWrapper.new(rack_request, rack_response)
ActionController::Dispatcher.dispatch(cgi, ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS, rack_response)
rack_response.finish
end
end
end
def start
ActionController::Routing.use_controllers! ActionController::Routing.controller_paths
use Rack::CommonLogger
run LifoAdapter.new
end