Add Brother printer switcher proxied through one port, move sinatra to port 80
This commit is contained in:
1 parent
2ea45b5f21
commit
daa39320a6
3 files changed
+82
-2
No files matched your search
@@ -2,11 +2,12 @@
|
|||||||
require 'sinatra'
|
require 'sinatra'
|
||||||
require "sinatra/reloader"
|
require "sinatra/reloader"
|
||||||
require 'tmpdir'
|
require 'tmpdir'
|
||||||
|
require 'net/http'
|
||||||
|
|
||||||
set :protection, :except => :frame_options
|
set :protection, :except => :frame_options
|
||||||
set :environment, :production
|
set :environment, :production
|
||||||
set :bind, "::"
|
set :bind, "::"
|
||||||
set :port, 4567
|
set :port, 80
|
||||||
|
|
||||||
if !File.exist?('config.json') || !File.readable?('config.json')
|
if !File.exist?('config.json') || !File.readable?('config.json')
|
||||||
raise RuntimeException, 'Could not read config.json'
|
raise RuntimeException, 'Could not read config.json'
|
||||||
@@ -17,6 +18,12 @@ PRINTER_DPI = 203 # DatamaxONeil E4204B Mark III native head resolution
|
|||||||
PRINTER_DARKNESS = 8 # lower than factory default to stop thermal dot-gain from fusing barcode bars
|
PRINTER_DARKNESS = 8 # lower than factory default to stop thermal dot-gain from fusing barcode bars
|
||||||
TEST_LABEL_PDF = File.join(File.dirname(__FILE__), 'test-label.pdf')
|
TEST_LABEL_PDF = File.join(File.dirname(__FILE__), 'test-label.pdf')
|
||||||
|
|
||||||
|
BROTHER_PRINTERS = {
|
||||||
|
'ql560' => { port: 8560, label: 'Brother QL-560' },
|
||||||
|
'ql700' => { port: 8700, label: 'Brother QL-700' },
|
||||||
|
'ql1100' => { port: 9100, label: 'Brother QL-1100' }
|
||||||
|
}.freeze
|
||||||
|
|
||||||
configure do
|
configure do
|
||||||
enable :static
|
enable :static
|
||||||
set :session_secret, File.read(File.join(File.dirname(__FILE__), '.session_secret')).strip
|
set :session_secret, File.read(File.join(File.dirname(__FILE__), '.session_secret')).strip
|
||||||
@@ -73,6 +80,36 @@ helpers do
|
|||||||
|
|
||||||
"^XA\n^MD#{PRINTER_DARKNESS}\n^PW#{width}\n^LL#{height}\n^FO0,0^GFA,#{total_bytes},#{total_bytes},#{bytes_per_row},#{hexdata}\n^XZ\n"
|
"^XA\n^MD#{PRINTER_DARKNESS}\n^PW#{width}\n^LL#{height}\n^FO0,0^GFA,#{total_bytes},#{total_bytes},#{bytes_per_row},#{hexdata}\n^XZ\n"
|
||||||
end
|
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)
|
||||||
|
uri = URI("http://127.0.0.1:#{port}#{path}")
|
||||||
|
uri.query = request.query_string unless request.query_string.empty?
|
||||||
|
|
||||||
|
proxied = Net::HTTP.const_get(request.request_method.capitalize).new(uri)
|
||||||
|
request.env.each do |key, value|
|
||||||
|
next unless key.start_with?('HTTP_') && key != 'HTTP_HOST'
|
||||||
|
|
||||||
|
header = key.sub(/^HTTP_/, '').split('_').map(&:capitalize).join('-')
|
||||||
|
proxied[header] = value
|
||||||
|
end
|
||||||
|
proxied['Content-Type'] = request.content_type if request.content_type
|
||||||
|
proxied['X-Forwarded-Prefix'] = '/brother'
|
||||||
|
proxied['X-Forwarded-Host'] = request.host
|
||||||
|
proxied.body = request.body.read unless request.request_method == 'GET'
|
||||||
|
|
||||||
|
res = Net::HTTP.start(uri.host, uri.port) { |http| http.request(proxied) }
|
||||||
|
|
||||||
|
headers = {}
|
||||||
|
res.each_header do |key, value|
|
||||||
|
next if %w[transfer-encoding connection content-length].include?(key.downcase)
|
||||||
|
|
||||||
|
headers[key] = value
|
||||||
|
end
|
||||||
|
[res.code.to_i, headers, [res.body || '']]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
before do
|
before do
|
||||||
@@ -136,3 +173,26 @@ post '/test-print' do
|
|||||||
end
|
end
|
||||||
redirect '/upload'
|
redirect '/upload'
|
||||||
end
|
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
|
||||||
|
|
||||||
|
proxy_to_brother(BROTHER_PRINTERS[key][:port], '/' + params[:splat].first)
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
<h1>Brother printers</h1>
|
||||||
|
<p>Choose a printer to open its label designer through this server.</p>
|
||||||
|
|
||||||
|
<div class="list-group" style="max-width: 400px;">
|
||||||
|
<% @printers.each do |key, info| %>
|
||||||
|
<form action="/brother/select" method="POST" class="mb-2">
|
||||||
|
<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 %>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<% if @selected %>
|
||||||
|
<p class="mt-3"><a href="/brother/">Open <%= @printers[@selected][:label] %> »</a></p>
|
||||||
|
<% end %>
|
||||||
+3
-1
@@ -24,6 +24,9 @@
|
|||||||
<li class="nav-item">
|
<li class="nav-item">
|
||||||
<a class="nav-link" href="/upload">Shipping labels</a>
|
<a class="nav-link" href="/upload">Shipping labels</a>
|
||||||
</li>
|
</li>
|
||||||
|
<li class="nav-item">
|
||||||
|
<a class="nav-link" href="/brother">Brother printers</a>
|
||||||
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -53,4 +56,3 @@
|
|||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
||||||
Reference in new issue
Block a user