-
Notifications
You must be signed in to change notification settings - Fork 3
/
stompbox.rb
148 lines (122 loc) · 3.66 KB
/
stompbox.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#
# Copyright 2011 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
$: << File.join(File.dirname(__FILE__), 'app')
require 'rubygems'
require 'sinatra/base'
require 'rack-flash'
require 'haml'
require 'sass'
require 'yaml'
require 'authentication'
require 'deployer'
require 'helpers'
require 'models'
if ENV['RACK_ENV'].to_s == 'development'
puts "StompBox: DATABASE_URL = #{ENV['DATABASE_URL']}"
puts "StompBox: DEPLOYMENTS = #{ENV['DEPLOYMENTS']}"
puts "StompBox: AUTO_MIGRATE = #{ENV['AUTO_MIGRATE']}"
puts "StompBox: API_KEY = #{ENV['API_KEY']}"
end
module StompBox
class Application < Sinatra::Base
include StompBox::Authentication
use Rack::Flash
enable :sessions, :logging, :method_override, :static
set :public, Proc.new { File.join(root, "public") }
set :root, Proc.new { File.expand_path(File.dirname(__FILE__)) }
set :views, Proc.new { File.join(File.dirname(__FILE__), "app", "views") }
if ENV['REQUIRE_AUTHENTICATION']
['*/push/*','*/login','*/logout', '*.js', '/*.css' ].each do |p|
before p do
skip_authentication
end
end
before do
require_authentication
end
else
puts "StompBox: REQUIRE_AUTHENTICATION is not set, *disabling* authentication" if ENV['REQUIRE_AUTHENTICATION'].nil?
end
post '/deploy' do
if (params[:id] && (push = Push.get(params[:id])))
Deployer.deploy(push)
end
redirect to('/')
end
post '/undeploy' do
if (params[:id] && (push = Push.get(params[:id])))
Deployer.undeploy(push)
end
redirect to('/')
end
# Post a deployment
post '/push/:api_key' do
if params[:payload] && (params[:api_key] == config('API_KEY'))
push = Push.create(:payload=>params[:payload], :created_at=>Time.now)
push.save if push
end
redirect to('/')
end
# List all deployments
get '/' do
@pushes = Push.all(:order => [ :created_at.desc ])
haml :'pushes/index'
end
get '/repositories' do
repositories
haml :'repositories/index'
end
post '/repositories' do
if params[:repository]
repo = Repository.create(params[:repository])
flash[:error] = repo.errors unless repo.save
end
redirect to("/repositories")
end
delete '/repositories/:id' do
if repo = Repository.get(params[:id])
repo.destroy
end
redirect to("/repositories")
end
put '/repositories/:id' do
if repo = Repository.get(params[:id])
repo.update(params[:repository])
repo.save
end
redirect to("repositories")
end
# Stylesheets - reset
get '/css/html5reset.css' do
sass :'/css/html5reset'
end
# App stylesheet
get '/css/styles.css' do
scss :'/css/styles'
end
get '/login' do
haml :'sessions/new'
end
post '/login' do
flash[:notice] = "Bad credentials. Try again?" unless authenticate( params[:user], params[:password] )
redirect to('/')
end
get '/logout' do
logout
end
run! if app_file == $0
end
end