-
Notifications
You must be signed in to change notification settings - Fork 6
/
liquor-cabinet.rb
140 lines (110 loc) · 3.37 KB
/
liquor-cabinet.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
$LOAD_PATH << File.join(File.expand_path(File.dirname(__FILE__)), 'lib')
require "json"
require "sinatra/base"
require 'sinatra/config_file'
require "sinatra/reloader"
require "remote_storage/s3"
class LiquorCabinet < Sinatra::Base
#
# Configuration
#
configure do
disable :protection, :logging
enable :dump_errors
register Sinatra::ConfigFile
set :environments, %w{development test production staging}
config_file 'config.yml.erb'
end
configure :development do
register Sinatra::Reloader
also_reload "lib/remote_storage/*.rb"
set :logging, Logger::DEBUG
end
configure :production do
# Disable logging
require "rack/common_logger"
end
configure :production, :staging do
if ENV['SENTRY_DSN']
require "raven/base"
require "raven/integrations/rack"
Raven.configure do |config|
config.dsn = ENV['SENTRY_DSN']
config.tags = { environment: settings.environment.to_s }
config.excluded_exceptions = ['Sinatra::NotFound']
end
use Raven::Rack
end
end
configure :staging do
set :logging, Logger::DEBUG
end
#
# Cabinet doors
#
before do
halt 503 if settings.maintenance rescue false
end
["/:user/*/:key", "/:user/:key", "/:user/*/", "/:user/"].each do |path|
before path do
headers 'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods' => 'GET, PUT, DELETE',
'Access-Control-Allow-Headers' => 'Authorization, Content-Type, Origin, If-Match, If-None-Match, Range',
'Access-Control-Expose-Headers' => 'ETag, Content-Length, Content-Range, Content-Type',
'Accept-Ranges' => 'bytes'
headers['Access-Control-Allow-Origin'] = env["HTTP_ORIGIN"] if env["HTTP_ORIGIN"]
headers['Cache-Control'] = 'no-cache'
@user, @key = params[:user], params[:key]
@directory = params[:splat] && params[:splat].first || ""
token = env["HTTP_AUTHORIZATION"] ? env["HTTP_AUTHORIZATION"].split(" ")[1] : ""
no_key = @key.nil? || @key.empty?
storage.authorize_request(@user, @directory, token, no_key) unless request.options?
end
options path do
headers['Access-Control-Max-Age'] = '7200'
halt 200
end
end
["/:user/*/:key", "/:user/:key"].each do |path|
head path do
storage.get_head(@user, @directory, @key)
end
get path do
storage.get_data(@user, @directory, @key)
end
put path do
data = request.body.read
halt 422 unless env['CONTENT_TYPE']
if env['CONTENT_TYPE'] == "application/x-www-form-urlencoded"
content_type = "text/plain; charset=utf-8"
else
content_type = env['CONTENT_TYPE']
end
storage.put_data(@user, @directory, @key, data, content_type)
end
delete path do
storage.delete_data(@user, @directory, @key)
end
end
["/:user/*/", "/:user/"].each do |path|
head path do
storage.get_head_directory_listing(@user, @directory)
end
get path do
storage.get_directory_listing(@user, @directory)
end
end
private
def storage
@storage ||= begin
if settings.respond_to? :s3
RemoteStorage::S3.new(settings, self)
else
puts <<-EOF
You need to set one storage backend in your config.yml file.
Riak and Swift are currently supported. See config.yml.example.
EOF
end
end
end
end