DemoCampCUSEC follow-up

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!

9 Responses to “DemoCampCUSEC follow-up”


  1. 1 Dan Simard January 18, 2008 at 10:53 am

    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…

  2. 2 macournoyer January 18, 2008 at 11:09 am

    no Zed was not at DemoCamp. I heard he went to the bar after the presentations, but I didn’t go.

  3. 3 James Golick January 18, 2008 at 11:32 am

    Great presentation!

    I really enjoyed seeing a rails app server coded in 12 minutes.

  4. 4 macournoyer January 18, 2008 at 11:40 am

    thx James!

    was it really 12 min? you timed it?

  5. 5 daniel January 18, 2008 at 10:04 pm

    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.

  6. 6 macournoyer January 18, 2008 at 10:46 pm

    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.

  7. 7 Coding/girl January 24, 2008 at 11:27 am

    so impressive!

  8. 8 Julie Hache January 27, 2008 at 8:02 pm

    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!

  9. 9 macournoyer January 28, 2008 at 11:29 am

    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.

Leave a Reply




RefactorMyCode.comrefactorer Recommend Me
Top Blogues page counter Add to Technorati Favorites

Blog Stats

  • 83,466 hits