forked from railsfactory-subbareddy/wistia-doc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththe_app.rb
132 lines (112 loc) · 3.45 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#
# This app handles search requests and also has a hook to allow for self-updating
#
require 'pp'
require 'rack/rewrite'
require_relative './_config'
# fix so foreman gets logging
$stdout.sync = true
class TheApp < Sinatra::Base
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 %r{/randor?}, "#{$config.basepath}/wistia-basics"
r301 %r{/embed-screen?}, "#{$config.basepath}/customizing-your-video"
r301 %r{/transcripts}, "#{$config.basepath}/captions"
r301 %r{/customize-api}, "#{$config.basepath}/data-api#customizations"
r301 %r{/replaceable-video}, "#{$config.basepath}/replace-video"
r301 %r{/viewer-rec}, "#{$config.basepath}/viewer-requirements"
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({
'PATH' => '/usr/local/bin:/usr/bin:/bin:/opt/bin:/usr/x86_64-pc-linux-gnu/gcc-bin/4.5.4:/opt/elasticsearch-0.19.9/bin',
'LANG' => 'en_US.UTF-8',
'LC_ALL' => 'en_US.UTF-8'
}, '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_error_file(File.join(File.dirname(__FILE__), '_site', '404.html'))
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
get "/wistia-basics" do
send_file(File.join(File.dirname(__FILE__), '_site', 'wistia-basics.html'))
end
def send_error_file(path)
[404,
{
'Content-Length' => ::File.size(path).to_s,
'Content-Type' => Rack::Mime.mime_type(::File.extname(path))
},
[::File.read(path)]
]
end
end