-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rb
More file actions
executable file
·114 lines (96 loc) · 3.33 KB
/
app.rb
File metadata and controls
executable file
·114 lines (96 loc) · 3.33 KB
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
require 'bundler/setup'
require 'sinatra/base'
require 'sinatra/config_file'
class EchoChamber < Sinatra::Base
register Sinatra::ConfigFile
set server: :thin, connections: []
set :protection, :except => :frame_options
config_file 'config.yml'
def authorized?
@auth ||= Rack::Auth::Basic::Request.new(request.env)
@auth.provided? && @auth.basic? && @auth.credentials && @auth.credentials == [settings.username, settings.password]
end
def protected!
unless authorized?
response['WWW-Authenticate'] = %(Basic realm="Control Interface")
throw(:halt, [401, "We need your login name & password to get to the control interface.\n"])
end
end
def read_list
@running_dir = File.dirname(__FILE__)
@running_dir = Dir.pwd if (@running_dir == '.')
@@list = {}
@@filehost = nil
File.readlines(@running_dir + '/data/list.txt').each do |line|
l = line.split('|')
@@list[l[0].chomp] = l[1].chomp if @@filehost
@@filehost = line.chomp unless @@filehost
end
@@list
end
configure do
@running_dir = File.dirname(__FILE__)
@running_dir = Dir.pwd if (@running_dir == '.')
@@list = {}
@@filehost = nil
File.readlines(@running_dir + '/data/list.txt').each do |line|
l = line.split('|')
@@list[l[0].chomp] = l[1].chomp if @@filehost
@@filehost = line unless @@filehost
end
@@current_title = @@list.keys.first
@@current_url = @@list[@@current_title]
@@title = "Default"
@@description = nil
puts "Hosting files from #{@@filehost}"
puts "Reading #{@@list.length} files."
puts "Defaulting to #{@@current_url}"
puts @@list.inspect
end
get '/' do
@current_title = @@current_title
@current_url = @@current_url
@title = @@title
@description = @@description
@filehost = @@filehost
erb :index
end
get '/ctrl' do
protected!
read_list
@current_title = @@current_title
@current_url = @@current_url
@title = @@title
@description = @@description
@strcount = settings.connections.length
@list = @@list
@filehost = @@filehost
erb :ctrl
end
get '/stream', :provides => 'text/event-stream' do
stream :keep_open do |out|
settings.connections << out
out.callback {settings.connections.delete(out)}
end
end
post '/post' do
if params[:title]
@@title = params[:title]
end
if params[:desc]
@@description = params[:desc]
end
if params[:play]
@@current_title = params[:play]
@@current_url = @@list[@@current_title]
settings.connections.each {|out| out << %Q^data: {"play": "#{@@current_url}", "title": "#{@@title}", "desc": "#{@@description}"}\n\n^}
end
if params[:ctrl]
settings.connections.each {|out| out << %Q^data: {"ctrl": "#{params[:ctrl]}"}\n\n^}
end
if params[:ctrl] && params[:jump]
settings.connections.each {|out| out << %Q^data: {"ctrl": "#{params[:ctrl]}", "jump": #{params[:jump]}}\n\n^}
end
204
end
end