lib/dashing/app.rb in smashing-1.0.0 vs lib/dashing/app.rb in smashing-1.1.0
- old
+ new
@@ -32,20 +32,23 @@
set :root, Dir.pwd
set :sprockets, Sprockets::Environment.new(settings.root)
set :assets_prefix, '/assets'
set :digest_assets, false
-set server: 'thin', connections: [], history_file: 'history.yml'
+set :server, 'thin'
+set :connections, []
+set :history_file, 'history.yml'
set :public_folder, File.join(settings.root, 'public')
set :views, File.join(settings.root, 'dashboards')
set :default_dashboard, nil
set :auth_token, nil
+set :template_languages, %i[html erb]
-if File.exists?(settings.history_file)
- set history: YAML.load_file(settings.history_file)
+if File.exist?(settings.history_file)
+ set :history, YAML.load_file(settings.history_file)
else
- set history: {}
+ set :history, {}
end
%w(javascripts stylesheets fonts images).each do |path|
settings.sprockets.append_path("assets/#{path}")
end
@@ -53,11 +56,11 @@
['widgets', File.expand_path('../../../javascripts', __FILE__)]. each do |path|
settings.sprockets.append_path(path)
end
not_found do
- send_file File.join(settings.public_folder, '404.html'), status: 404
+ send_file File.join(settings.public_folder, '404.html'), :status => 404
end
at_exit do
File.write(settings.history_file, settings.history.to_yaml)
end
@@ -68,11 +71,11 @@
raise Exception.new('There are no dashboards available') if not dashboard
redirect "/" + dashboard
end
-get '/events', provides: 'text/event-stream' do
+get '/events', :provides => 'text/event-stream' do
protected!
response.headers['X-Accel-Buffering'] = 'no' # Disable buffering for nginx
stream :keep_open do |out|
settings.connections << out
out << latest_events
@@ -80,13 +83,13 @@
end
end
get '/:dashboard' do
protected!
- tilt_html_engines.each do |suffix, _|
- file = File.join(settings.views, "#{params[:dashboard]}.#{suffix}")
- return render(suffix.to_sym, params[:dashboard].to_sym) if File.exist? file
+ settings.template_languages.each do |language|
+ file = File.join(settings.views, "#{params[:dashboard]}.#{language}")
+ return render(language, params[:dashboard].to_sym) if File.exist?(file)
end
halt 404
end
@@ -115,14 +118,16 @@
end
end
get '/views/:widget?.html' do
protected!
- tilt_html_engines.each do |suffix, engines|
- file = File.join(settings.root, "widgets", params[:widget], "#{params[:widget]}.#{suffix}")
- return engines.first.new(file).render if File.exist? file
+ settings.template_languages.each do |language|
+ file = File.join(settings.root, "widgets", params[:widget], "#{params[:widget]}.#{language}")
+ return Tilt[language].new(file).render if File.exist?(file)
end
+
+ "Drats! Unable to find a widget file named: #{params[:widget]} to render."
end
Thin::Server.class_eval do
def stop_with_connection_closing
Sinatra::Application.settings.connections.dup.each(&:close)
@@ -133,11 +138,11 @@
alias_method :stop, :stop_with_connection_closing
end
def send_event(id, body, target=nil)
body[:id] = id
- body[:updatedAt] ||= Time.now.to_i
+ body[:updatedAt] ||= (Time.now.to_f * 1000.0).to_i
event = format_event(body.to_json, target)
Sinatra::Application.settings.history[id] = event unless target == 'dashboards'
Sinatra::Application.settings.connections.each { |out| out << event }
end
@@ -157,24 +162,17 @@
files = Dir[File.join(settings.views, '*')].collect { |f| File.basename(f, '.*') }
files -= ['layout']
files.sort.first
end
-def tilt_html_engines
- Tilt.mappings.select do |_, engines|
- default_mime_type = engines.first.default_mime_type
- default_mime_type.nil? || default_mime_type == 'text/html'
- end
-end
-
def require_glob(relative_glob)
Dir[File.join(settings.root, relative_glob)].each do |file|
require file
end
end
settings_file = File.join(settings.root, 'config/settings.rb')
-require settings_file if File.exists?(settings_file)
+require settings_file if File.exist?(settings_file)
{}.to_json # Forces your json codec to initialize (in the event that it is lazily loaded). Does this before job threads start.
job_path = ENV["JOB_PATH"] || 'jobs'
require_glob(File.join('lib', '**', '*.rb'))
require_glob(File.join(job_path, '**', '*.rb'))