-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy paththe_app.rb
109 lines (92 loc) · 2.49 KB
/
the_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
#
# This app handles search requests and also has a hook to allow for self-updating
#
require 'tire'
require 'pp'
require 'sinatra'
require 'haml'
require 'rack/rewrite'
require_relative './_config'
# fix so foreman gets logging
$stdout.sync = true
use Rack::Rewrite do
r301 %r{/start/?}, "#{$config.basepath}"
r301 %r{/randor-basics/?}, "#{$config.basepath}/wistia-basics-getting-started"
r301 %r{/randor-(\w+)/?}, "#{$config.basepath}/wistia-basics-$1"
r301 "#{$config.basepath}/construct-an-embed-code", "#{$config.basepath}/embed-api"
rewrite %r{#{$config.basepath}(.*)}, '$1'
end
class SuperStatic
def initialize(app)
@app = app
@root = File.join(File.dirname(__FILE__), '_site')
@file_server = Rack::File.new(@root)
end
def call(env)
path = env['PATH_INFO']
file_path = File.join(@root, path)
file_path_with_index = File.join(@root, path, 'index.html')
if FileTest.file?(file_path)
send_file(file_path)
elsif FileTest.file?(file_path_with_index)
send_file(file_path_with_index)
else
@app.call(env)
end
end
def send_file(path)
[ 200,
{
'Content-Length' => ::File.size(path).to_s,
'Content-Type' => Rack::Mime.mime_type(::File.extname(path))
},
[::File.read(path)]
]
end
end
use SuperStatic
get "/search/:q" do
q = params[:q]
q.gsub!("_", " ")
s = Tire.search 'posts' do
query do
string q
end
end
Tire.configure do
logger 'elasticsearch.log'
end
results_list = s.results.map do |result|
{
:title => result.title,
:url => result.url,
:description => result.description
}
end
result = { :results => results_list }.to_json
if params[:callback]
"#{params[:callback]}(#{result});"
else
result
end
end
# TODO: Properly re-initialize the server.
# github will hit this URL after a commit so we can auto-update
# the doc. omg this is cool.
post '/update' do
return 403 unless params[:update_key] == $config.update_key
spawn('rake', 'nuclear_update', chdir: File.dirname(__FILE__))
'We can rebuild him. We have the technology. We can make him better than he was. Better...stronger...faster.'
end
# 404 page
not_found do
send_file(File.join(File.dirname(__FILE__), '_site', '404.html'), {:status => 404})
end
# DevHQ page
get "/developers" do
send_file(File.join(File.dirname(__FILE__), '_site', 'developers.html'))
end
# Agency!
get "/agency" do
send_file(File.join(File.dirname(__FILE__), '_site', 'agency.html'))
end