Skip to content

Commit

Permalink
Add relative_root config item and convert links to path only
Browse files Browse the repository at this point in the history
  • Loading branch information
sodabrew committed Jan 23, 2015
1 parent 7cbdad7 commit c58d4ef
Show file tree
Hide file tree
Showing 16 changed files with 116 additions and 80 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ config:
config.json
``` json
{"env":"development","host":"127.0.0.1","port":"3333","addr":"0.0.0.0","tmpdir":"/tmp",
"relative_root":"/therq",
"basic_auth":{
"realm":"Your RQ",
"users":{
Expand All @@ -137,6 +138,7 @@ Key | Description
**addr** | Addr for the web UI to listen on (0.0.0.0 for all interfaces)
**tmpdir** | Directory for temp files
allow_new_queue | Boolean, enable the new queue web UI, default `false`
relative_root | Path prefix for the web UI, default `/`
basic_auth | Hash for HTTP Basic authentication, has two required elements
basic_auth: **realm** | Realm for HTTP Basic
basic_auth: **users** | Hash of username:password pairs
Expand Down
89 changes: 60 additions & 29 deletions code/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,35 @@
module RQ
class Main < Sinatra::Base

# This is almost identical to Rack::URLMap but without checking that
# env['HTTP_HOST'] == env['SERVER_NAME'], and without support for
# multiple mapped paths.
class RelativeRoot
def initialize(app, relative_root)
@app = app
@relative_root = relative_root.chomp('/')
@match = Regexp.new("^#{Regexp.quote(@relative_root).gsub('/', '/+')}(.*)", nil, 'n')
end

def call(env)
script_name = env['SCRIPT_NAME']
path = env['PATH_INFO']

m = @match.match(path.to_s)
if m && (rest = m[1]) && (!rest || rest.empty? || rest[0] == ?/)
env['SCRIPT_NAME'] = (script_name + @relative_root)
env['PATH_INFO'] = rest
@app.call(env)
else
[404, {"Content-Type" => "text/plain", "X-Cascade" => "pass"}, ["Not Found: #{path}"]]
end

ensure
env['SCRIPT_NAME'] = script_name
env['PATH_INFO'] = path
end
end

enable :sessions
set :session_secret, 'super secret' # we are forking, so we must set

Expand Down Expand Up @@ -42,25 +71,27 @@ class Main < Sinatra::Base
def initialize(app=nil, config={})
super(app)
@allow_new_queue = config.fetch('allow_new_queue', false)
end

# If basic auth is enabled, wrap ourselves in Rack middleware
def self.new(app=nil, config={})
basic_auth = config['basic_auth']
if basic_auth
app = Rack::Auth::Basic.new(super(app, config)) do |username, password|
basic_auth['users'][username] == password
@relative_root = config.fetch('relative_root', '/').chomp('/') + '/'
end

def self.to_app(config)
relative_root = config.fetch('relative_root', '/').chomp('/') + '/'
raise ArgumentError, 'relative_root must start with /' unless relative_root.start_with?('/')
Rack::Builder.app do
use RQ::Main::RelativeRoot, relative_root
basic_auth = config['basic_auth']
if basic_auth
use Rack::Auth::Basic, basic_auth['realm'] do |username, password|
basic_auth['users'][username] == password
end
end
app.realm = basic_auth['realm']
app
else
super(app, config)
run RQ::Main.new(nil, config)
end
end

helpers do
def url
"http://#{request.host}:#{request.port}/"
def root
@relative_root
end

def allow_new_queue?
Expand Down Expand Up @@ -124,7 +155,7 @@ def flash(type, msg)
# This creates and starts a queue
result = queuemgr.create_queue(params['queue'])
flash :notice, "We got <code>#{params.inspect}</code> from form, and <code>#{result}</code> from QueueMgr"
redirect "/q/#{params['queue']['name']}"
redirect "#{root}q/#{params['queue']['name']}"
end

post '/new_queue_link' do
Expand All @@ -142,14 +173,14 @@ def flash(type, msg)
result = queuemgr.create_queue_link(params['queue']['json_path'])
#TODO - do the right thing with the result code
flash :notice, "We got <code>#{params.inspect}</code> from form, and <code>#{result}</code> from QueueMgr"
redirect "/q/#{js_data['name']}"
redirect "#{root}q/#{js_data['name']}"
end

post '/delete_queue' do
# This creates and starts a queue
result = queuemgr.delete_queue(params['queue_name'])
flash :notice, "We got <code>#{params.inspect}</code> from form, and <code>#{result}</code> from QueueMgr"
redirect "/"
redirect root
end

get '/q.txt' do
Expand Down Expand Up @@ -405,7 +436,7 @@ def flash(type, msg)
end

flash :notice, "Message cloned successfully"
redirect "/q/#{params[:name]}"
redirect "#{root}q/#{params[:name]}"
end

post '/q/:name/:msg_id/run_now' do
Expand All @@ -425,7 +456,7 @@ def flash(type, msg)
end

flash :notice, "Message in run successfully"
redirect "/q/#{params[:name]}/#{params[:msg_id]}"
redirect "#{root}q/#{params[:name]}/#{params[:msg_id]}"
end

# TODO: change URL for this call
Expand Down Expand Up @@ -481,7 +512,7 @@ def flash(type, msg)
else
if result[0] == "ok"
flash :notice, "Attached message successfully"
redirect "/q/#{params[:name]}/#{params[:msg_id]}"
redirect "#{root}q/#{params[:name]}/#{params[:msg_id]}"
else
"Commit #{params[:name]}/#{params[:msg_id]} got #{result}"
end
Expand All @@ -506,7 +537,7 @@ def flash(type, msg)
else
if result[0] == "ok"
flash :notice, "Attachment deleted successfully"
redirect "/q/#{params[:name]}/#{params[:msg_id]}"
redirect "#{root}q/#{params[:name]}/#{params[:msg_id]}"
else
"Delete of attach #{params[:attachment_name]} on #{params[:name]}/#{params[:msg_id]} got #{result}"
end
Expand Down Expand Up @@ -591,7 +622,7 @@ def flash(type, msg)
erb :tailview, :layout => false,
:locals => {
:tail_path => path,
:state_path => "/q/#{params[:name]}/#{msg_id}/state.json",
:state_path => "#{root}q/#{params[:name]}/#{msg_id}/state.json",
:name => params['attach_name'],
}
end
Expand All @@ -612,8 +643,8 @@ def flash(type, msg)

erb :tailview, :layout => false,
:locals => {
:tail_path => "/q/#{params[:name]}/#{msg_id}/log/#{params[:log_name]}",
:state_path => "/q/#{params[:name]}/#{msg_id}/state.json",
:tail_path => "#{root}q/#{params[:name]}/#{msg_id}/log/#{params[:log_name]}",
:state_path => "#{root}q/#{params[:name]}/#{msg_id}/state.json",
}
end

Expand All @@ -635,10 +666,10 @@ def flash(type, msg)
else
if result[0] == "ok"
flash :notice, "Message deleted successfully"
redirect "/q/#{params[:name]}"
redirect "#{root}q/#{params[:name]}"
else
flash :error, "Delete got #{result.inspect}"
redirect "/q/#{params[:name]}/#{params[:msg_id]}"
redirect "#{root}q/#{params[:name]}/#{params[:msg_id]}"
end
end

Expand All @@ -649,10 +680,10 @@ def flash(type, msg)
else
if result[0] == "ok"
flash :notice, "Message destroyed successfully"
redirect "/q/#{params[:name]}"
redirect "#{root}q/#{params[:name]}"
else
flash :error, "destroy got #{result.inspect}"
redirect "/q/#{params[:name]}/#{params[:msg_id]}"
redirect "#{root}q/#{params[:name]}/#{params[:msg_id]}"
end
end

Expand All @@ -666,7 +697,7 @@ def flash(type, msg)
else
flash :error, "Commit got #{result.inspect}"
end
redirect "/q/#{params[:name]}/#{params[:msg_id]}"
redirect "#{root}q/#{params[:name]}/#{params[:msg_id]}"
end
else
throw :halt, [400, "400 - Invalid method param"]
Expand Down
5 changes: 3 additions & 2 deletions code/queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ def run_queue_script!(msg)
ENV["RQ_VER"] = RQ_VER
ENV["RQ_SCRIPT"] = @config.script
ENV["RQ_REALSCRIPT"] = script_path
ENV["RQ_HOST"] = "http://#{@host}:#{@port}/"
ENV["RQ_HOST"] = "http://#{@host}:#{@port}#{@root}"
ENV["RQ_DEST"] = gen_full_dest(msg)['dest']
ENV["RQ_DEST_QUEUE"] = gen_full_dest(msg)['queue']
ENV["RQ_MSG_ID"] = msg_id
Expand Down Expand Up @@ -429,6 +429,7 @@ def load_rq_config
js_data = JSON.parse(data)
@host = js_data['host']
@port = js_data['port']
@root = js_data.fetch('relative_root', '/').chomp('/') + '/'
true
rescue
false
Expand Down Expand Up @@ -861,7 +862,7 @@ def get_message(params, state,
end

def gen_full_msg_id(msg)
"http://#{@host}:#{@port}/q/#{@name}/#{msg['msg_id']}"
"http://#{@host}:#{@port}#{@root}q/#{@name}/#{msg['msg_id']}"
end

def gen_full_dest(msg)
Expand Down
2 changes: 1 addition & 1 deletion code/queuemgr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def start_webserver

$0 = $log.progname = '[rq-web]'
Rack::Handler::UnixRack.run(
RQ::Main.new(nil, @config), {
RQ::Main.to_app(@config), {
:Port => @config['port'],
:Host => @config['addr'],
:Hostname => @config['host'],
Expand Down
4 changes: 2 additions & 2 deletions code/views/_main_queue_form.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<form class="inline inrow" method="post" action="<%= "#{url}q/#{name}/adminoper" %>">
<form class="inline inrow" method="post" action="<%= "#{root}q/#{name}/adminoper" %>">
<input type="hidden" name="back" value="<%= request.path %>" />
<%- if status == 'PAUSE' -%>
<button id="resume-queue" name="resume" value="resume" <%= 'disabled="disabled"' if status == 'DOWN' %>>Resume</button>
Expand All @@ -11,7 +11,7 @@
<button id="down-queue" name="down" value="down">Down</button>
<%- end -%>
</form>
<form class="inline inrow" method="post" action="<%= "#{url}q/#{name}/restart" %>">
<form class="inline inrow" method="post" action="<%= "#{root}q/#{name}/restart" %>">
<input type="hidden" name="back" value="<%= request.path %>" />
<button id="restart-queue" name="restart" value="restart">Restart</button>
</form>
6 changes: 3 additions & 3 deletions code/views/_main_queue_row.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<tr class="<%= 'odd-row' if odd %>">
<td class="left-aligned"><a href="<%= "#{url}q/#{name}" %>"><%= name %></a></td>
<td class="left-aligned"><a href="<%= "#{root}q/#{name}" %>"><%= name %></a></td>
<% begin
qc = get_queueclient(name)
raise unless qc.running?
Expand All @@ -9,7 +9,7 @@
<td>
<pre><%= msgs_labels.zip(qc.num_messages.values_at(*msgs_labels)).map{|ab| "#{ab[0]}:#{ab[1].to_s.ljust(4)} "}.join %></pre>
<% qc.config[1]['schedule'].each do |sched| %>
<form class="inline inrow" method="post" action="<%= "#{url}q/#{name}/new_message" %>">
<form class="inline inrow" method="post" action="<%= "#{root}q/#{name}/new_message" %>">
<input type="hidden" name="back" value="<%= request.path %>" />
<input type="hidden" name="x_format" value="html" />
<input type="hidden" name="mesg[src]" value="cron-now " />
Expand Down Expand Up @@ -46,6 +46,6 @@
<td><span class="red">DOWN <%= $! %></span></td>
<% end -%>
<td>
<%= erb :_main_queue_form, :locals => {:status => status, :url => url, :name => name } %>
<%= erb :_main_queue_form, :locals => {:status => status, :name => name } %>
</td>
</tr>
2 changes: 1 addition & 1 deletion code/views/layout.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<title>RQ</title>
<% end %>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="/css/rq.css" type="text/css" media="screen, projection" />
<link rel="stylesheet" href="<%= root %>css/rq.css" type="text/css" media="screen, projection" />
</head>
<% our_env = queuemgr.environment rescue '' %>
<body class="<%= our_env %>">
Expand Down
4 changes: 2 additions & 2 deletions code/views/main.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<div id="header">
<% if allow_new_queue? -%>
<p id="new-queue"><a href="/new_queue">New Queue</a></p>
<p id="new-queue"><a href="<%= root %>new_queue">New Queue</a></p>
<% end -%>
<h1><a href="<%= url %>">RQ</a></h1>
<h1><a href="<%= root %>">RQ</a></h1>
</div>

<h2>Queue List</h2>
Expand Down
17 changes: 10 additions & 7 deletions code/views/message.erb
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<div id="header">
<h1><a href="<%= url %>">RQ</a> : <a href="<%= "#{url}q/#{q_name}" %>"><%= q_name %></a> : <%= msg_id %></h1>
<h1><a href="<%= root %>">RQ</a> : <a href="<%= "#{root}q/#{q_name}" %>"><%= q_name %></a> : <%= msg_id %></h1>
</div>

<div id="sub-header">
<div id="message-actions">
<form class="inline" id="clone-form" method="post" action="<%= "#{url}q/#{q_name}/#{msg_id}/clone" %>">
<form class="inline" id="clone-form" method="post" action="<%= "#{root}q/#{q_name}/#{msg_id}/clone" %>">
<button id="clone-button">Clone message</button>
</form>
<form class="inline" id="run-form" method="post" action="<%= "#{url}q/#{q_name}/#{msg_id}/run_now" %>">
<form class="inline" id="run-form" method="post" action="<%= "#{root}q/#{q_name}/#{msg_id}/run_now" %>">
<button id="run-button">Run message (risky)</button>
</form>
</div>
<p>message.json [ <a href="#" onclick="show_toggle()">full/summary</a> | <a href="<%= "#{url}q/#{q_name}/#{msg_id}.json" %>">raw</a> ]</p>
<p>message.json [ <a href="#" onclick="show_toggle()">full/summary</a> | <a href="<%= "#{root}q/#{q_name}/#{msg_id}.json" %>">raw</a> ]</p>
<table id="message-json-default" class="">
<% ['dest', 'status', 'state' ].each do |key| %>
<% next unless msg.has_key?(key) %>
Expand Down Expand Up @@ -63,7 +63,7 @@
<% msg['_attachments'].keys.sort.each do |key| %>
<li>
<span class="">
<a href="<%= "#{url}q/#{q_name}/#{msg_id}/attach/#{key}" %>"><%= key %></a>
<a href="<%= "#{root}q/#{q_name}/#{msg_id}/attach/#{key}" %>"><%= key %></a>
</span>
</li>
<% end %>
Expand All @@ -72,8 +72,11 @@
<p>No attachments.</p>
<% end %>

<h3>Log Tail Window [ <a href="<%= "#{url}q/#{q_name}/#{msg_id}/log/stdio.log" %>">raw</a> ] [ <a href="<%= "#{url}q/#{q_name}/#{msg_id}/tailviewlog/stdio.log" %>">Full Tail</a> ] </h3>
<iframe src="<%= "#{url}q/#{q_name}/#{msg_id}/tailviewlog/stdio.log" %>"
<h3>Log Tail Window
[ <a href="<%= "#{root}q/#{q_name}/#{msg_id}/log/stdio.log" %>">Raw log</a> ]
[ <a href="<%= "#{root}q/#{q_name}/#{msg_id}/tailviewlog/stdio.log" %>">Full tail</a> ]
</h3>
<iframe id="log" src="<%= "#{root}q/#{q_name}/#{msg_id}/tailviewlog/stdio.log" %>"
width="100%" height="400" frameborder="0">
</iframe>
<script>
Expand Down
6 changes: 3 additions & 3 deletions code/views/new_message.erb
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<div id="header">
<h1><a href="<%= url %>">RQ</a> : <a href="<%= "#{url}q/#{q_name}" %>"><%= q_name %></a></h1>
<h1><a href="<%= root %>">RQ</a> : <a href="<%= "#{root}q/#{q_name}" %>"><%= q_name %></a></h1>
</div>

<h3>New Message</h3>
<form id="new_queue_form" method="post" action="<%= "/q/#{q_name}/new_message" %>">
<form id="new_queue_form" method="post" action="<%= "#{root}q/#{q_name}/new_message" %>">
<fieldset>
<legend>Message Options</legend>

<input type="hidden" name="x_format" value="html" />

<div class="field" id="mesg_dest_field" <%= overrides.show_field('mesg_dest') ? '' : "style=\"display: none;\"" %>>
<label class="field_label" for="mesg_dest">Destination Queue</label>
<input type="text" size="100" name="mesg[dest]" id="mesg_dest" value="<%= "#{url}q/#{q_name}" %>" />
<input type="text" size="100" name="mesg[dest]" id="mesg_dest" value="<%= "#{root}q/#{q_name}" %>" />
</div>

<div class="field" id="mesg_src_field" <%= overrides.show_field('mesg_src') ? '' : "style=\"display: none;\"" %>>
Expand Down
2 changes: 1 addition & 1 deletion code/views/new_message_post.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<h3>New Message Result</h3>

<p>Queue: <a href="/q/<%= q_name %>"><%= q_name %></a></p>
<p>Queue: <a href="<%= root %>q/<%= q_name %>"><%= q_name %></a></p>

<p>
<% if result[0] == 'ok' %>
Expand Down
Loading

0 comments on commit c58d4ef

Please sign in to comment.