-
Notifications
You must be signed in to change notification settings - Fork 10
/
web.rb
108 lines (95 loc) · 3.36 KB
/
web.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
require 'rubygems'
require 'bundler/setup'
require 'sinatra'
require 'json'
require 'rdf/vocab'
require 'request_store'
require_relative 'mu.rb'
configure do
set :environment, ENV['RACK_ENV'].downcase.to_sym
set :bind, '0.0.0.0'
set :port, 80
set :protection, :except => [:json_csrf]
end
if development?
# Verify we're on a sane environment
mounted_volume = '/app'
if not File.directory?(mounted_volume) or not File.exist?("#{mounted_volume}/#{ENV['APP_ENTRYPOINT']}")
Mu::log.warn "Template is started in development mode, but no sources are mounted in #{mounted_volume}. Expected a file at #{mounted_volume}/#{ENV['APP_ENTRYPOINT']}."
exit(1)
end
# debug/session (does not run on JRuby) provides chromium debugger support
unless RUBY_PLATFORM == 'java'
require 'debug/session'
DEBUGGER__.open_tcp(port: ENV['RUBY_DEBUG_PORT'], nonstop: true, log_level: 'ERROR')
end
# better_errors provides nicer error message on the console and interactive web page
require 'better_errors'
use BetterErrors::Middleware
BetterErrors::Middleware.allow_ip! ENV['TRUSTED_IP'] if ENV['TRUSTED_IP']
# set the application root in order to abbreviate filenames within the application:
BetterErrors.application_root = File.expand_path('..', __FILE__)
end
###
# Vocabularies
###
include RDF
MU = RDF::Vocabulary.new('http://mu.semte.ch/vocabularies/')
MU_CORE = RDF::Vocabulary.new(MU.to_uri.to_s + 'core/')
MU_EXT = RDF::Vocabulary.new(MU.to_uri.to_s + 'ext/')
SERVICE_RESOURCE_BASE = 'http://mu.semte.ch/services/'
###
# Helpers
###
if Mu::truthy? ENV['USE_LEGACY_UTILS']
Mu::log.info "USE_LEGACY_UTILS enabled. Deprecated utilities will be included. Upgrade by using utils from the Mu-module instead. E.g. 'query' becomes 'Mu::query'"
require_relative 'sinatra_template/helpers.rb'
require_relative 'sinatra_template/utils.rb'
include SinatraTemplate::GlobalUtils
end
helpers Mu::Helpers
###
# Hooks
###
use RequestStore::Middleware
before do
begin
request.body.rewind
@json_body = JSON.parse request.body.read
rescue
# request doesn't have a JSON body. Do nothing.
end
begin
if Mu::Helpers::session_id_header(request)
RequestStore.store[:mu_session_id] = Mu::Helpers::session_id_header(request)
RequestStore.store[:mu_call_id] = request.env['HTTP_MU_CALL_ID']
RequestStore.store[:mu_auth_allowed_groups] = request.env['HTTP_MU_AUTH_ALLOWED_GROUPS']
RequestStore.store[:mu_auth_used_groups] = request.env['HTTP_MU_AUTH_USED_GROUPS']
end
rescue Exception => e
Mu::log.error e
end
end
after do
auth_headers = {}
if RequestStore.store[:mu_auth_allowed_groups]
if headers['mu-auth-allowed-groups']
Mu::log.info "ruby template: not setting allowed groups because header already provided with value #{headers['mu-auth-allowed-groups'].inspect}"
else
auth_headers['mu-auth-allowed-groups'] = RequestStore.store[:mu_auth_allowed_groups]
end
end
if RequestStore.store[:mu_auth_used_groups]
if headers['mu-auth-used-groups']
Mu::log.info "ruby template: not setting used groups because header already provided with value #{headers['mu-auth-used-groups'].inspect}"
else
auth_headers['mu-auth-used-groups'] = RequestStore.store[:mu_auth_used_groups]
end
end
headers auth_headers
end
###
# Include extension code
###
app_file = ENV['APP_ENTRYPOINT']
require_relative "ext/#{app_file}"