Address Brother QL printer by URL path instead of session cookie
deploy / deploy (push) Successful in 9s
deploy / deploy (push) Successful in 9s
Drops /brother/select and the session-based "active printer" state: the printer key now lives in the path (/brother/<printer>/*), so the proxy is stateless and any path under it -- including brother_ql_web's API, not just its UI -- is reachable without a prior "activation" step.
This commit is contained in:
1 parent
776f7fdd5a
commit
5c06f725f0
4 files changed
+13
-34
No files matched your search
+2
-2
@@ -10,8 +10,8 @@ Unified UI for printing labels/stickers:
|
|||||||
* `/` -- upload a PDF and print it (ZPL)
|
* `/` -- upload a PDF and print it (ZPL)
|
||||||
* `/upload` -- upload form
|
* `/upload` -- upload form
|
||||||
* `/test-print` -- print the bundled test label
|
* `/test-print` -- print the bundled test label
|
||||||
* `/brother` -- pick which Brother QL printer to proxy to
|
* `/brother` -- list of Brother QL printers
|
||||||
* `/brother/select` -- switch the active Brother QL printer
|
* `/brother/<printer>/*` -- proxied to that printer's brother_ql_web instance (UI and API alike)
|
||||||
|
|
||||||
== Running
|
== Running
|
||||||
|
|
||||||
|
|||||||
@@ -89,9 +89,10 @@ helpers do
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Transparently reverse-proxies the current request to one of the
|
# Transparently reverse-proxies the current request to one of the
|
||||||
# brother_ql_web instances, so all three printers are reachable through
|
# brother_ql_web instances, so all three printers (web UI and API alike)
|
||||||
# this single sinatra port instead of their own dedicated ports.
|
# are reachable through this single sinatra port instead of their own
|
||||||
def proxy_to_brother(port, path)
|
# dedicated ports.
|
||||||
|
def proxy_to_brother(port, prefix, path)
|
||||||
uri = URI("http://127.0.0.1:#{port}#{path}")
|
uri = URI("http://127.0.0.1:#{port}#{path}")
|
||||||
uri.query = request.query_string unless request.query_string.empty?
|
uri.query = request.query_string unless request.query_string.empty?
|
||||||
|
|
||||||
@@ -103,7 +104,7 @@ helpers do
|
|||||||
proxied[header] = value
|
proxied[header] = value
|
||||||
end
|
end
|
||||||
proxied['Content-Type'] = request.content_type if request.content_type
|
proxied['Content-Type'] = request.content_type if request.content_type
|
||||||
proxied['X-Forwarded-Prefix'] = '/brother'
|
proxied['X-Forwarded-Prefix'] = prefix
|
||||||
proxied['X-Forwarded-Host'] = request.host
|
proxied['X-Forwarded-Host'] = request.host
|
||||||
proxied.body = request.body.read unless request.request_method == 'GET'
|
proxied.body = request.body.read unless request.request_method == 'GET'
|
||||||
|
|
||||||
@@ -188,23 +189,14 @@ end
|
|||||||
|
|
||||||
get '/brother' do
|
get '/brother' do
|
||||||
@printers = BROTHER_PRINTERS
|
@printers = BROTHER_PRINTERS
|
||||||
@selected = session[:brother_printer]
|
|
||||||
erb :brother_picker
|
erb :brother_picker
|
||||||
end
|
end
|
||||||
|
|
||||||
post '/brother/select' do
|
|
||||||
key = params[:printer]
|
|
||||||
halt 400, 'unknown printer' unless BROTHER_PRINTERS.key?(key)
|
|
||||||
|
|
||||||
session[:brother_printer] = key
|
|
||||||
redirect '/brother/'
|
|
||||||
end
|
|
||||||
|
|
||||||
%w[get post put delete].each do |verb|
|
%w[get post put delete].each do |verb|
|
||||||
send(verb, '/brother/*') do
|
send(verb, '/brother/:printer/*') do
|
||||||
key = session[:brother_printer]
|
printer = BROTHER_PRINTERS[params[:printer]]
|
||||||
redirect '/brother' unless key
|
halt 404, 'unknown printer' unless printer
|
||||||
|
|
||||||
proxy_to_brother(BROTHER_PRINTERS[key][:port], '/' + params[:splat].first)
|
proxy_to_brother(printer[:port], "/brother/#{params[:printer]}", '/' + params[:splat].first)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
+1
-4
@@ -16,10 +16,7 @@
|
|||||||
<span class="navbar-text me-2">Brother:</span>
|
<span class="navbar-text me-2">Brother:</span>
|
||||||
<div class="d-flex gap-1">
|
<div class="d-flex gap-1">
|
||||||
<% BROTHER_PRINTERS.each do |key, info| %>
|
<% BROTHER_PRINTERS.each do |key, info| %>
|
||||||
<form action="/brother/select" method="POST" class="m-0">
|
<a href="/brother/<%= key %>/" class="btn btn-sm <%= request.path.start_with?("/brother/#{key}/") ? 'btn-primary' : 'btn-outline-secondary' %>"><%= info[:label].sub('Brother ', '') %></a>
|
||||||
<input type="hidden" name="printer" value="<%= key %>">
|
|
||||||
<button type="submit" class="btn btn-sm <%= session[:brother_printer] == key ? 'btn-primary' : 'btn-outline-secondary' %>"><%= info[:label].sub('Brother ', '') %></button>
|
|
||||||
</form>
|
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -3,16 +3,6 @@
|
|||||||
|
|
||||||
<div class="list-group" style="max-width: 400px;">
|
<div class="list-group" style="max-width: 400px;">
|
||||||
<% @printers.each do |key, info| %>
|
<% @printers.each do |key, info| %>
|
||||||
<form action="/brother/select" method="POST" class="mb-2">
|
<a href="/brother/<%= key %>/" class="btn btn-outline-primary w-100 text-start mb-2"><%= info[:label] %></a>
|
||||||
<input type="hidden" name="printer" value="<%= key %>">
|
|
||||||
<button type="submit" class="btn <%= @selected == key ? 'btn-primary' : 'btn-outline-primary' %> w-100 text-start">
|
|
||||||
<%= info[:label] %>
|
|
||||||
<% if @selected == key %> (active) <% end %>
|
|
||||||
</button>
|
|
||||||
</form>
|
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<% if @selected %>
|
|
||||||
<p class="mt-3"><a href="/brother/">Open <%= @printers[@selected][:label] %> »</a></p>
|
|
||||||
<% end %>
|
|
||||||
Reference in new issue
Block a user