From 1bd5d6eb0d38b4d09902c352a5b086263b65d92a Mon Sep 17 00:00:00 2001 From: Roman Heinrich Date: Mon, 11 Mar 2013 18:06:02 +0200 Subject: [PATCH] index controller clean --- lib/riemann/dash/config.rb | 58 +++++++++++++++++++++++++--- lib/riemann/dash/controller/index.rb | 40 +++---------------- 2 files changed, 58 insertions(+), 40 deletions(-) diff --git a/lib/riemann/dash/config.rb b/lib/riemann/dash/config.rb index 54a4f15..ac2d59a 100644 --- a/lib/riemann/dash/config.rb +++ b/lib/riemann/dash/config.rb @@ -1,12 +1,20 @@ class Riemann::Dash::Config attr_accessor :config_path attr_accessor :store - def initialize(config_path) - @config_path = config_path - @store = {} + + def initialize + self.store = {} setup_default_values end + def self.instance + @instance ||= Riemann::Dash::Config.new + end + + def self.reset! + @instance = nil + end + def setup_default_values store.merge!({ :controllers => [File.join(File.dirname(__FILE__), 'controller')], @@ -16,9 +24,13 @@ class Riemann::Dash::Config }) end + def ws_config_file + store[:ws_config] + end # Executes the configuration file. - def load_config + def load_config(path) + self.config_path = path begin Riemann::Dash::App.instance_eval File.read(config_path) true @@ -27,7 +39,6 @@ class Riemann::Dash::Config end end - def load_controllers store[:controllers].each { |d| load_controllers_from(d) } end @@ -93,4 +104,41 @@ class Riemann::Dash::Config end end + + require 'multi_json' + require 'fileutils' + require 'set' + + + def read_ws_config + if File.exists? ws_config_file + File.read(ws_config_file) + else + MultiJson.encode({}) + end + end + + def update_ws_config(update) + update = MultiJson.decode(update) + # Read old config + if File.exists? ws_config_file + old = MultiJson.decode File.read(ws_config_file) + else + old = {} + end + + new_config = {} + + # Server + new_config['server'] = update['server'] or old['server'] + + p update['workspaces'] + new_config['workspaces'] = update['workspaces'] or old['workspaces'] + + # Save new config + FileUtils.mkdir_p 'config' + File.open(ws_config_file, 'w') do |f| + f.write(MultiJson.encode(new_config)) + end + end end \ No newline at end of file diff --git a/lib/riemann/dash/controller/index.rb b/lib/riemann/dash/controller/index.rb index 4b5071b..82bb445 100644 --- a/lib/riemann/dash/controller/index.rb +++ b/lib/riemann/dash/controller/index.rb @@ -1,50 +1,20 @@ class Riemann::Dash::App - require 'multi_json' - require 'fileutils' - require 'set' - - WS_CONFIG_FILE = @config.store[:ws_config] - get '/' do erb :index, :layout => false end get '/config', :provides => 'json' do - if File.exists? WS_CONFIG_FILE - send_file WS_CONFIG_FILE, :type => :json - else - MultiJson.encode({}) - end + content_type "application/json" + config.read_ws_config end post '/config' do # Read update request.body.rewind - update = MultiJson.decode(request.body.read) - - # Read old config - if File.exists? WS_CONFIG_FILE - old = MultiJson.decode File.read WS_CONFIG_FILE - else - old = {} - end - - new_config = {} - - # Server - new_config['server'] = update['server'] or old['server'] - - p update['workspaces'] - new_config['workspaces'] = update['workspaces'] or old['workspaces'] - - # Save new config - FileUtils.mkdir_p 'config' - File.open(WS_CONFIG_FILE, 'w') do |f| - f.write(MultiJson.encode(new_config)) - end + config.update_ws_config(request.body.read) # Return current config content_type "application/json" - MultiJson.encode(new_config) + config.read_ws_config end -end +end \ No newline at end of file