9
9
10
10
SCHEDULER = Rufus ::Scheduler . new
11
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
12
+ def development?
13
+ ENV [ 'RACK_ENV' ] == 'development'
19
14
end
20
15
21
- set server : 'thin' , connections : [ ] , history_file : 'history.yml'
16
+ def production?
17
+ ENV [ 'RACK_ENV' ] == 'production'
18
+ end
22
19
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
20
+ helpers Sinatra :: ContentFor
21
+ helpers do
22
+ def protected!
23
+ # override with auth logic
27
24
end
28
25
end
29
26
27
+ set :root , Dir . pwd
28
+ set :sprockets , Sprockets ::Environment . new ( settings . root )
29
+ set :assets_prefix , '/assets'
30
+ set :digest_assets , false
31
+ set server : 'thin' , connections : [ ] , history_file : 'history.yml'
32
+ set :public_folder , File . join ( settings . root , 'public' )
33
+ set :views , File . join ( settings . root , 'dashboards' )
34
+ set :default_dashboard , nil
35
+ set :auth_token , nil
36
+
30
37
if File . exists? ( settings . history_file )
31
38
set history : YAML . load_file ( settings . history_file )
32
39
else
33
40
set history : { }
34
41
end
35
42
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
43
+ %w( javascripts stylesheets fonts images ) . each do |path |
44
+ settings . sprockets . append_path ( "assets/#{ path } " )
45
+ end
40
46
41
- helpers Sinatra ::ContentFor
42
- helpers do
43
- def protected!
44
- # override with auth logic
45
- end
47
+ [ 'widgets' , File . expand_path ( '../../javascripts' , __FILE__ ) ] . each do |path |
48
+ settings . sprockets . append_path ( path )
49
+ end
50
+
51
+ not_found do
52
+ send_file File . join ( settings . public_folder , '404.html' )
53
+ end
54
+
55
+ at_exit do
56
+ File . write ( settings . history_file , settings . history . to_yaml )
57
+ end
58
+
59
+ get '/' do
60
+ protected!
61
+ dashboard = settings . default_dashboard || first_dashboard
62
+ raise Exception . new ( 'There are no dashboards available' ) if not dashboard
63
+
64
+ redirect "/" + dashboard
46
65
end
47
66
48
67
get '/events' , provides : 'text/event-stream' do
@@ -55,15 +74,6 @@ def protected!
55
74
end
56
75
end
57
76
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
77
get '/:dashboard' do
68
78
protected!
69
79
tilt_html_engines . each do |suffix , _ |
@@ -74,14 +84,6 @@ def protected!
74
84
halt 404
75
85
end
76
86
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
87
post '/dashboards/:id' do
86
88
request . body . rewind
87
89
body = JSON . parse ( request . body . read )
@@ -109,16 +111,12 @@ def protected!
109
111
end
110
112
end
111
113
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'
114
+ get '/views/:widget?.html' do
115
+ protected!
116
+ tilt_html_engines . each do |suffix , engines |
117
+ file = File . join ( settings . root , "widgets" , params [ :widget ] , "#{ params [ :widget ] } .#{ suffix } " )
118
+ return engines . first . new ( file ) . render if File . exist? file
119
+ end
122
120
end
123
121
124
122
def send_event ( id , body , target = nil )
@@ -154,14 +152,16 @@ def tilt_html_engines
154
152
end
155
153
end
156
154
157
- settings_file = File . join ( settings . root , 'config/settings.rb' )
158
- if ( File . exists? ( settings_file ) )
159
- require settings_file
155
+ def require_glob ( relative_glob )
156
+ Dir [ File . join ( settings . root , relative_glob ) ] . each do |file |
157
+ require file
158
+ end
160
159
end
161
160
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.
161
+ settings_file = File . join ( settings . root , 'config/settings .rb' )
162
+ require settings_file if File . exists? ( settings_file )
164
163
164
+ { } . to_json # Forces your json codec to initialize (in the event that it is lazily loaded). Does this before job threads start.
165
165
job_path = ENV [ "JOB_PATH" ] || 'jobs'
166
- files = Dir [ File . join ( settings . root , job_path , '**' , '/ *.rb' ) ]
167
- files . each { | job | require ( job ) }
166
+ require_glob ( File . join ( 'lib' , '**' , '*.rb' ) )
167
+ require_glob ( File . join ( job_path , '**' , '*.rb' ) )
0 commit comments