forked from noahgibbs/rsb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.ru
77 lines (68 loc) · 1.99 KB
/
config.ru
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
require "complex"
require "rack"
if ENV["RACK_ENV"] == "profile"
use Rack::RubyProf,
:path => File.expand_path(File.join(__dir__, 'log/profile')),
:prefix => "rsb-rack-#{Process.pid}-",
:max_requests => ENV["RSB_PROFILE_REQS"] || 10_000
end
class SpeedTest
HEADERS = { "Content-Type" => "text/html" }
ROUTES = {
"/" => proc { [200, HEADERS, ["Hello World!"]] },
"/static" => proc { [200, HEADERS, ["Static Text"]] },
"/request" => proc { |env| r = Rack::Request.new(env); [200, HEADERS, ["Static Text"]] },
"/mandelbrot" => proc { |env|
x, i = env["QUERY_STRING"].split("&",2).map { |item| item.split("=", 2)[1].to_f }
[200, HEADERS, [ SpeedTest.in_mandelbrot(x,i) ? "in" : "out" ]]
},
"/fivehundred" => proc { raise "This raises an error!" },
"/delay" => proc { |env|
t = 0.001
if env["QUERY_STRING"] != nil && env["QUERY_STRING"] != ""
t = env["QUERY_STRING"].split("=",2)[1].to_f
end
sleep t
[ 200, HEADERS, [ "Static Text" ] ]
},
"/erb" => proc { |env|
[ 200, HEADERS, [ SpeedTest.erb_template ] ]
},
# Not yet: /db
"/process_mem" => proc { [ 200, HEADERS, [ "Process memory in bytes: #{GetProcessMem.new.bytes.to_i}" ] ] },
# In multiprocess configurations, this only shuts down a single worker. That's probably not what you want.
"/shutdown" => proc { exit 0 },
}
require 'erb'
TEMPLATE = ERB.new(<<erb)
<html>
<head> <%= title %> </head>
<body>
<h1> <%= title %> </h1>
<p>
<%= content %>
</p>
</body>
</html>
erb
def self.erb_template
title = 'hello world!'
content = "hello world!\n" * 10
TEMPLATE.result(binding)
end
def self.in_mandelbrot(x, i)
z0 = Complex(x, i)
z = z0
80.times { z = z * z }
z.abs < 2.0
end
def call(env)
route = ROUTES[env["PATH_INFO"]]
if route
return route.call(env)
else
[ 404, HEADERS, [ "Sad Trombone... Your route is not found." ] ]
end
end
end
run SpeedTest.new