Yesterday I presented Thin at DemoCampCUSEC. Things went pretty well and the other presentations were very good too.
My demo was basically re-coding a simplified version of Thin live, here’s the final result:
%w(rubygems eventmachine thin thin_parser rack).each { |f| require f }
class Connection < EventMachine::Connection
attr_accessor :app
def initialize
@parser = Thin::HttpParser.new
@data = ‘‘
@nparsed = 0
@env = {}
end
def receive_data(data)
@data << data
@nparsed = @parser.execute(@env, @data, @nparsed)
process if @parser.finished?
end
def process
status, headers, body = @app.call(@env)
body_output = ‘‘
body.each { |l| body_output << l }
send_data "HTTP/1.1 #{status} OK\r\n" +
headers.inject(‘‘) { |h, (k,v)| h += "#k: #v\r\n" } +
"\r\n" +
body_output
close_connection_after_writing
end
end
welcome_app = proc do |env|
[
200, # Status
{'Content-Type' => 'text/html'}, # Headers
[
'<html><body>',
'<h1>Welcome</h1>',
'<p>Welcome to my server!</p>', # Body
'<p><a href="/rails">My Rails app!</a></p>',
'</body></html>'
]
]
end
rails_app = Rack::Adapter::Rails.new(:root => ‘/Users/marc/projects/refactormycode‘, :prefix => ‘/rails‘)
app = Rack::URLMap.new(‘/‘ => welcome_app, ‘/rails‘ => rails_app)
EventMachine.run do
EventMachine.start_server ‘0.0.0.0‘, 3000, Connection do |con|
con.app = app
end
end
Hope you enjoyed it!









Congrats! Unfortunately, I wasn’t there…
I was also wondering if you would meet Zed Shaw. I think he’s presenting at CUSEC2 and it would be nice if you’d write about a potential conversation you’d have with him…
no Zed was not at DemoCamp. I heard he went to the bar after the presentations, but I didn’t go.
Great presentation!
I really enjoyed seeing a rails app server coded in 12 minutes.
thx James!
was it really 12 min? you timed it?
I’d read the blog posts of course, but it was very interesting to hear you comment on each part of the code as you built the app. Very cool.
hey thx daniel!
You were there? I didn’t saw you, and they kicked us out very quickly after the presentations.
I’m glad you find it interesting.
so impressive!
Marc-Andre,
Thanks again for coming out, and making a super cool presentation!
I’m sorry we had to rush everyone out after, the conference center wasn’t exactly accommodating… :/
Keep up with the great projects, you do awesome work!
thx to you Julie for organizing this, DemoCamp was a lot of fun. Sad I couldn’t make it to the main event, I’ve red it was amazing.