Single file Rails Application 30

Posted by pratik
on Saturday, February 16

UPDATE 1 : The inspiration for this article came from here ( it predates yo mama ). It has abso-fucking-lutely nothing to do with merb whatsoever. And “fuck you to your face” is NOT directed towards anything/anyone :)

Just for fun and profit

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
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::Routing.use_controllers! ['home']
ActionController::Dispatcher.unprepared = false
Dependencies.mechanism = :require

class ActionController::Dispatcher
  def prepare_application
  end
end

# And your routes
ActionController::Routing::Routes.draw do |map|
  map.root :controller => 'home'
  map.connect ':controller/:action/:id'
end

# Application code
class ::HomeController < ActionController::Base
  def index
    render :text => "fuck you to your face"
  end
  
  def hello
    render :text => params.inspect
  end
end

# Make Thin Happy
class LifoAdapter
  def call(env)
    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

# Run LifoAdapter
Thin::Server.start('0.0.0.0', 3000) do
  use Rack::CommonLogger
  run LifoAdapter.new
end

UPDATE 2

I just made a few more changes for funkification, which you can find at my github repository

With tinyrails, the “hello world” application looks like :

1
2
3
4
5
6
7
8
9
10
11
12
13
# thin -p 3000 -r mini.rb start

require 'tinyrails'

routes { root :controller => 'home' }

controller "home" do
  def index
    render :text => "Hello World"
  end
end

start

You can also use models/views with tinyrails, as demonstrated here

UPDATE 3 : Fabiano França changed the code to run it with webrick/mongrel. He also has a funky microrest framework. His pastie can be found here

Comments

Leave a response

  1. MartinFebruary 16, 2008 @ 10:13 AM

    Fantastic! :)

  2. PJFebruary 16, 2008 @ 10:55 AM

    I like what you’ve done here.

  3. August LilleaasFebruary 16, 2008 @ 11:57 AM

    Lol! Wannabe camping!

  4. Jacek BecelaFebruary 16, 2008 @ 01:10 PM

    I can only imagine who is the index action addressed to, but not sure I’m right ;) Care to clarify on this one?

  5. bryanlFebruary 17, 2008 @ 02:07 AM

    absolutely awesome!

  6. ChrisFebruary 17, 2008 @ 02:57 AM

    Anyone using this in production?

  7. macournoyerFebruary 17, 2008 @ 04:10 AM

    w00t!

    but Thin is always happy, what are you talking about?

  8. Dr NicFebruary 17, 2008 @ 05:31 AM

    I can smell the sweet taste of beautiful sounding money rolling in. Nice.

  9. Anil WadghuleFebruary 17, 2008 @ 06:06 AM

    Nice done man! It made me happy!

  10. Kevin MarshFebruary 17, 2008 @ 07:28 AM

    So fast.

  11. BrianFebruary 17, 2008 @ 10:18 AM

    Why do you have to do ::HomeController instead of just HomeController?

  12. PratikFebruary 17, 2008 @ 02:52 PM

    Chris, I don’t think the code is production ready yet. I still need to add some tests. But I’d really appreciate if you can probably try it in production and report back any bugs you’d encounter. Patches most welcome too !

    Brian, that is to keep const_missing magic happy.

  13. EvanFebruary 17, 2008 @ 08:05 PM

    I am worried whether it can scale.

  14. (other) ChrisFebruary 17, 2008 @ 08:13 PM

    Benchmarks please.

  15. JonFebruary 18, 2008 @ 11:26 AM

    Nice – and yes, of course it has nothing whatsoever to do with Merb :P

  16. LawrenceFebruary 18, 2008 @ 12:01 PM

    Yes, benchmarks. Thin is so January 2008. The new color is…. Ebb. ;)

  17. SurFebruary 18, 2008 @ 04:23 PM

    Its awesome man!!

  18. David MedinetsFebruary 18, 2008 @ 04:53 PM

    Why did you feel the need to have profanity in your code? What purpose is it serving?

  19. PratikFebruary 18, 2008 @ 05:12 PM

    David, it’s serving nothing but stupid humor really. I never planned to publish this code while I was working on it @ localhost. But when it worked, I just posted the code without giving it a lot of thoughts. I personally find it quite funny. That’s all.

  20. EzraFebruary 18, 2008 @ 10:06 PM

    Single file rails: Requests per second: 609.25 [#/sec] (mean) 25MB ram

    Single file merb Requests per second: 1831.09 [#/sec] (mean) 13Mb ram

    Just for fun and profit ;)

  21. PratikFebruary 18, 2008 @ 11:15 PM

    Hey…it’s a lot of fun and even more profit on my box ;-) :

    All benchmarks done using Thin and ‘ab -n 2000 -c 50’

    • Single file rails Requests per second: 804.90 [#/sec] (mean) – Using Rails gem version 2.0.2 ( too lazy to enter full path to my local edge copy ) – Includes each and every feature of Rails e.g. flash, callbacks, sessions, etc.
    • Single file merb Requests per second: 1418.09 [#/sec] (mean) – Using latest merb-core from git – My single file can be found here – I had hardcoded Thin adapter in Merb::Server.start
    • Single file Invisible : Requests per second: 3806.71 [#/sec] (mean)

    My Lesson ? Lets not compare apples to oranges

  22. WTFFebruary 19, 2008 @ 12:36 AM

    Fuckwords makes Agile Jesus™ cry. So stop it!

  23. Geoffrey GrosenbachFebruary 19, 2008 @ 07:47 AM

    This has raised the bar for all the tiny framework developers out there.

    I predict that within the next month we’ll see a web framework that fits in only half a file.

  24. Shanti BrafordFebruary 19, 2008 @ 07:48 AM

    I see what you did there.

  25. Senthil NayagamFebruary 19, 2008 @ 01:52 PM

    Rack support would be a useful thing as more and more ruby implementations are coming.

    would test it with my projects and see if I can promote it for any specific use

  26. AnupFebruary 20, 2008 @ 07:41 AM

    Awesome!

  27. remiFebruary 20, 2008 @ 07:42 AM

    @GG “half a file” ???? :P

    fucking hot. ( need I say more? )

  28. Fabiano FrançaFebruary 21, 2008 @ 05:45 PM

    Hi,

    Fantastic idea.

    I’m change the code for use of Mongrel e WEBrick. See the code in http://pastie.caboo.se/pastes/155367

  29. PratikFebruary 21, 2008 @ 08:36 PM

    Hey Fabiano, That’s awesome ! I’ll update my main post and add a link to your pastie :)

  30. FredMarch 05, 2008 @ 02:44 PM

    Looks like a new Revolution on Ruby Frameworks, and most are getting faster.

    how Awesome.

    I love ruby

Comment