-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.ru
35 lines (28 loc) · 751 Bytes
/
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
unless ENV['RACK_ENV'] == 'production'
require 'dotenv'
Dotenv.load
end
require 'aws-sdk'
s3 = Aws::S3::Client.new
app = Proc.new do |env|
request = Rack::Request.new(env)
path_info = request.path_info
s3_key = path_info.gsub(/^\/+/, '')
file_ext = ".#{path_info.split('.').last}"
content_type = Rack::Mime.mime_type(file_ext)
content = s3.get_object(
bucket: ENV['AWS_BUCKET'],
key: s3_key
)
[
'200',
{ 'Content-Type' => content_type },
[ content.body.string ]
]
end
# Check creds
protected_app = Rack::Auth::Basic.new(app) do |username, password|
username == ENV['AUTH_USER'] &&
Rack::Utils.secure_compare(ENV['AUTH_PASSWORD'], password)
end
run protected_app