Repository navigation
Expand file tree
/
Copy pathconfig.ru
More file actions
64 lines (55 loc) · 1.45 KB
/
Copy pathconfig.ru
File metadata and controls
64 lines (55 loc) · 1.45 KB
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
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "rowdy", path: "."
gem "rack"
gem "puma"
end
module Model
class Person < Data.define(:name, :email)
def self.all
10.times.map { |n| find n }
end
def self.find(id)
new name: "Person #{id}", email: "person-#{id}@example.com"
end
end
end
module Authentication
def route(http)
if Rack::Auth::Basic::Request.new(http.request.env).provided?
super http
else
http.response['WWW-Authenticate'] = %(Basic realm="Super duper ultra-secret area")
http.response.write "Authenticate with any username and password"
http.response.status = 401
end
end
end
class ProtectedResources < Rowdy::Controller::Resources
prepend Authentication
class Resource < Resource
def show
"Super secret model #{@model.inspect}"
end
end
end
# Application code (dev sees this)
class Application < Rowdy::Server
def route(http)
http.response.headers["Content-Type"] = "text/plain"
http.response.status = 200
case http.route
in root: true
http.response.write "Hello world! Check out the resources over at /people."
in "people", *_
Rowdy::Controller::Resources.new(scope: Model::Person).route(http)
in "folks", *_
ProtectedResources.new(scope: Model::Person).route(http)
else
http.response.write "Not Found"
http.response.status = 404
end
end
end
run Application.new