Skip to content
This repository was archived by the owner on Mar 7, 2018. It is now read-only.

Commit fbc9497

Browse files
committed
moving app to it's own file under lib/dashing
1 parent 0c1adbd commit fbc9497

File tree

3 files changed

+323
-319
lines changed

3 files changed

+323
-319
lines changed

lib/dashing.rb

Lines changed: 2 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -1,170 +1,6 @@
1-
require 'sinatra'
2-
require 'sprockets'
3-
require 'sinatra/content_for'
4-
require 'rufus/scheduler'
5-
require 'coffee-script'
6-
require 'sass'
7-
require 'json'
8-
require 'yaml'
9-
101
require 'dashing/cli'
112
require 'dashing/downloader'
3+
require 'dashing/app'
124

13-
SCHEDULER = Rufus::Scheduler.new
14-
15-
set :root, Dir.pwd
16-
17-
set :sprockets, Sprockets::Environment.new(settings.root)
18-
set :assets_prefix, '/assets'
19-
set :digest_assets, false
20-
['assets/javascripts', 'assets/stylesheets', 'assets/fonts', 'assets/images', 'widgets', File.expand_path('../../javascripts', __FILE__)]. each do |path|
21-
settings.sprockets.append_path path
22-
end
23-
24-
set server: 'thin', connections: [], history_file: 'history.yml'
25-
26-
# Persist history in tmp file at exit
27-
at_exit do
28-
File.open(settings.history_file, 'w') do |f|
29-
f.puts settings.history.to_yaml
30-
end
31-
end
32-
33-
if File.exists?(settings.history_file)
34-
set history: YAML.load_file(settings.history_file)
35-
else
36-
set history: {}
37-
end
38-
39-
set :public_folder, File.join(settings.root, 'public')
40-
set :views, File.join(settings.root, 'dashboards')
41-
set :default_dashboard, nil
42-
set :auth_token, nil
43-
44-
helpers Sinatra::ContentFor
45-
helpers do
46-
def protected!
47-
# override with auth logic
48-
end
49-
end
50-
51-
get '/events', provides: 'text/event-stream' do
52-
protected!
53-
response.headers['X-Accel-Buffering'] = 'no' # Disable buffering for nginx
54-
stream :keep_open do |out|
55-
settings.connections << out
56-
out << latest_events
57-
out.callback { settings.connections.delete(out) }
58-
end
59-
end
60-
61-
get '/' do
62-
protected!
63-
begin
64-
redirect "/" + (settings.default_dashboard || first_dashboard).to_s
65-
rescue NoMethodError => e
66-
raise Exception.new("There are no dashboards in your dashboard directory.")
67-
end
68-
end
69-
70-
get '/:dashboard' do
71-
protected!
72-
tilt_html_engines.each do |suffix, _|
73-
file = File.join(settings.views, "#{params[:dashboard]}.#{suffix}")
74-
return render(suffix.to_sym, params[:dashboard].to_sym) if File.exist? file
75-
end
76-
77-
halt 404
78-
end
79-
80-
get '/views/:widget?.html' do
81-
protected!
82-
tilt_html_engines.each do |suffix, engines|
83-
file = File.join(settings.root, "widgets", params[:widget], "#{params[:widget]}.#{suffix}")
84-
return engines.first.new(file).render if File.exist? file
85-
end
86-
end
87-
88-
post '/dashboards/:id' do
89-
request.body.rewind
90-
body = JSON.parse(request.body.read)
91-
body['dashboard'] ||= params['id']
92-
auth_token = body.delete("auth_token")
93-
if !settings.auth_token || settings.auth_token == auth_token
94-
send_event(params['id'], body, 'dashboards')
95-
204 # response without entity body
96-
else
97-
status 401
98-
"Invalid API key\n"
99-
end
5+
module Dashing
1006
end
101-
102-
post '/widgets/:id' do
103-
request.body.rewind
104-
body = JSON.parse(request.body.read)
105-
auth_token = body.delete("auth_token")
106-
if !settings.auth_token || settings.auth_token == auth_token
107-
send_event(params['id'], body)
108-
204 # response without entity body
109-
else
110-
status 401
111-
"Invalid API key\n"
112-
end
113-
end
114-
115-
not_found do
116-
send_file File.join(settings.public_folder, '404.html')
117-
end
118-
119-
def development?
120-
ENV['RACK_ENV'] == 'development'
121-
end
122-
123-
def production?
124-
ENV['RACK_ENV'] == 'production'
125-
end
126-
127-
def send_event(id, body, target=nil)
128-
body[:id] = id
129-
body[:updatedAt] ||= Time.now.to_i
130-
event = format_event(body.to_json, target)
131-
Sinatra::Application.settings.history[id] = event unless target == 'dashboards'
132-
Sinatra::Application.settings.connections.each { |out| out << event }
133-
end
134-
135-
def format_event(body, name=nil)
136-
str = ""
137-
str << "event: #{name}\n" if name
138-
str << "data: #{body}\n\n"
139-
end
140-
141-
def latest_events
142-
settings.history.inject("") do |str, (id, body)|
143-
str << body
144-
end
145-
end
146-
147-
def first_dashboard
148-
files = Dir[File.join(settings.views, '*')].collect { |f| File.basename(f, '.*') }
149-
files -= ['layout']
150-
files.sort.first
151-
end
152-
153-
def tilt_html_engines
154-
Tilt.mappings.select do |_, engines|
155-
default_mime_type = engines.first.default_mime_type
156-
default_mime_type.nil? || default_mime_type == 'text/html'
157-
end
158-
end
159-
160-
settings_file = File.join(settings.root, 'config/settings.rb')
161-
if (File.exists?(settings_file))
162-
require settings_file
163-
end
164-
165-
Dir[File.join(settings.root, 'lib', '**', '*.rb')].each {|file| require file }
166-
{}.to_json # Forces your json codec to initialize (in the event that it is lazily loaded). Does this before job threads start.
167-
168-
job_path = ENV["JOB_PATH"] || 'jobs'
169-
files = Dir[File.join(settings.root, job_path, '**', '/*.rb')]
170-
files.each { |job| require(job) }

lib/dashing/app.rb

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
require 'sinatra'
2+
require 'sprockets'
3+
require 'sinatra/content_for'
4+
require 'rufus/scheduler'
5+
require 'coffee-script'
6+
require 'sass'
7+
require 'json'
8+
require 'yaml'
9+
10+
SCHEDULER = Rufus::Scheduler.new
11+
12+
set :root, Dir.pwd
13+
14+
set :sprockets, Sprockets::Environment.new(settings.root)
15+
set :assets_prefix, '/assets'
16+
set :digest_assets, false
17+
['assets/javascripts', 'assets/stylesheets', 'assets/fonts', 'assets/images', 'widgets', File.expand_path('../../javascripts', __FILE__)]. each do |path|
18+
settings.sprockets.append_path path
19+
end
20+
21+
set server: 'thin', connections: [], history_file: 'history.yml'
22+
23+
# Persist history in tmp file at exit
24+
at_exit do
25+
File.open(settings.history_file, 'w') do |f|
26+
f.puts settings.history.to_yaml
27+
end
28+
end
29+
30+
if File.exists?(settings.history_file)
31+
set history: YAML.load_file(settings.history_file)
32+
else
33+
set history: {}
34+
end
35+
36+
set :public_folder, File.join(settings.root, 'public')
37+
set :views, File.join(settings.root, 'dashboards')
38+
set :default_dashboard, nil
39+
set :auth_token, nil
40+
41+
helpers Sinatra::ContentFor
42+
helpers do
43+
def protected!
44+
# override with auth logic
45+
end
46+
end
47+
48+
get '/events', provides: 'text/event-stream' do
49+
protected!
50+
response.headers['X-Accel-Buffering'] = 'no' # Disable buffering for nginx
51+
stream :keep_open do |out|
52+
settings.connections << out
53+
out << latest_events
54+
out.callback { settings.connections.delete(out) }
55+
end
56+
end
57+
58+
get '/' do
59+
protected!
60+
begin
61+
redirect "/" + (settings.default_dashboard || first_dashboard).to_s
62+
rescue NoMethodError => e
63+
raise Exception.new("There are no dashboards in your dashboard directory.")
64+
end
65+
end
66+
67+
get '/:dashboard' do
68+
protected!
69+
tilt_html_engines.each do |suffix, _|
70+
file = File.join(settings.views, "#{params[:dashboard]}.#{suffix}")
71+
return render(suffix.to_sym, params[:dashboard].to_sym) if File.exist? file
72+
end
73+
74+
halt 404
75+
end
76+
77+
get '/views/:widget?.html' do
78+
protected!
79+
tilt_html_engines.each do |suffix, engines|
80+
file = File.join(settings.root, "widgets", params[:widget], "#{params[:widget]}.#{suffix}")
81+
return engines.first.new(file).render if File.exist? file
82+
end
83+
end
84+
85+
post '/dashboards/:id' do
86+
request.body.rewind
87+
body = JSON.parse(request.body.read)
88+
body['dashboard'] ||= params['id']
89+
auth_token = body.delete("auth_token")
90+
if !settings.auth_token || settings.auth_token == auth_token
91+
send_event(params['id'], body, 'dashboards')
92+
204 # response without entity body
93+
else
94+
status 401
95+
"Invalid API key\n"
96+
end
97+
end
98+
99+
post '/widgets/:id' do
100+
request.body.rewind
101+
body = JSON.parse(request.body.read)
102+
auth_token = body.delete("auth_token")
103+
if !settings.auth_token || settings.auth_token == auth_token
104+
send_event(params['id'], body)
105+
204 # response without entity body
106+
else
107+
status 401
108+
"Invalid API key\n"
109+
end
110+
end
111+
112+
not_found do
113+
send_file File.join(settings.public_folder, '404.html')
114+
end
115+
116+
def development?
117+
ENV['RACK_ENV'] == 'development'
118+
end
119+
120+
def production?
121+
ENV['RACK_ENV'] == 'production'
122+
end
123+
124+
def send_event(id, body, target=nil)
125+
body[:id] = id
126+
body[:updatedAt] ||= Time.now.to_i
127+
event = format_event(body.to_json, target)
128+
Sinatra::Application.settings.history[id] = event unless target == 'dashboards'
129+
Sinatra::Application.settings.connections.each { |out| out << event }
130+
end
131+
132+
def format_event(body, name=nil)
133+
str = ""
134+
str << "event: #{name}\n" if name
135+
str << "data: #{body}\n\n"
136+
end
137+
138+
def latest_events
139+
settings.history.inject("") do |str, (id, body)|
140+
str << body
141+
end
142+
end
143+
144+
def first_dashboard
145+
files = Dir[File.join(settings.views, '*')].collect { |f| File.basename(f, '.*') }
146+
files -= ['layout']
147+
files.sort.first
148+
end
149+
150+
def tilt_html_engines
151+
Tilt.mappings.select do |_, engines|
152+
default_mime_type = engines.first.default_mime_type
153+
default_mime_type.nil? || default_mime_type == 'text/html'
154+
end
155+
end
156+
157+
settings_file = File.join(settings.root, 'config/settings.rb')
158+
if (File.exists?(settings_file))
159+
require settings_file
160+
end
161+
162+
Dir[File.join(settings.root, 'lib', '**', '*.rb')].each {|file| require file }
163+
{}.to_json # Forces your json codec to initialize (in the event that it is lazily loaded). Does this before job threads start.
164+
165+
job_path = ENV["JOB_PATH"] || 'jobs'
166+
files = Dir[File.join(settings.root, job_path, '**', '/*.rb')]
167+
files.each { |job| require(job) }

0 commit comments

Comments
 (0)