-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.rb
More file actions
81 lines (74 loc) · 1.39 KB
/
server.rb
File metadata and controls
81 lines (74 loc) · 1.39 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
require 'sinatra'
require 'rack/handler/puma'
require 'json'
require_relative 'app/import_from_csv'
require_relative 'app/test'
require_relative 'app/worker'
require_relative 'app/db'
#Web
get '/index' do
content_type :html
File.open('views/index.html')
end
get '/index/details' do
content_type :html
File.open('views/details.html')
end
# API
get '/tests' do
content_type :json
response = Test.all
if response.any?
status 200
response.to_json
else
status 404
{detail: "Not found"}.to_json
end
end
get '/tests/format=json' do
content_type :json
response = Test.all_json
if response.any?
status 200
response.to_json
else
status 404
{detail: "Not found"}.to_json
end
end
get '/tests/:token' do
content_type :json
response = Test.find(params['token'])
if response.any?
status 200
if response.length > 1
response.to_json
else
response.first.to_json
end
else
status 404
{detail: "Not found"}.to_json
end
end
post '/import' do
begin
csv = request.body.read
Worker.perform_async(csv)
status 201
rescue => exception
status 404
{detail: "Not created, error internal: #{exception}"}.to_json
puts "NOTICE: ERROR IMPORT"
end
redirect '/index'
end
# Configuration
if ENV['APP_ENV'] != 'test'
Rack::Handler::Puma.run(
Sinatra::Application,
Port: 3000,
Host: '0.0.0.0'
)
end