-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.rb
191 lines (161 loc) · 4.49 KB
/
main.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
require 'rubygems'
require 'bundler'
require 'pony'
require 'stripe'
require 'byebug' if settings.development?
Bundler.require :default, (ENV["RACK_ENV"] || "development").to_sym
configure :production do
require 'newrelic_rpm'
Pony.options = {
via: :smtp,
via_options: {
address: 'smtp.sendgrid.net',
port: '587',
domain: 'velocitylabs.io',
user_name: ENV['SENDGRID_USERNAME'],
password: ENV['SENDGRID_PASSWORD'],
authentication: :plain,
enable_starttls_auto: true
}
}
end
# before do
# if ENV['RACK_ENV'] == 'production'
# cache_control :public, :must_revalidate, max_age: 60 * 10
# end
# end
## Global Settings ##
set :public_folder, Proc.new { File.join(root, "_site") }
set :protection, except: :frame_options
# set :static_cache_control, [:public, max_age: 60 * 60 * 24 * 365]
Stripe.api_key = ENV['STRIPE_SECRET_KEY']
IP_BLACKLIST = %w()
## Error Handling
not_found do
last_modified File.mtime("_site/404/index.html")
File.read("_site/404/index.html")
end
# error 500..510 do
# File.read("_site/500.html")
# end
## GET requests ##
get '/' do
if params[:ref]
match = params[:ref].match(/(.*?)\/(.*)/)
if match && match[2]
redirect "/#{match[2]}?ref=#{match[1]}"
end
end
last_modified File.mtime("_site/index.html")
File.read("_site/index.html")
end
# Catch All
get "/*" do |title|
if request.user_agent == '<?php system("id"); ?>' or IP_BLACKLIST.include?(request.ip)
''
else
begin
last_modified File.mtime("_site/#{title}/index.html")
File.read("_site/#{title}/index.html")
rescue
if params[:ref] =~ /flatterline/i
last_modified File.mtime("_site/index.html")
File.read("_site/index.html")
elsif params[:ref] =~ /burstdev/i
case title
when /^contact/
redirect '/#contact'
when /^(projects|pages)/
redirect '/portfolio'
else
last_modified File.mtime("_site/index.html")
File.read("_site/index.html")
end
else
raise Sinatra::NotFound
end
end
end
end
## POST requests ##
post '/charge' do
puts params
customer = Stripe::Customer.create(
email: params[:stripeEmail],
card: params[:stripeToken],
metadata: {
phone: params[:phone],
company: params[:company],
name: params[:name]
}
)
customer.subscriptions.create plan: "maintenance-2"
redirect '/ruby-on-rails/maintenance/thank-you'
end
error Stripe::CardError do
env['sinatra.error'].message
end
post '/contact-form/?' do
recaptcha_raw_response = RestClient.post 'https://www.google.com/recaptcha/api/siteverify', secret: ENV['RECAPTCHA_SECRET_KEY'], response: params['g-recaptcha-response'], remoteip: request.ip
recaptcha = JSON.parse recaptcha_raw_response
# disable contact for now
if false # recaptcha["success"] == true
htmlBody = %Q{
<div style="font-family:Helvetica;">
<h2>Contact Information</h2>
<table style="font-size:11pt;">
<tbody>
<tr>
<td width="25%">Name:</td>
<td>#{params[:name]}</td>
</tr>
<tr>
<td>Email:</td>
<td>#{params[:email]}</td>
</tr>
<tr>
<td>Phone:</td>
<td>#{params[:phone]}</td>
</tr>
</tbody>
</table>
<h2>Message</h2>
<p style="font-size:11pt;">#{params[:message]}</p>
</div>
}
textBody = %Q{
Contact Information
====================
Name: #{params[:name]}
Email: #{params[:email]}
Phone: #{params[:phone]}
Message
====================
#{params[:message]}
}
begin
res = Pony.mail(
to: "Velocity Labs <[email protected]>",
from: "[email protected]",
reply_to: "#{params[:name]} <#{params[:email]}>",
subject: "Project contact form from #{params[:name]}",
body: textBody,
html_body: htmlBody
)
response = res ? { status: :success } : { status: :failure }
rescue
response = { status: :failure }
end
else
byebug if settings.development?
response = { status: :failure }
# puts '*'*100
# puts "RECAPTCHA CAUGHT SOME SPAM!"
# puts params.to_s
# puts recaptcha.to_s
# puts '*'*100
end
content_type :json
status 200
body response.to_json
end