Archive Page 2

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!

Thin : A Followup!

Thin was launched a week ago and it instantly became a huge success in the Ruby community!

  • Someone created the #thin IRC channel on freenode where around 7-10 ppl hang around
  • Lots of people are active on the Google Group, asking questions and submitting patches (more then 50 members already!)
  • Thin was featured on Ruby Inside blog
  • and on the Rails Envy podcast in which Jason and Gregg are having a hard time saying my name
  • Joao Pedrosa switched to Thin on his blog
  • Daniel Fisher had a great review
  • Mat, Heri and Carl said some very nice words about it too
  • Thin support was added to Ramaze and Vintage frameworks (Kevin William also submitted a patch to Merb but has not been accepted yet)
  • A couple of days ago, the LOLCAT release of Thin fixed a couple of bugs and introduced Ruby 1.9 support under which Thin is even faster!

Also Julie Hache, asked me to present Thin at the upcoming DemoCampCUSEC2 and I accepted with pleasure! See you there!

Thin - A fast and simple web server

ThinThin is a web server that glues together 3 of the best Ruby libraries in web history:

  • the Mongrel parser: the root of Mongrel speed and security
  • Event Machine: a network I/O library with extremely high scalability, performance and stability
  • Rack: a minimal interface between web servers and Ruby frameworks

Which makes it, with all humble humility, the most secure, stable, fast and extensible Ruby web server ever built, bundled in an easy to use gem for your own pleasure.

Why another web server ?

Thin started as an experiment to build an 100% Ruby singled threaded server which ended up being as fast as Mongrel on some cases (like handling Rails request). But that was not enough. Using the EventMachine library the performance and stability are now more impressive then a 500 pounds guy standing on one hand and juggling with the other.

It’s less then 500 lines of clean and fully tested Ruby code plus the customized Mongrel parser (written in C).

Faster then Mongrel ? Yeah right !

Yes it is! It’s even faster then the patched version of Mongrel that uses EventMachine (aka Evented Mongrel).
Request / seconds comparison

Try it

sudo gem install thin

Then in your Rails application directory:

thin start

You can also use it with any framework that support Rack. More info on the usage page.

More alpha then the Greek letter

This is the first experimental release. If you use this on your production server right now you are stupid, looking for extreme sensations or trying to find a way to get fired.

If you’d like to help, have fun or report a bug, join me in the project Google Group and get the code:

git clone http://code.macournoyer.com/git/thin.git

Stay tuned for updates and releases!

If you care, Digg it or Reddit it !

Has Zed jumped the shark ?

Zed ShawZed Shaw, the creator of Mongrel, is writing some pretty nasty stuff about the Rails community, ThoughWorks and Ruby.

There’s no work for a smart man in a town full of stupid.

Although I agree there might be some stupid people working with Rails right now, there’s a huge number of very bright people too. And there’s stupid people working with PHP, Java, Python, C, Haskell, Erlang, Lua and Factor too.

The thing is: Rails is getting easier to use every day, lowering the barrier of entry for new coders. The Rails community is growing beginner level coders a lot faster then expert coders now. A year ago, it was probably the opposite. Alpha geeks like being credited for doing hard stuff, solving impossible puzzle, and making code run faster then you could think possible. But Rails is so standardized now, there’s little place for innovation. So new coders to make a name in that community by changing the game again. It was easy before, nothing was there, everything was to be done!

Congratulations because all the idiots who paid ThoughtWorks 6x times salary for junior ass wipes got taken and simply paid to train ThoughtWorks’ new crew

And maybe ThoughWorks is taking advantage of Rails popularity, but it will happen for other cool framework/language too if they get some day to that level of popularity. There’s no reason to shit on the Rails community for this.

After revitalizing myself, getting out of the Rails business (or trying), distancing myself from Mongrel, disconnecting from the Ruby community

Is this the end of Mongrel ?

Zed rant was pretty excessive and even though he claims that everything he writes on his blog is an act, I’m sure he’ll regret this as this will get quoted out of that so-called humorous context.

Strange way to start the new year!

What’s your thoughts on this ?

Moron no more : I can Git !

Since Linus said we’re all morons ’cause we use Subversion and the like, lots of people started to migrate to his new faster, better, stronger, cooler and geekier revision control system Git.

Moron

Installation for Subversion morons like me

Get the latest stable source from http://git.or.cz/

wget http://kernel.org/pub/software/scm/git/git-1.5.3.7.tar.gz
tar xzf git-1.5.3.7.tar.gz
cd git-1.5.3.7.tar.gz
make prefix=/usr/local all
sudo make prefix=/usr/local install

Create your own little repo

mkdir moron
cd moron
git init

Congrats, you just created your first Git repo!

Most common commands looks like Subversion ones. add, rm, mv and commit. But you gotta remember that there’s no such thing as a working copy in Git. It’s a distributed revision control system. Which means that your local copy is a repository. In fact, with Git you do not checkout (in the Subversion sense) a copy of the repo, you clone it. That’s why Git is so much faster. Most of the commands: history browsing, local commits, reverting, etc, are done locally, without talking to the server. Check the official guide for Subversion morons.

Try adding a file:

echo "I'm no longer a moron Linus" > moron-no-more.txt
git commit -a

The -a ensures that all new files are added, you can do this manually with the git add command.
You then commit you change as you would do in Subversion, with the difference that it’s all done locally:

git commit -m "Take that Linus!"

All is not lost for us poor Subversion mortals

The cool thing about Git is it has builtin support for playing nicely with Subversion. Which ease the migration and can even let you use Git side by side with Subversion. So you can import a Subversion branch like this:

git svn init http://repo/url/trunk
git svn fetch

You Git repo now has all the revisions of you Subversion branch. Try running git log.
You can even send you changes back to Subversion:

git svn dcommit

Create your central repo

mkdir mah-repow.git
cd mah-repow.git
git --bare init-db
git-repo-config remotes.origin.url http://yourhostname.com/git/mah-repow.git/; # notice trailing slash!
git-update-server-info

Upload that directory to your public server. Like Subversion, Git supports various ways of serving a remote repo, I chose WebDAV because it’s the simpler one on DreamHost.

To send your changes to the server with Git you push your changes to the server and pull changes from others. With our previous examples, to push a series of commits to the server you’d do:

git repo-config remotes.origin.url http://yourhostname.com/git/mah-repow.git/; # once to setup the default server url
git push

Happy Gitting!

yu can has a mery Crismus

Image

It beez dat tiem of teh yeer agin, lol
Hai! Kittehs luv Crismus Trees very much! Dey liek to play wit dem, an eet dem, an mak tinsul hang out of der butt. But we mus remembah teh troo reesun we haf Crismus is cuz Ceiling Cat luv pien trees and he mak a holaday for dem. Srsly.
See dis piktur? Ceiling Cat in high plases cuz dat be wer he belong, srsly. Ceiling Cat don haf to stay in teh tree tho! He can has plases everywhar! Ceiling Cat can has part in ur Crismus? Uplaod yer pikturs or send dem to his proffet an show everywun wer Ceiling Cat hangz out in yer holiday decerashuns! Invisible sanity!
Yu can has a mery Crismus!

(WTF?)

If you’re looking for something to do this holidays, I’d suggest renewing with The Bible.

Merry Christmas and happy new year to all my readers.

Getting TextMate ready for Rails 2.0

After updating a couple of existing Rails app to 2.0, I found out some of TextMate sugar was out of date. Here’s 2 things I did to put the hot and sweet sugar back into TextMate.

Update the bundle

TextMate bundles are under heavy development all the time, get the latest version from their repo:


svn export http://macromates.com/svn/Bundles/trunk/Bundles/Ruby%20on%20Rails.tmbundle

Then double-click on the downloaded Ruby on Rails.tmbundle.

Fix the test commands

I’m a heavy user of Run (command+R), Run Focused Unit Test (shift+command+R) and the Rails menu (shift+ctrl+\) but that do not work with Rails 2 because of a conflict with the builder module.

Rob Sanheim explain how to solve this very easily, just rename the Builder.rb in the Rails bundle.

Hope this helps!

Rack, the frameworks framework

RackWith all the Ruby frameworks popping up, we’re starting to see some similarities. All of them provide something new or unique but one part of their code is always the same. The part that plugs it into a web server. Ultimately, all web servers have to support all frameworks and vice-versa. That is a lot of duplicated code! That makes me yell, running in circles, waving my arms: not DRY, not DRY, not DRY!

What?

Rack (not the iRack) by Christian Neukirchen, solves this problem.

From Rack website:

Rack provides an minimal interface between webservers supporting Ruby and Ruby frameworks.

Rack looks like this in my head:
Rack

Handlers on the left (web servers) serve requests to Adapters (frameworks) on the right.
(Don’t ask me what the rabbit is doing there, must be important)

Run Forest!

But Rack can do a lot more then plug Ruby framework into webservers. It is a framework in itself.

When you install it:

sudo gem install rack

You get the rackup command. Which lets you start your app on any supported web server.


require rack/lobster

run Rack::Lobster.new

- config.ru

Start your app: rackup -p3000 then browse to http://localhost:3000, oh the cute lobster!

To run it on other web servers (mongrel by default) play with the -s option.

It’s a framework I said

The Handler API is only 1 method: call(env). Which allows you to use proc as application.


app = proc do |env|
  [200, { 'Content-Type' => 'text/html' }, 'no pepper plz!']
end
run app

- config.ru

That will return a 200 OK response with the text/html content type and the body no pepper plz!. env contains the request parameters, so you could play with the QUERY_STRING or wrap it inside a Rack::Request and get a request param value with request.params['name'].

Plug

You can use middlewares to filter the process. For example, you can add logging and detailed exception support with those 2 lines in your config.ru file:


use Rack::CommonLogger, STDOUT
use Rack::ShowExceptions

You can also validate your application, the requests and responses according to the Rack spec automatically using the Rack::Lint middleware.

(There’s also a Reloader, Static, Cascade, File and much more middlewares, check the doc)

You can also have fun with urls:


map /files do
  run Rack::File.new(.)
end

map /lobster do
  run Rack::Lobster.new
end

Check it out!

Rack is the best example of a very well design Ruby library. The code is simple, well separated and yet, easily extensible, it’s beautiful! I encourage you to check out the code but mostly to use it if you’re building any Ruby web framework (like we need other one!).

Rontreal On Mails is torromow

Snow make use slowMontreal is white and snowy
Ruby is red and pretty
Rails is red (also) so lets party

Defensio launched

Remember my Defensio plugin ? It is used on RefactorMyCode to keep the site clean of any spam and the users happy.

Defensio(hit refresh if you don’t see the new site) is now public, so you can get an API key in seconds and get to speed with my plugin in milliseconds (or less).

Congrats to the Karabunga guys!

Here’s a screenshot of my stats window:

Defensio Stats

Pretty cool!

And you can also subscribe to a feed, so you get notifications of spam in your feed reader.

« Previous PageNext Page »


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

Blog Stats

  • 83,466 hits