-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.rb
80 lines (70 loc) · 1.45 KB
/
app.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
require 'sinatra'
require_relative 'keyserver'
require 'pry'
keyserver = KeyServer.new
invalid_key = 'Invalid key'
# Refresh the contents of keys every second
Thread.new do
loop do
sleep 1
keyserver.refresh_contents
end
end
get '/' do
'Server is working'
end
get '/generate_keys' do
keys = keyserver.generate_keys
keys.join('<br />')
end
get '/block/:key' do
response = keyserver.block_key(params['key'])
if response.nil?
status 404
body invalid_key
else
body 'Successfully blocked'
end
end
get '/unblock/:key' do
response = keyserver.unblock_key(params['key'])
if response.nil?
status 404
body invalid_key
else
body 'Successfully unblocked'
end
end
get '/delete/:key' do
response = keyserver.delete_key(params['key'])
if response.nil?
status 404
body invalid_key
else
body 'Successfully deleted'
end
end
get '/showall' do
hash = { blocked_keys: keyserver.blocked_keys.join('<br />'),
unblocked_keys: keyserver.unblocked_keys.join('<br />'),
deleted_keys: keyserver.deleted_keys.join('<br />') }
erb :showall, locals: hash
end
get '/ping/:key' do
response = keyserver.ping_key(params['key'])
if response.nil?
status 404
body invalid_key
else
body 'Key timestamp refreshed'
end
end
get '/serve_key' do
key = keyserver.serve_key
if key.nil?
status 404
body 'No key available! Please generate some keys...'
else
body key
end
end