PUT route

This commit is contained in:
Kload 2013-06-15 20:13:57 +00:00
parent 46736549d3
commit d39dfae312
2 changed files with 32 additions and 21 deletions

Binary file not shown.

View file

@ -35,39 +35,50 @@ end
post '/' do post '/' do
content_type :json content_type :json
# Check params # Check params
unless params[:subdomain].match /^[a-z0-9-]{3,16}$/
status 400 status 400
return { :error => "Subdomain is invalid: #{params[:subdomain]}.#{DOMAIN}" } return { :error => "Please indicate a subdomain" } unless params.has_key?("subdomain")
end return { :error => "Please indicate a public key" } unless params.has_key?("public_key")
unless params[:public_key].match /^[a-z0-9]{22}==$/i return { :error => "Subdomain is invalid: #{params[:subdomain]}.#{DOMAIN}" } unless params[:subdomain].match /^[a-z0-9-]{3,16}$/
status 400 return { :error => "Key is invalid: #{params[:public_key]}" } unless params[:public_key].match /^[a-z0-9]{22}==$/i
return { :error => "Key is invalid: #{params[:public_key]}" }
end
unless params[:current_ip].match /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
status 400
return { :error => "Key is invalid: #{params[:current_ip]}" }
end
# If already exists # If already exists
if entry = Entry.first(:subdomain => params[:subdomain])
status 409 status 409
return { :error => "Subdomain already taken: #{entry.subdomain}.#{DOMAIN}" } return { :error => "Subdomain already taken: #{entry.subdomain}.#{DOMAIN}" } if entry = Entry.first(:subdomain => params[:subdomain])
end return { :error => "Key already exists for domain #{entry.subdomain}.#{DOMAIN}" } if entry = Entry.first(:public_key => params[:public_key])
if entry = Entry.first(:public_key => params[:public_key])
status 409 # Process
return { :error => "Key already exists for domain #{entry.subdomain}.#{DOMAIN}" }
end
entry = Entry.new(:public_key => params[:public_key], :subdomain => params[:subdomain], :current_ip => request.ip) entry = Entry.new(:public_key => params[:public_key], :subdomain => params[:subdomain], :current_ip => request.ip)
entry.ips << Ip.create(:ip_addr => request.ip) entry.ips << Ip.create(:ip_addr => request.ip)
if entry.save if entry.save
status 201 status 201
return { :public_key => params[:public_key], :subdomain => params[:subdomain], :current_ip => request.ip }.to_json return { :public_key => entry.public_key, :subdomain => entry.subdomain, :current_ip => entry.current_ip }.to_json
else else
status 412 status 412
return { :error => "A problem occured during DNS registration" } return { :error => "A problem occured during DNS registration" }
end end
end end
put '/' do
content_type :json
# Check params
status 400
return { :error => "Please indicate a public key" } unless params.has_key?("public_key")
return { :error => "Key is invalid: #{params[:public_key]}" } unless params[:public_key].match /^[a-z0-9]{22}==$/i
entry = Entry.first(:public_key => params[:public_key])
unless request.ip == entry.current_ip
entry.ips << Ip.create(:ip_addr => request.ip)
end
entry.current_ip = request.ip
if entry.save
status 201
return { :public_key => entry.public_key, :subdomain => entry.subdomain, :current_ip => entry.current_ip }.to_json
else
status 412
return { :error => "A problem occured during DNS update" }
end
end
get '/all' do get '/all' do
unless request.ip == "82.242.206.127" unless request.ip == "82.242.206.127"
status 403 status 403