-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
152 lines (114 loc) · 3.7 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
require "sinatra"
require "sinatra/param"
require "postmark"
require "rack/attack"
require "active_support"
require_relative 'app/modou/models/ticket'
class QiniuCDN
require "nokogiri"
def initialize(app)
require "qiniu"
Qiniu.establish_connection! access_key: ENV['QINIU_ACCESS_KEY'], secret_key: ENV['QINIU_SECRET_KEY']
@app = app
end
def replace_asset!(body_string, asset_path)
if asset_path[0] == '/'
cdn_path = asset_path[1..-1]
else
cdn_path = asset_path
end
cdn_url = "http://#{ENV['QINIU_BUCKET']}.qiniudn.com/#{cdn_path}"
body_string.gsub!(asset_path, Qiniu::Auth.authorize_download_url(cdn_url))
end
def call(env)
status_code, headers, body = @app.call(env)
if body.respond_to? :join
headers.delete('Content-Length')
body_string = body.join
doc = Nokogiri::HTML(body_string)
doc.css('link').select { |link| link['href'] =~ /assets/ }.map do |link|
replace_asset!(body_string, link['href'])
end
doc.css('a').select { |a| a['href'] =~ /assets/ }.map do |a|
replace_asset!(body_string, a['href'])
end
doc.css('script').select { |node| node['src'] =~ /assets/ }.map do |node|
replace_asset!(body_string, node['src'])
end
[status_code, headers, [body_string]]
else
[status_code, headers, body]
end
end
end
use QiniuCDN
use Rack::Attack
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
Rack::Attack.blacklist('block bad guys') do |req|
# Requests are blocked if the return value is truthy
require "yaml"
ips = YAML.load_file(File.expand_path('../data/blacklisted_ips.yml', __FILE__))
ips.include?(req.ip)
end
### Throttle Spammy Clients ###
# If any single client IP is making tons of requests, then they're
# probably malicious or a poorly-configured scraper. Either way, they
# don't deserve to hog all of the app server's CPU. Cut them off!
# Throttle all requests by IP
#
# Key: "rack::attack:#{Time.now.to_i/:period}:req/ip:#{req.ip}"
Rack::Attack.throttle('req/ip', :limit => 10, :period => 300) do |req|
req.ip
end
configure :production do
require "newrelic_rpm"
end
configure :development do
set :show_exceptions, false
set :raise_errors, true
end
helpers Sinatra::Param
helpers do
def default_params
{
:name => nil,
:phone => nil,
:email => nil,
:order_number => nil,
:reason => nil,
:comment => nil,
:error => nil
}
end
end
set :raise_sinatra_param_exceptions, true
error Sinatra::Param::InvalidParameterError do
p params.merge({ error: env['sinatra.error'].param })
erb :template, locals: params.merge({ error: env['sinatra.error'].param })
end
get '/' do
erb :template, locals: default_params.merge(params)
end
post '/support' do
p params
ticket = Modou::Ticket.create_from_params(params)
p ticket
puts ticket.to_human_readable_text
param :name, String, required: true
param :phone, String, required: true
param :email, String, format: /\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z/
param :order_number, String
param :comment, String, required: true
valid_reasons = %w{ refund-not-received refund-received replace repair other }.freeze
param "reason", String, in: valid_reasons, required: true, blank: false
api_key = ENV["POSTMARK_API_KEY"]
if api_key
client = Postmark::ApiClient.new(api_key)
client.deliver(from: "[email protected]",
to: "[email protected]",
headers: { 'Name' => ENV['DAIKE_CUSTOM_HEADER'], 'Value' => ticket.user_email },
subject: "support request from #{ticket.user_name}",
text_body: ticket.to_human_readable_text)
end
'thanks'
end