Brother: proxy one brother_ql_web for all QL printers, drop printer switcher
deploy / deploy (push) Successful in 10s

This commit is contained in:
randy committed 2026-10-30 23:52:36 +01:00
1 parent 66300c5278
commit 563772197d
4 files changed
+10 -37

No files matched your search

+8 -18
View File
@@ -29,11 +29,7 @@ PRINTER_DEVICE_EPSON = '/dev/usb/lp_epson' # see /etc/udev/rules.d/99-epson.rule
LABEL_REPO = LabelRepository.new(File.join(File.dirname(__FILE__), 'data', 'labels'))
BROTHER_PRINTERS = {
'ql560' => { port: 8560, label: 'Brother QL-560' },
'ql700' => { port: 8700, label: 'Brother QL-700' },
'ql1100' => { port: 9100, label: 'Brother QL-1100' }
}.freeze
BROTHER_PORT = 8013 # one brother_ql_web for all QL printers (its PRINTERS env)
configure do
enable :static
@@ -223,12 +219,10 @@ helpers do
res.body
end
# Transparently reverse-proxies the current request to one of the
# 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}")
# Transparently reverse-proxies the current request to brother_ql_web, so
# its web UI and API are reachable through this single sinatra port.
def proxy_to_brother(prefix, path)
uri = URI("http://127.0.0.1:#{BROTHER_PORT}#{path}")
uri.query = request.query_string unless request.query_string.empty?
proxied = Net::HTTP.const_get(request.request_method.capitalize).new(uri)
@@ -485,15 +479,11 @@ post '/repository/:id/delete' do
end
get '/brother' do
@printers = BROTHER_PRINTERS
erb :brother_picker
redirect '/brother/'
end
%w[get post put delete].each do |verb|
send(verb, '/brother/:printer/*') do
printer = BROTHER_PRINTERS[params[:printer]]
halt 404, 'unknown printer' unless printer
proxy_to_brother(printer[:port], "/brother/#{params[:printer]}", '/' + params[:splat].first)
send(verb, '/brother/*') do
proxy_to_brother('/brother', '/' + params[:splat].first)
end
end