Add pattern matching to url params

This commit is contained in:
Kload 2013-06-15 19:57:12 +00:00
parent e451f1ffab
commit 46736549d3
2 changed files with 23 additions and 6 deletions

Binary file not shown.

View file

@ -28,18 +28,37 @@ class Ip
belongs_to :entry belongs_to :entry
end end
#get '/' do get '/' do
#`whoami` `whoami`
#end end
post '/' do post '/' do
content_type :json content_type :json
# TODO: check params # Check params
unless params[:subdomain].match /^[a-z0-9-]{3,16}$/
status 400
return { :error => "Subdomain is invalid: #{params[:subdomain]}.#{DOMAIN}" }
end
unless params[:public_key].match /^[a-z0-9]{22}==$/i
status 400
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 entry = Entry.first(:subdomain => params[:subdomain])
status 409
return { :error => "Subdomain already taken: #{entry.subdomain}.#{DOMAIN}" }
end
if entry = Entry.first(:public_key => params[:public_key]) if entry = Entry.first(:public_key => params[:public_key])
status 409 status 409
return { :error => "Key already exists for domain #{entry.subdomain}.#{DOMAIN}" } return { :error => "Key already exists for domain #{entry.subdomain}.#{DOMAIN}" }
end 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)
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 => params[:public_key], :subdomain => params[:subdomain], :current_ip => request.ip }.to_json
@ -58,6 +77,4 @@ get '/all' do
Entry.all.to_json Entry.all.to_json
end end
DataMapper.auto_upgrade! DataMapper.auto_upgrade!