This repository has been archived by the owner on May 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 119
/
app.rb
269 lines (231 loc) · 6.32 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
require 'rubygems'
require 'sinatra'
require 'haml'
require 'sass'
require 'coderay'
require 'rack/codehighlighter'
require 'json'
require 'time'
$LOAD_PATH << File.dirname(__FILE__) + '/lib'
require 'article.rb'
require 'term.rb'
# Airbrake
configure :production do
require 'airbrake'
Airbrake.configure do |config|
config.api_key = `ENV['AIRBRAKE_API_KEY']`
end
use Airbrake::Rack
end
# NewRelic
configure :production do
require 'newrelic_rpm'
end
# require 'rack/coderay'
# use Rack::Coderay, "//pre[@lang]>code"
use Rack::Codehighlighter, :coderay, :markdown => true, :element => "pre>code", :pattern => /\A:::(\w+)\s*(\n|
)/i, :logging => false
configure :production do
ENV['APP_ROOT'] ||= File.dirname(__FILE__)
end
set :app_file, __FILE__
set :static_cache_control, [:public, :max_age => 3600*24]
# In-Mem Cache
$IO_CACHE ||= {}
configure :production do
if $IO_CACHE.empty?
Dir.glob(["#{settings.root}/docs/*.txt", "#{settings.root}/docs/*/*.txt"]) { |path|
$IO_CACHE[path] = File.read(path)
}
end
end
$DEFAULT_LANGUAGE = 'en'
$DEFAULT_VERSION = 'v1.0'
$DEPRECATED_VERSIONS = ['v0.10']
$ALL_VERSIONS = ['v1.0', 'v0.14', 'v0.12']
$TD_AGENT_VERSIONS = {'v1.0' => 'td-agent3', 'v0.12' => 'td-agent2'}
#
# For table-of-content
#
require 'toc'
def build_tocs
toc_vers = Dir.glob("#{settings.root}/lib/toc.#{$DEFAULT_LANGUAGE}.*.rb").map { |toc|
File.basename(toc, ".rb")["toc.#{$DEFAULT_LANGUAGE}.".size..-1]
}
tocs = {}
toc_vers.each { |ver|
tocs[ver] = TOC.new(settings.root, $DEFAULT_LANGUAGE, ver)
}
tocs
end
$TOCS = build_tocs
#
# Last update list for each article
#
$LAST_UPDATED = JSON.parse(File.read("#{settings.root}/config/last_updated.json"))
#
# NOT FOUND
#
not_found do
erb :not_found
end
set :root, File.dirname(__FILE__)
# sinatra-asset-pipeline
#unless ENV['RACK_ENV'] == 'test'
require 'sinatra/asset_pipeline'
set :assets_precompile, %w(application.js application.css *.png *.jpg *.svg *.eot *.ttf *.woff *.woff2)
set :assets_paths, %w(/app/js/ /app/css/)
set :assets_css_compressor, :yui
set :assets_js_compressor, :yui
register Sinatra::AssetPipeline
#end
#
# PATHS
#
get '/' do
redirect '/articles/quickstart', 301
end
get '/robots.txt' do
content_type 'text/plain'
"User-agent: *\nSitemap: /sitemap.xml\n"
end
get '/sitemap.xml' do
@article_names = []
$ALL_VERSIONS.each { |version|
$TOCS[version].sections.each { |_, _, categories|
categories.each { |_, _, articles|
articles.each { |name, _, _|
@article_names << [version, name]
}
}
}
}
content_type 'text/xml'
erb :sitemap, :layout => false
end
get '/categories/:category' do
@category_name = params[:category]
redirect "/#{$DEFAULT_VERSION}/categories/#{@category_name}", 301
end
get %r{/(v\d+\.\d+)/categories/(\S+)} do |version, category|
@version_num = @article_name = @category_name = @query_string = nil
@version_num = version
@version_num = 'v1.0' if version == 'v0.14'
@category_name = category
cache_long
render_category category, version
end
get '/articles/:article' do
@article_name = params[:article]
redirect "/#{$DEFAULT_VERSION}/articles/#{@article_name}", 301
end
get %r{/(v\d+\.\d+)/articles/(\S+)} do |version, article|
case article
when 'users'
redirect 'https://www.fluentd.org/testimonials'
when 'architecture'
redirect 'https://www.fluentd.org/architecture'
else
@version_num = @article_name = @category_name = @query_string = nil
@version_num = version
@version_num = 'v1.0' if version == 'v0.14'
@article_name = article
puts "@[#{ENV['RACK_ENV']}.articles] #{{ name: article }.to_json}"
cache_long
render_article article, version
end
end
helpers do
def link_to(page)
"/#{@article_version}" + page
end
def render_category(category, ver)
@article_version = ver
@articles = []
@desc = ''
sections(ver).each { |_, _, categories|
categories.each { |name, title, articles|
if name == category
@title = title
@articles = articles
@desc = title
break
end
}
}
if @articles.length == 1
article_name = @articles.first.first
redirect "/#{ver}/articles/#{article_name}", 301
elsif [email protected]?
@articles
erb :category
else
status 404
end
rescue Errno::ENOENT
status 404
end
def render_article(article, ver)
@default_url = "/#{$DEFAULT_VERSION}/articles/#{article}"
@filepath = article_file(article, ver)
@has_default_version = File.exist?(article_file(article, $DEFAULT_VERSION))
begin
unless $IO_CACHE.has_key? @filepath
$IO_CACHE[@filepath] = File.read(@filepath)
end
rescue Errno::ENOENT
if ver != $DEFAULT_VERSION && @has_default_version
return redirect(@default_url, 301)
else
return status(404)
end
end
doc_path = File.dirname(@filepath)
@article = Article.load(article, $IO_CACHE[@filepath], doc_path, ver)
@title = @article.title
@desc = @article.desc
@content = @article.content
@intro = @article.intro
@toc = @article.toc
@body = @article.body
@available_versions = $ALL_VERSIONS
@current_version = $DEFAULT_VERSION
@article_version = ver
@deprecated_article_version = $DEPRECATED_VERSIONS.include?(@article_version)
@last_updated = ($LAST_UPDATED[ver] || {})[article] || Time.now.to_s
erb :article
end
def article_file(article, ver)
if article.include?('/')
article
else
"#{settings.root}/docs/#{ver}/#{article}.txt"
end
end
def article_file_exists?(article, ver)
File.exist?(article_file(article, ver))
end
def cache_long
response['Cache-Control'] = "public, max-age=#{60 * 60 * 6}" unless self.class.development?
end
def slugify(title)
title.downcase.gsub(/[^a-z0-9 -_]/, '').gsub(/ /, '-')
end
def find_category(article, ver)
return nil if article.nil?
sections(ver).each { |_, _, categories|
categories.each { |category_name, _, articles|
articles.each { |article_name, _, _|
return category_name if article_name == article
}
}
}
nil
end
def sections(ver)
$TOCS[ver].sections
end
def next_section(current_slug, root=sections($DEFAULT_VERSION))
nil
end
alias_method :h, :escape_html
end