forked from onetimesecret/onetimesecret
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.ru
77 lines (62 loc) · 1.83 KB
/
config.ru
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
# frozen_string_literal: true
# Rackup Configuration
#
# Usage:
#
# $ thin -e dev -R config.ru -p 3000 start
#
# Environment Variables
ENV['RACK_ENV'] ||= 'production'
ENV['APP_ROOT'] = File.expand_path(__dir__).freeze
app_root = ENV['APP_ROOT']
# Directory Constants
unless defined?(PUBLIC_DIR)
PUBLIC_DIR = File.join(app_root, '/public/web').freeze
APP_DIR = File.join(app_root, '/lib/onetime/app').freeze
end
# Load Paths
$LOAD_PATH.unshift(File.join(app_root, 'lib'))
# Required Libraries
require 'rack/content_length'
require 'rack/contrib'
require_relative 'lib/middleware'
require_relative 'lib/onetime'
# Boot Application
Onetime.boot! :app
# Rack Applications Configuration
apps = {
'/api/v1' => '/api/v1/routes',
'/api/v2' => '/api/v2/routes',
'/' => '/web/routes'
}.transform_values { |path| Otto.new(File.join(APP_DIR, path)) }
# JSON Response Headers
headers = { 'Content-Type' => 'application/json' }
# API Error Responses
apps["/api/v1"].not_found = [404, headers, [{ error: 'Not Found' }.to_json]]
apps["/api/v1"].server_error = [500, headers, [{ error: 'Internal Server Error' }.to_json]]
apps["/api/v2"].not_found = [404, headers, [{ message: 'Not Found' }.to_json]]
apps["/api/v2"].server_error = [500, headers, [{ message: 'Internal Server Error' }.to_json]]
# Public Directory for Root Endpoint
apps['/'].option[:public] = PUBLIC_DIR
# Common Middleware
common_middleware = [
Rack::CommonLogger,
Rack::ClearSessionMessages,
Rack::HandleInvalidUTF8,
Rack::HandleInvalidPercentEncoding,
Rack::ContentLength
]
# Apply common middleware to all apps
common_middleware.each { |middleware| use middleware }
use Rack::Reloader, 1 if Otto.env?(:dev)
# Mount Applications
map '/api/v2' do
use Rack::JSONBodyParser
run apps['/api/v2']
end
map '/api/v1' do
run apps['/api/v1']
end
map '/' do
run apps['/']
end