You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltacloud.apache.org by mf...@apache.org on 2012/09/12 09:35:35 UTC

[5/7] git commit: Core: Mount 'images' dir as static asset directory

Core: Mount 'images' dir as static asset directory

* Refactored multiple frontends loading
* Added IndexApp with list of enabled frontends


Project: http://git-wip-us.apache.org/repos/asf/deltacloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltacloud/commit/79da91b1
Tree: http://git-wip-us.apache.org/repos/asf/deltacloud/tree/79da91b1
Diff: http://git-wip-us.apache.org/repos/asf/deltacloud/diff/79da91b1

Branch: refs/heads/master
Commit: 79da91b1ff374065c62f79ba1efa3a3540c4a167
Parents: a99310f
Author: Michal Fojtik <mf...@redhat.com>
Authored: Mon Sep 10 11:12:48 2012 +0200
Committer: Michal fojtik <mf...@redhat.com>
Committed: Tue Sep 11 12:08:28 2012 +0200

----------------------------------------------------------------------
 server/config.ru              |   33 +++++++++++++++++----------------
 server/lib/deltacloud_rack.rb |   24 ++++++++++++++++++++++++
 server/views/index.html.haml  |    9 +++++++++
 3 files changed, 50 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/79da91b1/server/config.ru
----------------------------------------------------------------------
diff --git a/server/config.ru b/server/config.ru
index a5f1d0a..6a28352 100644
--- a/server/config.ru
+++ b/server/config.ru
@@ -42,34 +42,35 @@ end
 
 routes = {}
 
+def frontends
+  ENV['API_FRONTEND'].split(',').size > 1 ?
+    ENV['API_FRONTEND'].split(',') : [ENV['API_FRONTEND']]
+end
+
 # If user wants to launch multiple frontends withing a single instance of DC API
 # then require them and prepare the routes for Rack
 #
 # NOTE: The '/' will not be generated, since multiple frontends could have
 #       different root_url's
 #
-if ENV['API_FRONTEND'].split(',').size > 1
-
-  ENV['API_FRONTEND'].split(',').each do |frontend|
-    Deltacloud[frontend.to_sym].require!
-    routes.merge!({
-      Deltacloud[frontend].root_url => Deltacloud[frontend].klass
-    })
-  end
-
-else
-  Deltacloud[ENV['API_FRONTEND'].to_sym].require!
-  Deltacloud[ENV['API_FRONTEND'].to_sym].default_frontend!
+frontends.each do |frontend|
+  Deltacloud[frontend.to_sym].require!
   routes.merge!({
-    Deltacloud.default_frontend.root_url => Deltacloud.default_frontend.klass
+    Deltacloud[frontend].root_url => Deltacloud[frontend].klass
   })
 end
 
-# Mount static assets directory
+def static_dir_for(name)
+  Rack::Directory.new( File.join(File.dirname(__FILE__), "public", name))
+end
+
+# Mount static assets directories and index entrypoint
 #
 routes.merge!({
-  "/stylesheets" =>  Rack::Directory.new( File.join(File.dirname(__FILE__), "public", "stylesheets") ),
-  "/javascripts" =>  Rack::Directory.new( File.join(File.dirname(__FILE__), "public", "javascripts") )
+  '/' => Deltacloud::IndexApp,
+  '/stylesheets' =>  static_dir_for('stylesheets'),
+  '/javascripts' =>  static_dir_for('javascripts'),
+  '/images' =>  static_dir_for('images')
 })
 
 run Rack::Builder.new {

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/79da91b1/server/lib/deltacloud_rack.rb
----------------------------------------------------------------------
diff --git a/server/lib/deltacloud_rack.rb b/server/lib/deltacloud_rack.rb
index 4b5898f..4999083 100644
--- a/server/lib/deltacloud_rack.rb
+++ b/server/lib/deltacloud_rack.rb
@@ -42,6 +42,10 @@ module Deltacloud
     Deltacloud[frontend].klass eval('::'+Deltacloud[frontend].klass)
   end
 
+  def self.enabled_frontends
+    @config.keys.select { |k| frontend_required?(k) }.map { |f| Deltacloud[f] }
+  end
+
   def self.frontend_required?(frontend)
     true unless Deltacloud[frontend].klass.kind_of? String
   end
@@ -52,6 +56,26 @@ module Deltacloud
     @default_frontend || config[:deltacloud]
   end
 
+  require 'sinatra/base'
+  require_relative './deltacloud/helpers/deltacloud_helper'
+  require_relative './sinatra/rack_accept'
+
+  class IndexApp < Sinatra::Base
+
+    helpers Deltacloud::Helpers::Application
+    register Rack::RespondTo
+
+    set :views, File.join(File.dirname(__FILE__), '..', 'views')
+
+    get '/' do
+      respond_to do |format|
+        format.xml { haml :'index', :layout => false }
+        format.html { haml :'index', :layout => false }
+        format.json { xml_to_json "index" }
+      end
+    end
+  end
+
   class Server
 
     attr_reader :name

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/79da91b1/server/views/index.html.haml
----------------------------------------------------------------------
diff --git a/server/views/index.html.haml b/server/views/index.html.haml
new file mode 100644
index 0000000..4263024
--- /dev/null
+++ b/server/views/index.html.haml
@@ -0,0 +1,9 @@
+%html
+  %head
+  %body
+    %h1 Deltacloud API
+    %h2 Enabled Frontends:
+    %ul
+      - Deltacloud.enabled_frontends.each do |f|
+        %li
+          %a{ :href => f.root_url}=f.name