-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
135 lines (106 loc) · 2.59 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
131
132
133
134
135
require_relative "./config/env"
# require "sinatra/content_for"
# require "sinatra/streaming"
require 'json'
Logger.class_eval { alias :write :'<<' }
LOGG = Logger.new STDOUT
class App < Sinatra::Base
set :logger, LOGG
configure do
use Rack::CommonLogger, LOGG
end
include Voidtools::Sinatra::ViewHelpers
# oauth
enable :sessions
set :session_secret, CONFIG[:session_secret]
use OmniAuth::Builder do
provider :google_oauth2, CONFIG[:google_oauth_identifier], CONFIG[:google_oauth_secret]
end
OmniAuth.config.allowed_request_methods = %i( get )
set :show_exceptions, false
error do
content_type :json
error = env['sinatra.error']
{ error: { name: error.class, message: error.message, backtrace: error.backtrace } }.to_json
end
PUBLIC_URLS = %w(
/stream
/auth/google_oauth2
)
before do
if APP_ENV != "development"
if request.env['omniauth.auth'].nil? && !logged_in? && !(PUBLIC_URLS.include? request.path)
halt haml(:log_in)
end
else
halt haml(:log_in) if params[:login]
end
end
def logged_in?
current_user && ALLOWED_USERS.include?(current_user)
end
def current_user
session[:current_user]
end
post "/logout" do
session[:current_user] = nil
redirect "/"
end
get '/auth/:name/callback' do
auth = request.env['omniauth.auth']
user = auth.info.email
session[:current_user] = user
redirect "/"
end
# helpers
helpers do
def nav_link(label)
url = "/#{label.downcase}"
# klass = "active" if request.path =~ /^#{url}/
# haml_tag :li, class: klass do
# haml_tag :a, href: url do
# haml_concat label
# end
# end
"<li><a href=\"#{url}\">#{label}</a></li>"
end
end
# main
get "/" do
haml :index
end
get "/apps" do
haml :apps
end
get "/domains" do
haml :domains
end
get "/machines" do
haml :machines
end
# apps
post /\/apps\/(.+)\/actions/ do |app_name|
content_type :json
request.body.rewind
payload = JSON.parse(request.body.read)
action = payload["name"].to_sym
ActionJob.new.async.perform(
event: action,
app: app_name
)
{ name: app_name }.to_json
end
post "/apps" do
content_type :json
request.body.rewind
payload = JSON.parse(request.body.read)
app_name = payload["name"].to_sym
CreateJob.new.async.perform(
app: app_name
)
{ name: app_name }.to_json
end
get '/stream' do
[200, { "Content-Type" => "text/event-stream;charset=utf-8" }, EventStream.instance]
end
end