-
Notifications
You must be signed in to change notification settings - Fork 0
/
myrack.rb
105 lines (87 loc) · 2.12 KB
/
myrack.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
require "rack"
#require "json"
require "sequel"
DB = Sequel.sqlite "jobs.db"
handler = Rack::Handler::WEBrick
class Base
class << self
def route
@mime ||= Hash[:ico => "image/x-icon", :js => "text/javascript", :css => "text/css", :json => "application/json"]
@route ||= Hash["GET" => {}, "POST" => {},"PUT"=>{},"PATCH"=>{},"DELETE"=>{}]
end
def view(tpl)
filename = "%s.erb" % tpl
ERB.new(File.read(filename)).result(binding)
end
def json(data)
if data.respond_to?(:to_hash)
data = data.to_json
end
end
def assets(filename)
File.open filename, &:read
end
def content(content_type)
arr = content_type.to_a.flatten
headers = arr[1]
headers["Content-Type"] = @mime[arr[0].to_sym]
end
def GET(path, &block)
route["GET"][path] = block
end
def POST(path, &block)
route["POST"][path] = block
end
end
end
class MyApp < Base
GET "/" do
@records = DB[:jobs].all
view "home"
end
GET "/new" do
view "form"
end
POST "/new" do |request, headers|
DB[:jobs].insert request.params
headers["Location"] = "/"
end
GET "/favicon.ico" do |request, headers|
content "ico" => headers
assets "favicon.ico"
end
GET "/spectre.css" do |request, headers|
content "css" => headers
assets "spectre.css"
end
GET "/api" do |request, headers|
content "json" => headers
#json '{"status":0,"data":"some"}'
json Hash[:bj => 100]
end
end
class RackApp
def call(env)
dup._call(env)
end
def _call(env)
puts self.object_id
path = env["PATH_INFO"] ||= "/"
method = env["REQUEST_METHOD"]
cb = MyApp.route[method][path]
headers = {
"Content-Type" => "text/html",
}
if nil == cb
[404, headers, ["NOT FOUND"]]
else
resp = cb.call Rack::Request.new(env), headers
if headers["Location"]
[302, headers, ['']]
else
[200, headers, [resp]]
end
end
end
end
handler.run RackApp.new