index controller clean

This commit is contained in:
Roman Heinrich 2013-03-11 18:06:02 +02:00
parent e54157ba0e
commit 1bd5d6eb0d
2 changed files with 58 additions and 40 deletions

View file

@ -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

View file

@ -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