forked from fnando/howto-rack
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a59765c
Showing
9 changed files
with
229 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
source :rubygems | ||
|
||
gem "rack" | ||
gem "rack-mount" | ||
gem "rack-contrib" | ||
gem "thin" | ||
gem "activesupport" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
document.write("<h2>#fuckyeah!</h2>"); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
body { | ||
background: blue; | ||
color: yellow; | ||
font-family: sans-serif; | ||
font-size: 36px; | ||
text-align: center; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |