public
Description: Lifo's Lab
Homepage: http://m.onkey.org
Clone URL: git://github.com/lifo/fabs.git
lifo (author)
Sun Mar 30 11:47:01 -0700 2008
commit  fae67add1d34ef576b68f10c3ed49d7fe8c52be9
tree    60ff04eb946ab258f057e0cbd8317bd73b7273ee
parent  bf9c6663b9ed869f1f2a8fd6842c6f219db9e030
fabs / rails-mini / tinyrails.rb
100644 79 lines (64 sloc) 2.294 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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