-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.rb
85 lines (64 loc) · 1.68 KB
/
server.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
# Load variables from .env
require 'dotenv/load'
require 'sinatra'
require 'sinatra/namespace'
require 'rack/cors'
require 'sinatra/reloader' if development?
require_relative 'setup'
require_relative 'controllers/BaseController'
require_relative 'controllers/TokenController'
require_relative 'controllers/ServiceController'
setup
token_controller = TokenController.new
service_controller = ServiceController.new
use Rack::Cors do
allow do
origins '*'
resource '*', headers: :any, methods: [:get, :post, :patch, :put, :options, :head, :delete]
end
end
set :public_folder, 'public'
set :port, ENV['PORT']
set :server, 'puma'
before do
content_type 'application/json'
end
helpers do
def halt_if_error
yield
rescue HTTPError => e
halt e.code, e.message.to_json
end
end
post '/token/request' do
halt_if_error { token_controller.request request }
end
post '/token/destroy' do
halt_if_error { token_controller.destroy request }
end
post '/token/refresh' do
halt_if_error { token_controller.refresh request }
end
# Service
get '/services/:service/*' do
halt_if_error { service_controller.request(request, params) }
end
post '/services/:service/*' do
halt_if_error { service_controller.request(request, params) }
end
options '/services/:service/*' do
halt_if_error { service_controller.request(request, params) }
end
patch '/services/:service/*' do
halt_if_error { service_controller.request(request, params) }
end
head '/services/:service/*' do
halt_if_error { service_controller.request(request, params) }
end
delete '/services/:service/*' do
halt_if_error { service_controller.request(request, params) }
end
not_found do
status 404
redirect '/oops.html'
end