-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.rb
113 lines (80 loc) · 2.38 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
require 'rubygems'
require 'bundler'
require 'securerandom'
require 'resque'
require 'sinatra'
require 'sinatra/reloader'
require 'sinatra/xsendfile'
require 'yaml'
require 'dig_rb'
require 'openssl'
$: << '.'
require_relative 'modules'
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
Bundler.require
$config = YAML::load(ERB.new(File.read(File.join(File.dirname(__FILE__), "config.yml"))).result(binding))
$genres = YAML::load(ERB.new(File.read(File.join(File.dirname(__FILE__), "genres.yml"))).result(binding))
$env = {:slave => false}
require_relative 'database'
Resque::Plugins::Status::Hash.expire_in = (60 * 60) * 24
module Nerve
class App < Sinatra::Application
configure do
disable :method_override
enable :sessions
set :views => File.dirname(__FILE__)
set :environment, ($config["environment"].to_sym rescue :development)
set :sessions,
:httponly => true,
:secure => false, #production?,
:expire_after => 31557600, # 1 year
:secret => ENV['SESSION_SECRET'] or SecureRandom.hex
set :protection, :except => [:json_csrf]
end
configure :production do
Sinatra::Xsendfile.replace_send_file!
set :xsf_header, 'X-Accel-Redirect'
end
use Nerve::Services::Login
use Nerve::Services::Metadata
use Nerve::Services::Moderation
use Nerve::Services::Upload
use Nerve::Services::Library
use Nerve::Services::Audio
use Rack::Deflater
get '/' do
redirect '/'
end
get '/dynamic/config' do
cache_control :public, max_age: 0
content_type "application/json"
service = Nerve::Services::Login.get_service
begin
raise "not logged in" if !session[:authenticated] or !session[:user_id]
p 'got service', service
@user = service.get_user session[:user_id]
rescue
session.clear
return {
'authorized' => false,
'redirect' => service.redirect(session, request) || '/login.html'
}.to_json
end
stats = {
:total_uploads => Nerve::Services::Upload.get_total_uploads,
:pending_moderation => Nerve::Services::Moderation.get_pending
}
key = session[:token] ||= SecureRandom.hex
{
'authorized' => true,
'stats' => stats,
'user' => @user.get_json,
'key' => key,
'station_name' => $config['station_name'],
'contact_email' => $config['contact_email'],
'genres' => $genres,
'banned_words' => $config['words']['banned']
}.to_json
end
end
end