-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathapp.rb
130 lines (105 loc) · 2.86 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
$stdout.sync = true
require 'dotenv'
Dotenv.load
require 'sinatra'
require 'sinatra/reloader' if development?
require 'sinatra/activerecord'
# Adding this to make sure that Timezones get formatted correctly http://pivotallabs.com/utc-vs-ruby-activerecord-sinatra-heroku-and-postgres/
Time.zone = 'UTC'
ActiveRecord::Base.default_timezone = :utc
require 'rack-flash'
require 'haml'
require './pingpong_config'
config = PingpongConfig
enable :sessions
use Rack::Flash
set :database_file, "database.yml"
if http_username = config[:http_username]
use Rack::Auth::Basic, "Pingpong Pros Only!" do |username, password|
username == config[:http_username] and password == config[:http_password]
end
end
get '/' do
@config = config
@notice = flash[:notice]
@error = flash[:error]
@checks = Check.all
haml :index
end
get '/check/new' do
@config = config
@error = flash[:error]
haml :new
end
delete '/check/:check_id/delete' do
check = Check.find(params[:check_id])
if check
flash[:notice] = "Removed check '#{check.name}'."
Check.find(params[:check_id]).destroy
end
redirect '/'
end
post '/check/create' do
check = Check.new
update_check(check, params)
if check.save
flash[:notice] = "Created a new check: #{params[:name]}"
redirect '/'
else
errorMsg = check.errors.full_messages.join(" ")
flash[:error] = "Could not save the new check. #{errorMsg}"
redirect '/check/new'
end
end
get '/check/:check_id/show' do
@notice = flash[:notice]
@config = config
@check = Check.find(params[:check_id])
haml :check
end
get '/check/:check_id/edit' do
@config = config
@check = Check.find(params[:check_id])
haml :edit
end
post '/check/:check_id/update' do
check = Check.find(params[:check_id])
check = update_check(check, params)
if check.nil?
flash[:error] = "No such check to edit."
redirect '/'
else
check.save!
flash[:notice] = "Updated check: #{check.name}."
redirect '/check/' + check.id.to_s + '/show'
end
end
def update_check(check, params)
check.name = params[:name]
check.url = params[:url]
check.method = params[:method]
check.frequency = params[:frequency]
check.custom_properties = params[:custom_properties]
check.data = params[:data]
check.save_body = params[:save_body] == 'true'
check.http_username = params[:http_username]
check.http_password = params[:http_password]
check.email_warn = params[:email_warn] == 'on'
check.slack_warn = params[:slack_warn] == 'on'
check.slack_bad = params[:slack_bad] == 'on'
check
end
# This first check makes sure we don't run his when doing
# migrations or other rake tasks.
if File.split($0).last != 'rake'
unless config['skip_pushpop']
require 'pushpop'
Dir.glob("#{File.dirname(__FILE__)}/jobs/**/*.rb").each { |file|
require file
}
Pushpop.schedule
Thread.new {
Clockwork.manager.run
}
end
end