require 'sinatra/base' require 'json' require 'date' require 'digest/md5' require_relative 'cpu_usage' require_relative 'network_usage' require_relative 'usage_manager' require_relative 'rotator' require_relative 'log_manager' require_relative 'services_manager' module ServState class Application < Sinatra::Base configure do ServicesManager.start enable :sessions set :environment, :test set :run, true set :logging, false set :show_exceptions, false set :raise_errors, false set :hostname, ServicesManager.hostname end before /^((?!\/login))/ do check_auth end helpers do def check_auth if settings.password unless Digest::MD5.hexdigest(settings.password) == session[:login] redirect '/login' end end end end get '/' do @name = settings.hostname erb :index end get '/renew.json' do content_type :json ServicesManager.json_for_dashboard end get '/charts/:type' do @name = settings.hostname @type = case params[:type] when '1day' then "1 day charts" when '10min' then "10 min charts" else redirect '404' end erb :charts end get '/charts/:type/renew.json' do content_type :json ServicesManager.json_for_charts(params[:type]) end get '/login' do session[:login] = nil; @name = settings.hostname erb :login end post '/login' do unless(settings.password) redirect '/' end if(params[:pass]) == settings.password session[:login] = Digest::MD5.hexdigest(settings.password) redirect '/' else redirect '/login' end end # error do # @name = settings.hostname # @header = 'Error!' # @text = "Sorry, but something went wrong." # erb :error # end not_found do @name = settings.hostname @header = 'Not found!' @text = "That page doesn't exist" erb :error end class << self def set_settings &block class_eval(&block) end private def password(value) set :password, value end def port(value) set :port, value end end end end