incubator-deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core 2/2] Added Rack middleware which set driver/per request using HTTP headers
Date Fri, 26 Nov 2010 14:19:50 GMT
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


Mime
View raw message