incubator-deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michal Fojtik <mfoj...@redhat.com>
Subject Re: [PATCH core 2/2] Added Rack middleware which set driver/per request using HTTP headers
Date Fri, 26 Nov 2010 14:26:16 GMT
On 26/11/10 15:19 +0100, mfojtik@redhat.com wrote:

Please ignore this email:

Send this email? ([y]es|[n]o|[q]uit|[a]ll): n
Result: 250 2.0.0 oAQEJsxJ013728 Message accepted for delivery

  -- Michal

>From: Michal Fojtik <mfojtik@redhat.com>
>
>---
> server/lib/sinatra/rack_driver_select.rb |   23 +++++++++++++++++++++++
> server/server.rb                         |    3 +++
> server/views/api/drivers.xml.haml        |    6 ++++++
> 3 files changed, 32 insertions(+), 0 deletions(-)
> create mode 100644 server/lib/sinatra/rack_driver_select.rb
> create mode 100644 server/views/api/drivers.xml.haml
>
>diff --git a/server/lib/sinatra/rack_driver_select.rb b/server/lib/sinatra/rack_driver_select.rb
>new file mode 100644
>index 0000000..a1a3c63
>--- /dev/null
>+++ b/server/lib/sinatra/rack_driver_select.rb
>@@ -0,0 +1,23 @@
>+class RackDriverSelect
>+
>+  def initialize(app, opts={})
>+    @app = app
>+    @opts = opts
>+  end
>+
>+  def call(env)
>+    original_driver = settings.driver
>+    if env['HTTP_HEADERS'] and new_driver=extract_driver(env)
>+      set :driver, new_driver
>+    end
>+    response = @app.call(env)
>+    set :driver, original_driver
>+    response
>+  end
>+
>+  def extract_driver(env)
>+    driver_name = env['HTTP_HEADERS'].match(/X\-Deltacloud\-Driver:(\w+)/).to_a
>+    return driver_name[1] if driver_name[1]
>+  end
>+
>+end
>diff --git a/server/server.rb b/server/server.rb
>index 297e556..f888af9 100644
>--- a/server/server.rb
>+++ b/server/server.rb
>@@ -10,10 +10,13 @@ require 'erb'
> require 'haml'
> require 'open3'
> require 'lib/deltacloud/helpers/blob_stream'
>+require 'sinatra/rack_driver_select'
>
> set :driver, (ENV['API_DRIVER'] ? ENV['API_DRIVER'] : :mock)
> set :version, '0.0.1'
>
>+use RackDriverSelect
>+
> configure do
>   set :raise_errors => false
>   set :show_exceptions, false
>diff --git a/server/views/api/drivers.xml.haml b/server/views/api/drivers.xml.haml
>new file mode 100644
>index 0000000..7beb9c8
>--- /dev/null
>+++ b/server/views/api/drivers.xml.haml
>@@ -0,0 +1,6 @@
>+%api{ :version => settings.version }
>+  %drivers
>+    - DRIVERS.each do |id, details|
>+      %driver{ :id => id }
>+        %name<
>+          =details[:name]
>--
>1.7.3.2
>

-- 
--------------------------------------------------------
Michal Fojtik, mfojtik@redhat.com
Deltacloud API: http://deltacloud.org
--------------------------------------------------------

Mime
View raw message