-
Notifications
You must be signed in to change notification settings - Fork 0
/
switchyard.rb
116 lines (103 loc) · 4.06 KB
/
switchyard.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# Copyright 2011-2015, The Trustees of Indiana University and Northwestern
# University. Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
#
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
# --- END LICENSE_HEADER BLOCK ---
$LOAD_PATH << File.expand_path('../', __FILE__)
$LOAD_PATH << File.expand_path('../', __FILE__) + '/lib/'
$LOAD_PATH << File.expand_path('../', __FILE__) + '/models/'
require 'sinatra'
require 'json'
require 'logger'
require 'switchyard_configuration'
require 'api_token'
require 'objects'
require 'router'
require 'collection'
require 'media_object'
require 'sinatra/activerecord'
require 'byebug' if settings.development?
# automatically load config/database.yml and assing it to settings.datase:
register Sinatra::ActiveRecordExtension
configure do
# Anything set here is available at this level with settings.foo
# @example
# settings.app_start_time #=> "2015-12-17T19:02:05Z"
# In libraries these settings can be accessed as Sinatra::Application.settings.foo
# @example
# Sinatra::Application.settings.app_start_time #=> "2015-12-17T19:02:05Z"
set :app_start_time, Time.now.utc.iso8601
set :max_retries, 5
set :max_sleep_seconds, 5
set :max_sleep_seconds, 0.2 unless settings.production?
loader = SwitchyardConfiguration.new
set :switchyard_configs, loader.load_yaml('switchyard.yml')
Dir.mkdir('log') unless Dir.exist?('log')
set :switchyard_log, Logger.new("log/#{ENV['RACK_ENV']}.log")
end
helpers do
# Function to place on any route that desire to have protected by the Api token,
# displays a 401 if the token is not valid
#
# @return [Nil] Returns nothing if the token is valid, allowing the request to continue
def protected!
return if ApiToken.new.valid_token?(env['HTTP_API_TOKEN'])
halt 401, "Not authorized\n"
end
def database_connection_failure!
halt 500, { error: true, message: 'Could not connect to database' }.to_json
end
def record_not_found!
halt 200, { error: true, message: 'Record not found' }.to_json
end
end
# Displays status information about the app
#
# @return [JSON] A JSON has of the status params
get '/' do
content_type :json
{ app_start_time: settings.app_start_time,
rack_env: Sinatra::Application.environment
}.to_json
end
post '/media_objects/create' do
content_type :json
protected!
posted_content = request.body.read
media_object = Objects.new(posted_content: posted_content)
# Parse the request and throw a 400 code if bad data was posted in
settings.switchyard_log.info "Received request #{posted_content}"
object = media_object.parse_request_body
settings.switchyard_log.info "Parsed #{object}"
halt 400, { status: '400', error: true, message: object[:status][:error] }.to_json unless object[:status][:valid] # halt if the provided data is incorrect
registration_results = media_object.register_object(object)
database_connection_failure! unless registration_results[:success]
# Display the object as it is currently entered into the database
status = media_object.object_status_as_json(registration_results[:group_name])
unless status[:success]
database_connection_failure! if status[:error] == 500
record_not_found! if status[:error] == 404
end
stream do |out|
out << status.to_json # return the initial status so MDPI has some response and then keep working
end
end
get '/media_objects/status/:group_name' do
content_type :json
protected!
media_object = Objects.new
status = media_object.object_status_as_json(params[:group_name])
unless status[:success]
database_connection_failure! if status[:error] == 500
record_not_found! if status[:error] == 404
end
status.to_json
end