diff --git a/README.adoc b/README.adoc index d0c5d6c..38c0600 100644 --- a/README.adoc +++ b/README.adoc @@ -10,8 +10,8 @@ Unified UI for printing labels/stickers: * `/` -- upload a PDF and print it (ZPL) * `/upload` -- upload form * `/test-print` -- print the bundled test label -* `/brother` -- pick which Brother QL printer to proxy to -* `/brother/select` -- switch the active Brother QL printer +* `/brother` -- list of Brother QL printers +* `/brother//*` -- proxied to that printer's brother_ql_web instance (UI and API alike) == Running diff --git a/app.rb b/app.rb index eca6b62..a258971 100644 --- a/app.rb +++ b/app.rb @@ -89,9 +89,10 @@ helpers do end # Transparently reverse-proxies the current request to one of the - # brother_ql_web instances, so all three printers are reachable through - # this single sinatra port instead of their own dedicated ports. - def proxy_to_brother(port, path) + # brother_ql_web instances, so all three printers (web UI and API alike) + # are reachable through this single sinatra port instead of their own + # dedicated ports. + def proxy_to_brother(port, prefix, path) uri = URI("http://127.0.0.1:#{port}#{path}") uri.query = request.query_string unless request.query_string.empty? @@ -103,7 +104,7 @@ helpers do proxied[header] = value end 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.body = request.body.read unless request.request_method == 'GET' @@ -188,23 +189,14 @@ end get '/brother' do @printers = BROTHER_PRINTERS - @selected = session[:brother_printer] erb :brother_picker 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| - send(verb, '/brother/*') do - key = session[:brother_printer] - redirect '/brother' unless key + send(verb, '/brother/:printer/*') do + printer = BROTHER_PRINTERS[params[:printer]] + 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 diff --git a/views/_navbar.erb b/views/_navbar.erb index c55002d..65d2acb 100644 --- a/views/_navbar.erb +++ b/views/_navbar.erb @@ -16,10 +16,7 @@ Brother:
<% BROTHER_PRINTERS.each do |key, info| %> -
- - -
+ "><%= info[:label].sub('Brother ', '') %> <% end %>
diff --git a/views/brother_picker.erb b/views/brother_picker.erb index 928917e..c4cedcc 100644 --- a/views/brother_picker.erb +++ b/views/brother_picker.erb @@ -3,16 +3,6 @@
<% @printers.each do |key, info| %> -
- - -
+ <%= info[:label] %> <% end %>
- -<% if @selected %> -

Open <%= @printers[@selected][:label] %> »

-<% end %>