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 Comments

Filed under conference, montreal, ruby, thin

9 responses to “DemoCampCUSEC follow-up

  1. 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. no Zed was not at DemoCamp. I heard he went to the bar after the presentations, but I didn’t go.

  3. Great presentation!

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

  4. thx James!

    was it really 12 min? you timed it?

  5. 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. 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. Coding/girl

    so impressive!

  8. 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. 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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s