lib/dashing.rb in dashing-1.0.6 vs lib/dashing.rb in dashing-1.1.0
- old
+ new
@@ -3,10 +3,11 @@
require 'sinatra/content_for'
require 'rufus/scheduler'
require 'coffee-script'
require 'sass'
require 'json'
+require 'yaml'
SCHEDULER = Rufus::Scheduler.start_new
set :root, Dir.pwd
@@ -15,11 +16,25 @@
set :digest_assets, false
['assets/javascripts', 'assets/stylesheets', 'assets/fonts', 'assets/images', 'widgets', File.expand_path('../../javascripts', __FILE__)]. each do |path|
settings.sprockets.append_path path
end
-set server: 'thin', connections: [], history: {}
+set server: 'thin', connections: [], history_file: 'history.yml'
+
+# Persist history in tmp file at exit
+at_exit do
+ File.open(settings.history_file, 'w') do |f|
+ f.puts settings.history.to_yaml
+ end
+end
+
+if File.exists?(settings.history_file)
+ set history: YAML.load_file(settings.history_file)
+else
+ set history: {}
+end
+
set :public_folder, File.join(settings.root, 'public')
set :views, File.join(settings.root, 'dashboards')
set :default_dashboard, nil
set :auth_token, nil
@@ -48,21 +63,24 @@
end
end
get '/:dashboard' do
protected!
- if File.exist? File.join(settings.views, "#{params[:dashboard]}.erb")
- erb params[:dashboard].to_sym
- else
- halt 404
+ 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
end
+
+ halt 404
end
get '/views/:widget?.html' do
protected!
- widget = params[:widget]
- send_file File.join(settings.root, 'widgets', widget, "#{widget}.html")
+ 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
+ end
end
post '/widgets/:id' do
request.body.rewind
body = JSON.parse(request.body.read)
@@ -105,12 +123,19 @@
str << body
end
end
def first_dashboard
- files = Dir[File.join(settings.views, '*.erb')].collect { |f| f.match(/(\w*).erb/)[1] }
+ files = Dir[File.join(settings.views, '*')].collect { |f| File.basename(f, '.*') }
files -= ['layout']
files.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
Dir[File.join(settings.root, 'lib', '**', '*.rb')].each {|file| require file }
{}.to_json # Forces your json codec to initialize (in the event that it is lazily loaded). Does this before job threads start.