[enh] Allow CORS on every route. Add comments.

This commit is contained in:
opi 2016-04-26 09:24:33 +02:00
parent 98738741e2
commit f1e8f2a108

View file

@ -49,7 +49,9 @@ not_found do
halt 404, { :error => "Not found" }.to_json halt 404, { :error => "Not found" }.to_json
end end
# Common tasks and settings for every route
before do before do
# Ban IP on flood
if Ipban.first(:ip_addr => request.ip) if Ipban.first(:ip_addr => request.ip)
halt 410, "Your ip is banned from the service" halt 410, "Your ip is banned from the service"
end end
@ -64,7 +66,12 @@ before do
Iplog.create(:ip_addr => request.ip, :visited_at => Time.now) Iplog.create(:ip_addr => request.ip, :visited_at => Time.now)
end end
end end
# Always return json
content_type :json content_type :json
# Allow CORS
headers['Access-Control-Allow-Origin'] = '*'
end end
# Check params # Check params
@ -97,12 +104,10 @@ get '/' do
end end
get '/domains' do get '/domains' do
headers['Access-Control-Allow-Origin'] = '*'
DOMAINS.to_json DOMAINS.to_json
end end
get '/test/:subdomain' do get '/test/:subdomain' do
headers['Access-Control-Allow-Origin'] = '*'
if entry = Entry.first(:subdomain => params[:subdomain]) if entry = Entry.first(:subdomain => params[:subdomain])
halt 409, { :error => "Subdomain already taken: #{entry.subdomain}" }.to_json halt 409, { :error => "Subdomain already taken: #{entry.subdomain}" }.to_json
else else