summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/dashing.rb2
-rw-r--r--lib/dashing/app.rb24
-rw-r--r--lib/dashing/cli.rb12
3 files changed, 26 insertions, 12 deletions
diff --git a/lib/dashing.rb b/lib/dashing.rb
index 855aa36..3527034 100644
--- a/lib/dashing.rb
+++ b/lib/dashing.rb
@@ -3,4 +3,4 @@ require 'dashing/downloader'
require 'dashing/app'
module Dashing
-end
+end \ No newline at end of file
diff --git a/lib/dashing/app.rb b/lib/dashing/app.rb
index 933ef5b..0ff3085 100644
--- a/lib/dashing/app.rb
+++ b/lib/dashing/app.rb
@@ -6,6 +6,7 @@ require 'coffee-script'
require 'sass'
require 'json'
require 'yaml'
+require 'thin'
SCHEDULER = Rufus::Scheduler.new
@@ -22,6 +23,11 @@ helpers do
def protected!
# override with auth logic
end
+
+ def authenticated?(token)
+ return true unless settings.auth_token
+ token && Rack::Utils.secure_compare(settings.auth_token, token)
+ end
end
set :root, Dir.pwd
@@ -49,7 +55,7 @@ end
end
not_found do
- send_file File.join(settings.public_folder, '404.html')
+ send_file File.join(settings.public_folder, '404.html'), status: 404
end
at_exit do
@@ -88,8 +94,7 @@ post '/dashboards/:id' do
request.body.rewind
body = JSON.parse(request.body.read)
body['dashboard'] ||= params['id']
- auth_token = body.delete("auth_token")
- if !settings.auth_token || settings.auth_token == auth_token
+ if authenticated?(body.delete("auth_token"))
send_event(params['id'], body, 'dashboards')
204 # response without entity body
else
@@ -101,8 +106,7 @@ end
post '/widgets/:id' do
request.body.rewind
body = JSON.parse(request.body.read)
- auth_token = body.delete("auth_token")
- if !settings.auth_token || settings.auth_token == auth_token
+ if authenticated?(body.delete("auth_token"))
send_event(params['id'], body)
204 # response without entity body
else
@@ -120,6 +124,16 @@ get '/views/:widget?.html' do
"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)
+ stop_without_connection_closing
+ end
+
+ alias_method :stop_without_connection_closing, :stop
+ alias_method :stop, :stop_with_connection_closing
+end
+
def send_event(id, body, target=nil)
body[:id] = id
body[:updatedAt] ||= Time.now.to_i
diff --git a/lib/dashing/cli.rb b/lib/dashing/cli.rb
index 27d8f6b..4b93f89 100644
--- a/lib/dashing/cli.rb
+++ b/lib/dashing/cli.rb
@@ -38,12 +38,12 @@ module Dashing
puts "Invalid generator. Either use widget, dashboard, or job"
end
- desc "install GIST_ID", "Installs a new widget from a gist."
- def install(gist_id)
+ desc "install GIST_ID [--skip]", "Installs a new widget from a gist (skip overwrite)."
+ def install(gist_id, *args)
gist = Downloader.get_gist(gist_id)
public_url = "https://gist.github.com/#{gist_id}"
- install_widget_from_gist(gist)
+ install_widget_from_gist(gist, args.include?('--skip'))
print set_color("Don't forget to edit the ", :yellow)
print set_color("Gemfile ", :yellow, :bold)
@@ -89,15 +89,15 @@ module Dashing
system(command)
end
- def install_widget_from_gist(gist)
+ def install_widget_from_gist(gist, skip_overwrite)
gist['files'].each do |file, details|
if file =~ /\.(html|coffee|scss)\z/
widget_name = File.basename(file, '.*')
new_path = File.join(Dir.pwd, 'widgets', widget_name, file)
- create_file(new_path, details['content'])
+ create_file(new_path, details['content'], :skip => skip_overwrite)
elsif file.end_with?('.rb')
new_path = File.join(Dir.pwd, 'jobs', file)
- create_file(new_path, details['content'])
+ create_file(new_path, details['content'], :skip => skip_overwrite)
end
end
end