forked from ontoportal/ontologies_api
-
Notifications
You must be signed in to change notification settings - Fork 10
/
app.rb
187 lines (158 loc) · 5.38 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# sinatra-base
require 'sinatra'
# sinatra-contrib
require 'sinatra/respond_with'
require 'sinatra/namespace'
require 'sinatra/advanced_routes'
require 'sinatra/multi_route'
# Other gem dependencies
require 'oj'
require 'multi_json'
require 'cgi'
# NCBO dependencies
require 'ontologies_linked_data'
require 'ncbo_annotator'
require 'ncbo_cron'
require 'ncbo_ontology_recommender'
# Require middleware
require 'rack/accept'
require 'rack/post-body-to-params'
require 'rack-timeout'
require 'rack/cors'
require_relative 'lib/rack/slow_requests'
require_relative 'lib/rack/cube_reporter'
require_relative 'lib/rack/param_translator'
require_relative 'lib/rack/slice_detection'
require_relative 'lib/rack/request_lang'
# Logging setup
require_relative "config/logging"
# Inflector setup
require_relative "config/inflections"
require 'request_store'
# Protection settings
set :protection, :except => :path_traversal
# Allow HTTP method overrides
set :method_override, true
# Setup root and static public directory
set :root, File.dirname(__FILE__)
use Rack::Static,
:urls => ["/static"],
:root => "public"
# Setup the environment
environment = settings.environment.nil? ? :development : settings.environment
require_relative "config/config"
if ENV['OVERRIDE_CONFIG'] == 'true'
LinkedData.config do |config|
config.goo_backend_name = ENV['GOO_BACKEND_NAME']
config.goo_host = ENV['GOO_HOST']
config.goo_port = ENV['GOO_PORT'].to_i
config.goo_path_query = ENV['GOO_PATH_QUERY']
config.goo_path_data = ENV['GOO_PATH_DATA']
config.goo_path_update = ENV['GOO_PATH_UPDATE']
config.goo_redis_host = ENV['REDIS_HOST']
config.goo_redis_port = ENV['REDIS_PORT']
config.http_redis_host = ENV['REDIS_HOST']
config.http_redis_port = ENV['REDIS_PORT']
end
Annotator.config do |config|
config.annotator_redis_host = ENV['ANNOTATOR_REDIS_HOST']
config.annotator_redis_port = ENV['ANNOTATOR_REDIS_PORT']
config.mgrep_host = ENV['MGREP_HOST']
config.mgrep_port = ENV['MGREP_PORT']
end
end
require_relative "config/environments/#{environment}.rb"
# Development-specific options
if [:development, :console].include?(settings.environment)
require 'pry' # Debug by placing 'binding.pry' where you want the interactive console to start
# Show exceptions
set :raise_errors, true
set :dump_errors, false
set :show_exceptions, false
end
# mini-profiler sets the etag header to nil, so don't use when caching is enabled
if [:development].include?(settings.environment) && !LinkedData.settings.enable_http_cache && LinkedData::OntologiesAPI.settings.enable_miniprofiler
begin
require 'rack-mini-profiler'
Rack::MiniProfiler.config.storage = Rack::MiniProfiler::FileStore
Rack::MiniProfiler.config.position = 'right'
c = ::Rack::MiniProfiler.config
c.pre_authorize_cb = lambda { |env|
true
}
tmp = File.expand_path("../tmp/miniprofiler", __FILE__)
FileUtils.mkdir_p(tmp) unless File.exists?(tmp)
c.storage_options = {path: tmp}
use Rack::MiniProfiler
puts ">> rack-mini-profiler is enabled"
rescue LoadError
# profiler isn't there
end
end
use Rack::Cors do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :put, :patch, :delete, :options]
end
end
# Use middleware (ORDER IS IMPORTANT)
use Rack::Cors do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :put, :patch, :delete, :options]
end
end
if Goo.queries_debug?
use Goo::Debug
end
# Monitoring middleware
if LinkedData::OntologiesAPI.settings.enable_monitoring
cube_settings = {
cube_host: LinkedData::OntologiesAPI.settings.cube_host,
cube_port: LinkedData::OntologiesAPI.settings.cube_port
}
use Rack::CubeReporter, cube_settings
use Rack::SlowRequests, log_path: LinkedData::OntologiesAPI.settings.slow_request_log
end
# Show exceptions after timeout
if LinkedData::OntologiesAPI.settings.enable_req_timeout
use Rack::Timeout; Rack::Timeout.timeout = LinkedData::OntologiesAPI.settings.req_timeout # seconds, shorter than unicorn timeout
end
use Rack::SliceDetection
use Rack::Accept
use Rack::PostBodyToParams
use Rack::ParamTranslator
use RequestStore::Middleware
use Rack::RequestLang
use LinkedData::Security::Authorization
use LinkedData::Security::AccessDenied
if LinkedData::OntologiesAPI.settings.enable_throttling
require_relative 'config/rack_attack'
end
if LinkedData.settings.enable_http_cache
require 'rack/cache'
require 'redis-rack-cache'
redis_host_port = "#{LinkedData::OntologiesAPI.settings.http_redis_host}:#{LinkedData::OntologiesAPI.settings.http_redis_port}"
verbose = environment == :development
use Rack::Cache,
verbose: verbose,
allow_reload: true,
metastore: "redis://#{redis_host_port}/0/metastore",
entitystore: "redis://#{redis_host_port}/0/entitystore"
end
# Initialize unicorn Worker killer to mitigate unicorn worker memory bloat
if LinkedData::OntologiesAPI.settings.enable_unicorn_workerkiller
require 'unicorn'
require_relative 'config/unicorn_workerkiller'
end
# Add New Relic last to allow Rack middleware instrumentation
require 'newrelic_rpm'
# Initialize the app
require_relative 'init'
# Enter console mode
if settings.environment == :console
require 'rack/test'
include Rack::Test::Methods; def app() Sinatra::Application end
Pry.start binding, :quiet => true
exit
end