From 5b4338bf8c14ce8f78617d7ed68c48564e98a104 Mon Sep 17 00:00:00 2001 From: Roman Heinrich Date: Mon, 11 Mar 2013 13:10:19 +0200 Subject: [PATCH] rename Riemann::Dash -> Riemann::Dash::App + move sinatra app to app.rb --- bin/riemann-dash | 4 +- lib/riemann/dash.rb | 106 +-------------------------- lib/riemann/dash/app.rb | 104 ++++++++++++++++++++++++++ lib/riemann/dash/controller/css.rb | 2 +- lib/riemann/dash/controller/index.rb | 2 +- lib/riemann/dash/version.rb | 2 +- 6 files changed, 112 insertions(+), 108 deletions(-) create mode 100644 lib/riemann/dash/app.rb diff --git a/bin/riemann-dash b/bin/riemann-dash index 94c76ca..386d25c 100755 --- a/bin/riemann-dash +++ b/bin/riemann-dash @@ -3,5 +3,5 @@ $LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))) require 'riemann/dash' -Riemann::Dash.load ARGV.first -Riemann::Dash.run! +Riemann::Dash::App.load ARGV.first +Riemann::Dash::App.run! diff --git a/lib/riemann/dash.rb b/lib/riemann/dash.rb index 5ac6a3f..6cf8f8f 100644 --- a/lib/riemann/dash.rb +++ b/lib/riemann/dash.rb @@ -1,105 +1,5 @@ require 'rubygems' require 'sinatra/base' - -module Riemann - class Dash < Sinatra::Base - # A little dashboard sinatra application. - - require 'yaml' - require 'find' - require 'erubis' - require 'sass' - - def self.config - @config ||= { - :controllers => [File.join(File.dirname(__FILE__), 'dash', 'controller')], - :views => File.join(File.dirname(__FILE__), 'dash', 'views'), - :ws_config => File.join(File.dirname(__FILE__), '..', '..', 'config', 'config.json'), - :public => File.join(File.dirname(__FILE__), 'dash', 'public') - } - end - - def self.load(filename) - unless load_config(filename || 'config.rb') - # Configuration failed; load a default view. - puts "No configuration loaded; using defaults." - end - - # load controllers - config[:controllers].each { |d| load_controllers d } - - # views - set :views, File.expand_path(config[:views]) - - # setup public dir - public_dir(config[:public]) - end - - # Executes the configuration file. - def self.load_config(filename) - begin - instance_eval File.read(filename) - true - rescue Errno::ENOENT - false - end - end - - # Load controllers. - # Controllers can be regular old one-file-per-class, but - # if you prefer a little more modularity, this method will allow you to - # define all controller methods in their own files. For example, get - # "/posts/*/edit" can live in controller/posts/_/edit.rb. The sorting - # system provided here requires files in the correct order to handle - # wildcards appropriately. - def self.load_controllers(dir) - rbs = [] - Find.find( - File.expand_path(dir) - ) do |path| - rbs << path if path =~ /\.rb$/ - end - - # Sort paths with _ last, becase those are wildcards. - rbs.sort! do |a, b| - as = a.split File::SEPARATOR - bs = b.split File::SEPARATOR - - # Compare common subpaths - l = [as.size, bs.size].min - catch :x do - (0...l).each do |i| - a, b = as[i], bs[i] - if a[/^_/] and not b[/^_/] - throw :x, 1 - elsif b[/^_/] and not a[/^_/] - throw :x, -1 - elsif ord = (a <=> b) and ord != 0 - throw :x, ord - end - end - - # All subpaths are identical; sort longest first - if as.size > bs.size - throw :x, -1 - elsif as.size < bs.size - throw :x, -1 - else - throw :x, 0 - end - end - end - - rbs.each do |r| - require r - end - end - - # Add an additional public directory. - def self.public_dir(dir) - require 'riemann/dash/rack/static' - use Riemann::Dash::Static, :root => dir - end - end - -end +require 'riemann/dash/version' +require 'riemann/dash/config' +require 'riemann/dash/app' \ No newline at end of file diff --git a/lib/riemann/dash/app.rb b/lib/riemann/dash/app.rb new file mode 100644 index 0000000..6e32379 --- /dev/null +++ b/lib/riemann/dash/app.rb @@ -0,0 +1,104 @@ +module Riemann + module Dash + class App < Sinatra::Base + # A little dashboard sinatra application. + + require 'yaml' + require 'find' + require 'erubis' + require 'sass' + + def self.config + @config ||= { + :controllers => [File.join(File.dirname(__FILE__), 'dash', 'controller')], + :views => File.join(File.dirname(__FILE__), 'dash', 'views'), + :ws_config => File.join(File.dirname(__FILE__), '..', '..', 'config', 'config.json'), + :public => File.join(File.dirname(__FILE__), 'dash', 'public') + } + end + + def self.load(filename) + unless load_config(filename || 'config.rb') + # Configuration failed; load a default view. + puts "No configuration loaded; using defaults." + end + + # load controllers + config[:controllers].each { |d| load_controllers d } + + # views + set :views, File.expand_path(config[:views]) + + # setup public dir + public_dir(config[:public]) + end + + # Executes the configuration file. + def self.load_config(filename) + begin + instance_eval File.read(filename) + true + rescue Errno::ENOENT + false + end + end + + # Load controllers. + # Controllers can be regular old one-file-per-class, but + # if you prefer a little more modularity, this method will allow you to + # define all controller methods in their own files. For example, get + # "/posts/*/edit" can live in controller/posts/_/edit.rb. The sorting + # system provided here requires files in the correct order to handle + # wildcards appropriately. + def self.load_controllers(dir) + rbs = [] + Find.find( + File.expand_path(dir) + ) do |path| + rbs << path if path =~ /\.rb$/ + end + + # Sort paths with _ last, becase those are wildcards. + rbs.sort! do |a, b| + as = a.split File::SEPARATOR + bs = b.split File::SEPARATOR + + # Compare common subpaths + l = [as.size, bs.size].min + catch :x do + (0...l).each do |i| + a, b = as[i], bs[i] + if a[/^_/] and not b[/^_/] + throw :x, 1 + elsif b[/^_/] and not a[/^_/] + throw :x, -1 + elsif ord = (a <=> b) and ord != 0 + throw :x, ord + end + end + + # All subpaths are identical; sort longest first + if as.size > bs.size + throw :x, -1 + elsif as.size < bs.size + throw :x, -1 + else + throw :x, 0 + end + end + end + + rbs.each do |r| + require r + end + end + + # Add an additional public directory. + def self.public_dir(dir) + require 'riemann/dash/rack/static' + use Riemann::Dash::Static, :root => dir + end + end + + end +end \ No newline at end of file diff --git a/lib/riemann/dash/controller/css.rb b/lib/riemann/dash/controller/css.rb index 556418f..4de6e2b 100644 --- a/lib/riemann/dash/controller/css.rb +++ b/lib/riemann/dash/controller/css.rb @@ -1,4 +1,4 @@ -class Riemann::Dash +class Riemann::Dash::App get '/css' do scss :css, :layout => false end diff --git a/lib/riemann/dash/controller/index.rb b/lib/riemann/dash/controller/index.rb index 0d94a09..49bf84f 100644 --- a/lib/riemann/dash/controller/index.rb +++ b/lib/riemann/dash/controller/index.rb @@ -1,4 +1,4 @@ -class Riemann::Dash +class Riemann::Dash::App require 'multi_json' require 'fileutils' require 'set' diff --git a/lib/riemann/dash/version.rb b/lib/riemann/dash/version.rb index 5def92e..d19df91 100644 --- a/lib/riemann/dash/version.rb +++ b/lib/riemann/dash/version.rb @@ -1,4 +1,4 @@ module Riemann; end -class Riemann::Dash +module Riemann::Dash VERSION = '0.2.2' end