forked from mislav/rfc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
143 lines (114 loc) · 3 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
# encoding: utf-8
require 'sinatra'
use Rack::Static, :urls => %w[/favicon.ico /apple-touch-icon], :root => 'public'
bootstrap_root = File.expand_path('bootstrap', settings.root)
use Rack::Static, urls: %w[/img], root: bootstrap_root
configure :production do
require 'rack/cache'
use Rack::Cache,
allow_reload: true,
verbose: settings.development?,
metastore: 'memcached://localhost:11211/meta',
entitystore: 'memcached://localhost:11211/body?compress=true'
end
require 'rack/deflater'
use Rack::Deflater
require_relative 'lib/sinatra_boilerplate'
set :sass do
options = {
style: settings.production? ? :compressed : :nested,
load_paths: ['.', File.join(bootstrap_root, 'lib')]
}
options[:cache_location] = File.join(ENV['TMPDIR'], 'sass-cache') if ENV['TMPDIR']
options
end
set :js_assets, %w[zepto.js app.coffee]
configure :development do
set :logging, false
ENV['DATABASE_URL'] ||= 'postgres://localhost/rfc'
end
require 'dm-core'
configure :development do
DataMapper::Logger.new($stderr, :info)
end
configure do
DataMapper.setup(:default, ENV['DATABASE_URL'])
end
require_relative 'models'
configure do
DataMapper.finalize
DataMapper::Model.raise_on_save_failure = true
RfcFetcher.download_dir = File.expand_path('../tmp/xml', __FILE__)
end
helpers do
def display_document_id doc
doc_id = String === doc ? doc : doc.id
if doc_id =~ /^ rfc (\d+) $/ix
"RFC %d" % $1.to_i
else
doc_id
end
end
def display_abstract text
text.sub(/\[STANDARDS[ -]{1,2}TRA?CK\]/, '') if text
end
def search_path options = {}
get_params = request.GET.merge('page' => options[:page])
url '/search?' + Rack::Utils.build_query(get_params), false
end
def rfc_path doc
doc_id = String === doc ? doc : doc.id
url doc_id, false
end
def home_path
url '/'
end
def page_title title = nil
if title
@page_title = title
else
@page_title
end
end
end
require_relative 'lib/auto_last_modified'
before do
page_title "Pretty RFC"
end
error 404 do
erb :not_found
end
error do
err = env['sinatra.error']
locals = {}
if err.respond_to? :message
locals[:error_type] = err.class.name
locals[:error_message] = err.message
end
erb :error, {}, locals
end
get "/" do
expires 5 * 60, :public
erb :index, auto_last_modified: true
end
get "/search" do
@query = params[:q].to_s.strip
redirect to('/RFC%04d' % $1.to_i) if @query =~ /^(?:rfc\s*)?(\d+)$/
expires 5 * 60, :public
@limit = 50
@results = RfcDocument.search @query, page: params[:page], limit: @limit
erb :search
end
get "/url/*" do
rfc = RfcDocument.resolve_url(params[:splat].first) { not_found }
target = url(rfc.id)
target << '#' << params[:hash] if params[:hash]
redirect target
end
get "/:doc_id" do
expires 5 * 60, :public
@rfc = RfcDocument.fetch(params[:doc_id]) { not_found }
redirect to(@rfc.id) unless request.path == "/#{@rfc.id}"
@rfc.make_pretty
erb :show, auto_last_modified: @rfc.last_modified
end