Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando committed Nov 11, 2011
0 parents commit a59765c
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Gemfile
@@ -0,0 +1,7 @@
source :rubygems

gem "rack"
gem "rack-mount"
gem "rack-contrib"
gem "thin"
gem "activesupport"
13 changes: 13 additions & 0 deletions app.rb
@@ -0,0 +1,13 @@
require "rack"

class MyApp
def call(env)
[
200,
{"Content-Type" => "text/plain"},
["Hello Rackers!"]
]
end
end

Rack::Handler::Thin.run MyApp.new, :Port => 1234
25 changes: 25 additions & 0 deletions builder.rb
@@ -0,0 +1,25 @@
require "rack"

class MyApp
def call(env)
[
200,
{"Content-Type" => "text/plain"},
["Hello Rackers!"]
]
end
end

app = Rack::Builder.app do
use Rack::ContentLength

map "/codeplane" do
run MyApp.new
end

map "/howto" do
run MyApp.new
end
end

Rack::Handler::Thin.run app, :Port => 1234
14 changes: 14 additions & 0 deletions config.ru
@@ -0,0 +1,14 @@
class MyApp
def call(env)
[
200,
{"Content-Type" => "text/plain"},
["Hello Rackers!"]
]
end
end

use Rack::ContentLength
use Rack::Lint

run MyApp.new
136 changes: 136 additions & 0 deletions fu.rb
@@ -0,0 +1,136 @@
require "rack"
require "rack/mount"
require "rack/contrib"
require "active_support/hash_with_indifferent_access"

class FU
class App
attr_reader :env
attr_accessor :callback

def initialize(callback)
@callback = callback
end

def request
@request ||= Rack::Request.new(env)
end

def params
@params ||= begin
params = request.params || {}
params.merge!(request.env["rack.request.form_hash"] || {})
params.merge!(request.env["rack.routing_args"] || {})
ActiveSupport::HashWithIndifferentAccess.new(params)
end
end

def call(env)
@env = env
instance_eval(&callback)
end
end

def self.app(&block)
new(&block).to_app
end

def initialize(&block)
instance_eval(&block)
end

def middlewares
@middlewares ||= []
end

def get(path, &block)
match(path, "GET", &block)
end

def post(path, &block)
match(path, "POST", &block)
end

def put(path, &block)
match(path, "PUT", &block)
end

def delete(path, &block)
match(path, "DELETE", &block)
end

def head(path, &block)
match(path, "HEAD", &block)
end

def match(path, method = nil, &block)
conditions = {path_info: compile_path(path)}
conditions[:request_method] = method if method

builder = Rack::Builder.new
builder.use Rack::ETag
builder.use Rack::ContentLength
builder.use Rack::MethodOverride
builder.use Rack::NestedParams

middlewares.each do |middleware, *args|
builder.use middleware, *args
end

app = App.new(block)
builder.run(app)

route_set.add_route(builder, conditions)
end

def use(middleware, *args)
middlewares << [middleware, args]
end

def to_app
route_set.freeze
end

private
def route_set
@route_set ||= Rack::Mount::RouteSet.new
end

def compile_path(path)
Rack::Mount::Strexp.compile Rack::Mount::Utils.normalize_path(path), {}, %w[/.?]
end
end

app = FU.app do
use Rack::Runtime

get "/" do
name = params.fetch(:name, "Rackers")

[
200,
{"Content-Type" => "text/html"},
["Hello #{name}!"]
]
end

get "/:name" do
[
200,
{"Content-Type" => "text/html"},
["Hello #{params[:name]}!"]
]
end

post "/" do
name = params.fetch(:name, "Rackers")

[
200,
{"Content-Type" => "text/html"},
["Hello #{name}! Post data sent!"]
]
end
end

Rack::Handler::Thin.run app, :Port => 1234
13 changes: 13 additions & 0 deletions public/index.html
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="/style.css" type="text/css">
</head>

<body>
<h1>Hello Rackers!</h1>
<script type="text/javascript" src="/script.js"></script>
</body>
</html>
1 change: 1 addition & 0 deletions public/script.js
@@ -0,0 +1 @@
document.write("<h2>#fuckyeah!</h2>");
7 changes: 7 additions & 0 deletions public/style.css
@@ -0,0 +1,7 @@
body {
background: blue;
color: yellow;
font-family: sans-serif;
font-size: 36px;
text-align: center;
}
13 changes: 13 additions & 0 deletions static.rb
@@ -0,0 +1,13 @@
require "rack"

app = Rack::Builder.new do
use Rack::Static,
urls: {"/" => "index.html"},
root: "public"

run Rack::URLMap.new({
"/" => Rack::Directory.new("public")
})
end

Rack::Handler::Thin.run app, :Port => 1234

0 comments on commit a59765c

Please sign in to comment.