Created
February 4, 2013 21:25
-
-
Save simcap/4709848 to your computer and use it in GitHub Desktop.
Using only Ruby core creating a simple server (port 9292) with routes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rack' | |
class MyApplication | |
def initialize | |
@routes = [ {:pattern => "poney", :response => lambda do |env, match| | |
[ 200, {'Content-Type' => 'text/html'}, ['<html><body><h1>Poney Islandais</h1></body></html>'] ] | |
end | |
}, {:pattern => "hello", :response => lambda do |env, match| | |
[ 200, {'Content-Type' => 'text/html'}, ['Hello world'] ] | |
end | |
}] | |
end | |
def call(env) | |
@routes.each do |route| | |
match = env['REQUEST_PATH'].match(route[:pattern]) | |
return route[:response].call(env, match) if match | |
end | |
[ 404, {'Content-Type' => 'text/html'}, ['Not found'] ] | |
end | |
end | |
run MyApplication.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run it with
$ rackup server.ru