forked from tejasshah93/key-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
83 lines (72 loc) · 1.33 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
81
82
83
# app.rb
require 'sinatra'
require 'json'
require 'set'
require_relative 'key_server'
key_server = KeyServer.new
Thread.new do
loop do
sleep 1
key_server.cleanup
end
end
# Route: /
get '/' do
'OK'
end
# Route: generate keys
get '/keys' do
keys = key_server.generate_keys(3)
keys.join('<br/>')
end
# Displays the list of free keys
get '/free' do
content_type :json
key_server.free_keys.to_json
end
# Shows the status of all keys
get '/show' do
content_type :json
key_server.keys.to_json
end
# Route: Get key with given id
get '/key' do
key = key_server.fetch_key
if key.nil?
status 404
body 'No free keys found. Generate more keys.'
else
status 200
body key
end
end
# Route: Unblock key with given id
get '/key/unblock/:id' do |key|
if !key_server.unblock_key(key)
status 404
"No key #{key} found"
else
status 200
"Unblock successful for #{key}"
end
end
# Route: Delete key with given id
get '/key/delete/:id' do |key|
if !key_server.delete_key(key)
status 404
"No key #{key} found"
else
status 200
"Delete successful for #{key}"
end
end
# Route: Keep alive key with given id
get '/key/keep/:id' do |key|
if !key_server.keep_alive_key(key)
status 404
"No key #{key} found"
else
status 200
"Keep alive successful for #{key}"
end
end