-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
216 lines (192 loc) · 6.5 KB
/
main.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
require 'sinatra/base'
require 'sinatra/assetpack'
require 'sinatra/support'
require 'compass'
require 'addressable/uri'
require 'json'
require 'net/https'
require 'elasticsearch'
require 'loofah'
class Aoi < Sinatra::Base
configure do
data = open('config.json') do |io|
JSON.load(io)
end
use Rack::Session::Cookie, :secret => data['session_secret']
set :client_id, data['client_id']
set :client_secret, data['client_secret']
set :project_name, data['project_name'] || ''
set :index, data['index'] || 'aoi'
set :members, data['members'] || []
set :title, settings.project_name + '::Aoi'
end
register Sinatra::CompassSupport
compass = Compass.configuration
compass.project_path = root
compass.images_dir = "public/images"
compass.http_generated_images_path = "/images"
register Sinatra::AssetPack
assets do
serve '/assets/js', :from => 'assets/js'
serve '/assets/css', :from => 'assets/scss'
js :application, [
'/assets/js/jquery.js',
'/assets/js/marked.js',
'/assets/js/app.js'
]
css :application, [
'/assets/css/github-markdown.css',
'/assets/css/pure.css',
'/assets/css/app.css'
]
js_compression :jsmin
css_compression :sass
end
def es
Elasticsearch::Client.new log: false
end
helpers do
def project_name
settings.project_name
end
end
helpers do
def strip_script(html)
doc = Loofah.fragment(html).scrub!(:prune)
doc.to_s
end
end
helpers do
include Rack::Utils
alias_method :h, :escape_html
end
before do
pass if request.path_info =~ /(?:^\/login|^\/assets|^\/callback)/
redirect to('/login') unless !session[:user].nil?
end
get '/' do
redirect to('/entry')
end
get '/write' do
@title = 'Write - ' + settings.title
erb :write
end
post '/write' do
body = { title: params['title'], content: params['content'], user: session[:user], time: Time.now.to_i }
response = es.index index: settings.index, type: 'entry', body: body
es.indices.refresh index: settings.index
id = response['_id']
redirect to ("/entry/#{id}")
end
get '/search' do
query = params['q']
redirect to('/entry') if query.empty?
body = { query: { simple_query_string: { fields: ["content", "title"], query: query } } }
response = es.search index: settings.index, type: 'entry', body: body
total = response['hits']['total']
sources = response['hits']['hits']
@title = 'Search - ' + settings.title
erb :search, :locals => { :total => total, :sources => sources, :query => query }
end
get '/entry' do
size = 20
page = params['page'] || 1;
from = (page.to_i - 1) * 3
body = { size: size, from: from, sort: [ { time: 'desc' } ], query: { match_all:{} } }
@title = 'Entries - ' + settings.title
begin response = es.search index: settings.index, type: 'entry', body: body
rescue Elasticsearch::Transport::Transport::Errors::NotFound
erb :entries, :locals => { :total => 0, :sources => [], :size => size, :page => page }
end
total = !response.nil? ? response['hits']['total'] : 0
if from >= total.to_i
erb :entries, :locals => { :total => 0, :sources => [], :size => size, :page => page }
end
sources = !response.nil? ? response['hits']['hits'] : []
erb :entries, :locals => { :total => total, :sources => sources, :size => size, :page => page }
end
get '/entry/:id' do
id = params['id']
entry = es.get index: settings.index, type: 'entry', id: id
@title = entry['_source']['title'] + ' - ' + settings.title
erb :show, :locals => { :entry => entry }
end
get '/entry/:id/?edit' do
id = params['id']
entry = es.get index: settings.index, type: 'entry', id: id
redirect to('/') unless entry['_source']['user']['id'] == session[:user][:id]
@title = 'Edit - ' + settings.title
erb :edit, :locals => { :entry => entry }
end
post '/entry/:id/?edit' do
id = params['id']
entry = es.get index: settings.index, type: 'entry', id: id
redirect to('/') unless entry['_source']['user']['id'] == session[:user][:id]
body = { doc: { title: params['title'], content: params['content'],
user: session[:user], time: entry['_source']['time'] || Time.now.to_i } }
response = es.update index: settings.index, type: 'entry', id: id, body: body
redirect to ("/entry/#{id}")
end
get '/entry/:id/?delete' do
id = params['id']
entry = es.get index: settings.index, type: 'entry', id: id
redirect to('/') unless entry['_source']['user']['id'] == session[:user][:id]
es.delete index: settings.index, type: 'entry', id: id
redirect to('/');
end
get '/login' do
@title = 'Login - ' + settings.title
erb :login, :layout => false
end
post '/login' do
uri = Addressable::URI.parse('https://github.com/login/oauth/authorize')
uri.query_values = {
:client_id => settings.client_id,
:redirect_url => "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}/callback"
}
redirect uri
end
get '/callback' do
code = params['code']
redirect to('/login') if code.to_s.empty?
auth_uri = Addressable::URI.parse('https://github.com/login/oauth/access_token')
auth_uri.query_values = {
:client_id => settings.client_id,
:client_secret => settings.client_secret,
:code => code
}
http = Net::HTTP.new('github.com', 443)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(auth_uri.normalize.request_uri)
request['Accept'] = 'application/json'
response = http.request(request)
access_token = JSON.parse(response.body)["access_token"]
user_uri = Addressable::URI.parse('https://api.github.com/user')
user_uri.query_values = {
:client_id => settings.client_id,
:client_secret => settings.client_secret,
:access_token => access_token
}
http = Net::HTTP.new('api.github.com', 443)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
response = http.get(user_uri.normalize.request_uri)
user_info = JSON.parse(response.body)
members = settings.members.grep(user_info['login'])
redirect to('/login') unless members.length > 0
session[:user] = {
:id => user_info['id'],
:login => user_info['login'],
:avatar_url => user_info['avatar_url']
}
redirect to('/')
end
get '/logout' do
session.clear
redirect '/login'
end
not_found do
"404 Not Found..."
end
end