Skip to content

Instantly share code, notes, and snippets.

@simcap
Created February 4, 2013 21:25
Show Gist options
  • Save simcap/4709848 to your computer and use it in GitHub Desktop.
Save simcap/4709848 to your computer and use it in GitHub Desktop.
Using only Ruby core creating a simple server (port 9292) with routes
#!/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
@simcap
Copy link
Author

simcap commented Feb 4, 2013

Run it with

$ rackup server.ru

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment