From: David Lutterkort <[email protected]>
Signed-off-by: David Lutterkort <[email protected]> --- .../lib/deltacloud/helpers/application_helper.rb | 6 ++-- server/lib/deltacloud/helpers/blob_stream.rb | 2 +- server/lib/deltacloud/models/bucket.rb | 2 +- server/lib/deltacloud/models/image.rb | 2 +- server/lib/sinatra/rabbit.rb | 33 +++++++------------ server/lib/sinatra/url_for.rb | 8 +---- server/server.rb | 18 ++++++++-- 7 files changed, 33 insertions(+), 38 deletions(-) diff --git a/server/lib/deltacloud/helpers/application_helper.rb b/server/lib/deltacloud/helpers/application_helper.rb index 46fa0eb..859129a 100644 --- a/server/lib/deltacloud/helpers/application_helper.rb +++ b/server/lib/deltacloud/helpers/application_helper.rb @@ -22,12 +22,12 @@ module ApplicationHelper include Deltacloud def bread_crumb - s = "<ul class='breadcrumb'><li class='first'><a href='#{root_url}'>δ</a></li>" + s = "<ul class='breadcrumb'><li class='first'><a href='#{settings.root_url}'>δ</a></li>" url = request.path_info.split('?') #remove extra query string parameters levels = url[0].split('/') #break up url into different levels levels.each_with_index do |level, index| unless level.blank? - next if "/#{level}" == Sinatra::UrlForHelper::DEFAULT_URI_PREFIX + next if "/#{level}" == settings.root_url if index == levels.size-1 || (level == levels[levels.size-2] && levels[levels.size-1].to_i > 0) s += "<li class='subsequent'>#{level.gsub(/_/, ' ')}</li>\n" unless level.to_i > 0 else @@ -177,7 +177,7 @@ module ApplicationHelper def link_to_documentation return '' unless request.env['REQUEST_URI'] uri = request.env['REQUEST_URI'].dup - uri.gsub!(Sinatra::UrlForHelper::DEFAULT_URI_PREFIX, + uri.gsub!(settings.root_url, api_url_for(:docs)) unless uri.include?("docs") #i.e. if already serving under /api/docs, leave it be '<a href="%s">[ Documentation ]</a>' % uri end diff --git a/server/lib/deltacloud/helpers/blob_stream.rb b/server/lib/deltacloud/helpers/blob_stream.rb index 07b9566..df5cdc6 100644 --- a/server/lib/deltacloud/helpers/blob_stream.rb +++ b/server/lib/deltacloud/helpers/blob_stream.rb @@ -187,7 +187,7 @@ class BlobStreamIO def self.is_put_blob(request = nil) path = request.env['PATH_INFO'] method = request.env['REQUEST_METHOD'] - if ( path =~ /^#{Regexp.escape(Sinatra::UrlForHelper::DEFAULT_URI_PREFIX)}\/buckets/ && method == 'PUT' ) + if ( path =~ /^#{Regexp.escape(settings.root_url)}\/buckets/ && method == 'PUT' ) return true else return false diff --git a/server/lib/deltacloud/models/bucket.rb b/server/lib/deltacloud/models/bucket.rb index d7c984b..b1551b7 100644 --- a/server/lib/deltacloud/models/bucket.rb +++ b/server/lib/deltacloud/models/bucket.rb @@ -26,7 +26,7 @@ class Bucket < BaseModel h = self.to_hash_original unless blob_list.nil? h[:blob_list] = self.blob_list.collect { |blob| { :id => blob, - :href => "#{Sinatra::UrlForHelper::DEFAULT_URI_PREFIX}/buckets/#{self.id}/#{blob}"}} + :href => "#{settings.root_url}/buckets/#{self.id}/#{blob}"}} end return h end diff --git a/server/lib/deltacloud/models/image.rb b/server/lib/deltacloud/models/image.rb index bdde7d0..67f6dfb 100644 --- a/server/lib/deltacloud/models/image.rb +++ b/server/lib/deltacloud/models/image.rb @@ -30,7 +30,7 @@ class Image < BaseModel h.merge({ :actions => [ :create_instance => { :method => 'post', - :href => "#{Sinatra::UrlForHelper::DEFAULT_URI_PREFIX}/instances;image_id=#{self.id}" + :href => "#{settings.root_url}/instances;image_id=#{self.id}" }] }) end diff --git a/server/lib/sinatra/rabbit.rb b/server/lib/sinatra/rabbit.rb index cf8c14f..c35943b 100644 --- a/server/lib/sinatra/rabbit.rb +++ b/server/lib/sinatra/rabbit.rb @@ -89,8 +89,8 @@ module Sinatra def generate_documentation coll, oper = @collection, self - Rabbit::routes << [:get, "#{Sinatra::UrlForHelper::DEFAULT_URI_PREFIX}/docs/#{@collection.name}/#{@name}"] - ::Sinatra::Application.get("#{Sinatra::UrlForHelper::DEFAULT_URI_PREFIX}/docs/#{@collection.name}/#{@name}") do + Rabbit::routes << [:get, "#{settings.root_url}/docs/#{@collection.name}/#{@name}"] + ::Sinatra::Application.get("#{settings.root_url}/docs/#{@collection.name}/#{@name}") do @collection, @operation = coll, oper @features = driver.features_for_operation(coll.name, oper.name) respond_to do |format| @@ -102,8 +102,8 @@ module Sinatra def generate_options current_operation = self - Rabbit::routes << [:options, "#{Sinatra::UrlForHelper::DEFAULT_URI_PREFIX}/#{current_operation.collection.name}/#{current_operation.name}"] - ::Sinatra::Application.options("#{Sinatra::UrlForHelper::DEFAULT_URI_PREFIX}/#{current_operation.collection.name}/#{current_operation.name}") do + Rabbit::routes << [:options, "#{settings.root_url}/#{current_operation.collection.name}/#{current_operation.name}"] + ::Sinatra::Application.options("#{settings.root_url}/#{current_operation.collection.name}/#{current_operation.name}") do required_params = current_operation.effective_params(driver).collect do |name, validation| name.to_s if validation.type.eql?(:required) end.compact.join(',') @@ -152,8 +152,8 @@ module Sinatra end def generate - Rabbit::routes << [@method, "#{Sinatra::UrlForHelper::DEFAULT_URI_PREFIX}/#{path}"] - ::Sinatra::Application.send(@method, "#{Sinatra::UrlForHelper::DEFAULT_URI_PREFIX}/#{path}", {}, &@control) + Rabbit::routes << [@method, "#{settings.root_url}/#{path}"] + ::Sinatra::Application.send(@method, "#{settings.root_url}/#{path}", {}, &@control) # Set up some Rails-like URL helpers if name == :index gen_route "#{@collection.name}_url" @@ -239,8 +239,8 @@ module Sinatra def generate_head current_collection = self - Rabbit::routes << [:head, "#{Sinatra::UrlForHelper::DEFAULT_URI_PREFIX}/#{name}"] - ::Sinatra::Application.head("#{Sinatra::UrlForHelper::DEFAULT_URI_PREFIX}/#{name}") do + Rabbit::routes << [:head, "#{settings.root_url}/#{name}"] + ::Sinatra::Application.head("#{settings.root_url}/#{name}") do methods_allowed = current_collection.operations.collect { |o| o[1].method.to_s.upcase }.uniq.join(',') headers 'Allow' => "HEAD,OPTIONS,#{methods_allowed}" [200, ''] @@ -249,8 +249,8 @@ module Sinatra def generate_options current_collection = self - Rabbit::routes << [:options, "#{Sinatra::UrlForHelper::DEFAULT_URI_PREFIX}/#{name}"] - ::Sinatra::Application.options("#{Sinatra::UrlForHelper::DEFAULT_URI_PREFIX}/#{name}") do + Rabbit::routes << [:options, "#{settings.root_url}/#{name}"] + ::Sinatra::Application.options("#{settings.root_url}/#{name}") do operations_allowed = current_collection.operations.collect { |o| o[0] }.join(',') headers 'X-Operations-Allowed' => operations_allowed [200, ''] @@ -259,8 +259,8 @@ module Sinatra def generate_documentation coll = self - Rabbit::routes << [:get, "#{Sinatra::UrlForHelper::DEFAULT_URI_PREFIX}/docs/#{@name}"] - ::Sinatra::Application.get("#{Sinatra::UrlForHelper::DEFAULT_URI_PREFIX}/docs/#{@name}") do + Rabbit::routes << [:get, "#{settings.root_url}/docs/#{@name}"] + ::Sinatra::Application.get("#{settings.root_url}/docs/#{@name}") do coll.check_supported(driver) @collection = coll @operations = coll.operations @@ -392,15 +392,6 @@ module Sinatra collections[name] = Collection.new(name, &block) collections[name].generate end - - # Generate a root route for API docs - get "#{Sinatra::UrlForHelper::DEFAULT_URI_PREFIX}/docs\/?" do - respond_to do |format| - format.html { haml :'docs/index' } - format.xml { haml :'docs/index' } - end - end - end module RabbitHelper diff --git a/server/lib/sinatra/url_for.rb b/server/lib/sinatra/url_for.rb index 2891a53..fba6668 100644 --- a/server/lib/sinatra/url_for.rb +++ b/server/lib/sinatra/url_for.rb @@ -29,8 +29,6 @@ require 'uri' module Sinatra module UrlForHelper - DEFAULT_URI_PREFIX = "/api" - def api_url_for(url_fragment, mode=:path_only) matrix_params = '' if request.params['api'] @@ -38,7 +36,7 @@ module Sinatra matrix_params += ";driver=%s" % request.params['api']['driver'] if request.params['api']['driver'] end url_fragment = "/#{url_fragment}" unless url_fragment =~ /^\// # There is no need to prefix URI with '/' - url_for "#{DEFAULT_URI_PREFIX}#{matrix_params}#{url_fragment}", mode + url_for "#{settings.root_url}#{matrix_params}#{url_fragment}", mode end # Construct a link to +url_fragment+, which should be given relative to @@ -87,10 +85,6 @@ module Sinatra "#{base}#{url_escape}" end end - - def root_url - DEFAULT_URI_PREFIX - end end diff --git a/server/server.rb b/server/server.rb index fd4d8e1..91096f4 100644 --- a/server/server.rb +++ b/server/server.rb @@ -46,6 +46,7 @@ use Rack::MediaType use Rack::Date configure do + set :root_url, "/api" set :views, File.dirname(__FILE__) + '/views' set :public_folder, File.dirname(__FILE__) + '/public' # Try to load the driver on startup to fail early if there are issues @@ -89,7 +90,16 @@ end # Redirect to /api get '/' do redirect root_url, 301; end -get "#{Sinatra::UrlForHelper::DEFAULT_URI_PREFIX}\/?" do + +# Generate a root route for API docs +get "#{settings.root_url}/docs\/?" do + respond_to do |format| + format.html { haml :'docs/index' } + format.xml { haml :'docs/index' } + end +end + +get "#{settings.root_url}\/?" do if params[:force_auth] return [401, 'Authentication failed'] unless driver.valid_credentials?(credentials) end @@ -293,7 +303,7 @@ collection :instance_states do end end -get "#{Sinatra::UrlForHelper::DEFAULT_URI_PREFIX}/instances/:id/run" do +get "#{settings.root_url}/instances/:id/run" do @instance = driver.instance(credentials, :id => params[:id]) respond_to do |format| format.html { haml :"instances/run_command" } @@ -769,7 +779,7 @@ end # existing blob NEW_BLOB_FORM_ID = "new_blob_form_d15cfd90" -get "#{Sinatra::UrlForHelper::DEFAULT_URI_PREFIX}/buckets/:bucket/#{NEW_BLOB_FORM_ID}" do +get "#{settings.root_url}/buckets/:bucket/#{NEW_BLOB_FORM_ID}" do @bucket_id = params[:bucket] respond_to do |format| format.html {haml :"blobs/new"} @@ -992,7 +1002,7 @@ collection :buckets do end -get "#{Sinatra::UrlForHelper::DEFAULT_URI_PREFIX}/addresses/:id/associate" do +get "#{settings.root_url}/addresses/:id/associate" do @instances = driver.instances(credentials) @address = Address::new(:id => params[:id]) respond_to do |format| -- 1.7.6
