-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver-1.rb
65 lines (58 loc) · 1.46 KB
/
server-1.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
require 'sinatra'
require 'rack/handler/puma'
require 'csv'
require 'pg'
require 'logger'
require_relative 'insertions'
require_relative 'query_and_format'
require 'rack/cors'
require_relative 'worker'
require 'sidekiq'
require 'active_support/time'
require 'redis'
use Rack::Cors do
allow do
origins '*'
resource '*', headers: :any, methods: [:get, :post]
end
end
configure do
set :logging, Logger::DEBUG
end
get '/tests' do
QueryAndFormat.get_all_tests
end
get '/tests/:token' do
QueryAndFormat.get_single_test(params[:token])
end
post '/import' do
file = params[:csvFile]
begin
json_csv = Insertions.read_and_parse_csv_to_json(file['tempfile'])
job_id = Worker.perform_async(json_csv, file['filename'])
headers 'Access-Control-Allow-Origin' => '*'
headers 'Access-Control-Allow-Methods' => 'POST', 'Access-Control-Allow-Headers' => 'Content-Type'
body job_id
status 200
rescue => e
headers 'Access-Control-Allow-Origin' => '*'
headers 'Access-Control-Allow-Methods' => 'POST', 'Access-Control-Allow-Headers' => 'Content-Type'
status 400
body "Erro ao processar o arquivo: #{e.message}"
end
end
post '/status' do
job_ids = JSON.parse(request.body.read)
jobs_status = []
job_ids.each do |job_id|
jobs_status << Worker.get_job_status(job_id)
end
JSON.pretty_generate(jobs_status)
end
if ENV['APP_ENV'] != 'test'
Rack::Handler::Puma.run(
Sinatra::Application,
Port: 3000,
Host: '0.0.0.0'
)
end