-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
96 lines (80 loc) · 2.19 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
$: << File.expand_path('../app', __FILE__)
require 'sinatra'
require 'sinatra/url_for'
require 'rack/mobile-detect'
require 'sinatra/respond_with'
require 'sinatra/cookies'
require 'securerandom'
require 'haml'
require 'logger'
require 'wikipedia'
require 'digest'
require 'modules'
require 'models'
require 'routes'
require 'services'
require 'jwt'
class Bemused < Sinatra::Application
helpers Sinatra::UrlForHelper
helpers Sinatra::Cookies
use Rack::MobileDetect
register Sinatra::RespondWith
enable :sessions
set :session_secret, ENV["BEMUSED_SESSION_SECRET"]
set :views, Proc.new { File.join(root, "app/views") }
set :local_authority, 'http://192.168.0.47' # for Sinatra::LocalApp
set :bind, '0.0.0.0'
set :user_cache, {}
helpers do
def user_tags
request.cookies['tags'].nil? ? [] : request.cookies['tags'].split('&').map{|id| Tag[id] }
end
def context?
!user_tags.empty?
end
def current_user
token = request.cookies["auth"]
begin
if token then
decoded = JWT.decode token, ENV['BEMUSED_JWT_SECRET'], 'HS256'
return settings.user_cache[decoded] if settings.user_cache[decoded]
user = User.find(id: decoded[0]['id'])
settings.user_cache[decoded] = user
end
rescue
end
user
end
def order_tracks(tracks)
tracks.sort_by{ |t| t.track_number.to_i }
end
def resumeable?(location)
patterns = [
/^\/$/,
/^\/resume$/,
/\/summary$/,
/\/stream\//,
/\/log\//,
/\/images\//,
/^\/livesearch$/,
/^\/search$/,
/\/favorite$/
]
unioned_regex = Regexp.union(patterns)
not location.match(unioned_regex)
end
end
configure do
info = development? || test? ? Wikipedia : ::Wikipedia
set :info, Info.new(info)
end
before do
#response.set_cookie(:bmc, value: SecureRandom.uuid, expires: Time.now + 3600 * 24 * 365 * 10) if request.cookies["bmc"].nil?
@tags = user_tags
Resume.log_location(current_user.id, request.path) if current_user && resumeable?(request.path)
end
not_found do
puts "#{request.request_method} #{request.path}"
haml :hunh
end
end