feat: Moonraker route prefix support (#1464)

* Added config directives to specify moonraker route prefix, and
explicitly specify SSL

* Added newly-implemented options to documentation

* Fixed boolean handling

* Cleanup

* Fixed trailing whitespace

* refactor: simplifications and lint fixes

---------

Co-authored-by: Alfredo Monclus <alfredomonclus@gmail.com>
This commit is contained in:
andrewmvandAlfredo Monclus authored and GitHub committed 2024-09-13 18:02:12 -03:00
1 parent 7fd759d5fc
commit 21a269edd2
5 files changed
+24 -8

No files matched your search

+6 -1
View File
@@ -46,8 +46,13 @@ Multiple printers can be defined
[printer MyPrinter]
# Define the moonraker host/port if different from 127.0.0.1 and 7125
moonraker_host: 127.0.0.1
# ports 443 and 7130 will use https/wss
moonraker_port: 7125
# Use HTTPS/WSS. Defaults to True for ports 443 or 7130, False for any other port
# moonraker_ssl: False
# If you're using the route_prefix option in your moonraker config, specify it here.
# This can be useful for running multiple printers behind a path-based reverse proxy.
# Most installs will not need this.
# moonraker_path: printer1
# Moonraker API key if this host is not connecting from a trusted client IP
# moonraker_api_key: False
+5 -2
View File
@@ -4,15 +4,18 @@ import requests
class KlippyRest:
def __init__(self, ip, port=7125, api_key=False):
def __init__(self, ip, port=7125, api_key=False, path='', ssl=None):
self.ip = ip
self.port = port
self.path = f"/{path}" if path else ''
self.ssl = ssl
self.api_key = api_key
self.ssl = int(self.port) in {443, 7130} if ssl is None else bool(ssl)
self.status = ''
@property
def endpoint(self):
return f"{'https' if int(self.port) in {443, 7130} else 'http'}://{self.ip}:{self.port}"
return f"{'https' if self.ssl else 'http'}://{self.ip}:{self.port}{self.path}"
@staticmethod
def process_response(response):
+5 -3
View File
@@ -20,7 +20,7 @@ class KlippyWebsocket(threading.Thread):
reconnect_count = 0
max_retries = 4
def __init__(self, callback, host, port, api_key):
def __init__(self, callback, host, port, api_key, path='', ssl=None):
threading.Thread.__init__(self)
self._wst = None
self.ws_url = None
@@ -30,16 +30,18 @@ class KlippyWebsocket(threading.Thread):
self.closing = False
self.host = host
self.port = port
self.path = f"/{path}" if path else ''
self.ssl = int(self.port) in {443, 7130} if ssl is None else bool(ssl)
self.header = {"x-api-key": api_key} if api_key else {}
self.api_key = api_key
@property
def _url(self):
return f"{self.host}:{self.port}"
return f"{self.host}:{self.port}{self.path}"
@property
def ws_proto(self):
return "wss" if int(self.port) in {443, 7130} else "ws"
return "wss" if self.ssl else "ws"
def initial_connect(self):
if self.connect() is not False:
+4 -2
View File
@@ -98,6 +98,8 @@ class KlipperScreenConfig:
{printer[8:]: {
"moonraker_host": self.config.get(printer, "moonraker_host", fallback="127.0.0.1"),
"moonraker_port": self.config.get(printer, "moonraker_port", fallback="7125"),
"moonraker_path": self.config.get(printer, "moonraker_path", fallback='').strip('/'),
"moonraker_ssl": self.config.getboolean(printer, "moonraker_ssl", fallback=None),
"moonraker_api_key": self.config.get(printer, "moonraker_api_key", fallback="").replace('"', '')
}} for printer in printers
]
@@ -173,10 +175,10 @@ class KlipperScreenConfig:
)
elif section.startswith('printer '):
bools = (
'invert_x', 'invert_y', 'invert_z',
'invert_x', 'invert_y', 'invert_z', 'moonraker_ssl',
)
strs = (
'moonraker_api_key', 'moonraker_host', 'titlebar_name_type',
'moonraker_api_key', 'moonraker_host', 'moonraker_path', 'titlebar_name_type',
'screw_positions', 'power_devices', 'titlebar_items', 'z_babystep_values',
'extrude_distances', 'extrude_speeds', 'move_distances', 'zcalibrate_custom_commands'
)
+4
View File
@@ -235,6 +235,8 @@ class KlipperScreen(Gtk.Window):
self.printers[ind][name]["moonraker_host"],
self.printers[ind][name]["moonraker_port"],
self.printers[ind][name]["moonraker_api_key"],
self.printers[ind][name]["moonraker_path"],
self.printers[ind][name]["moonraker_ssl"],
)
self._ws = KlippyWebsocket(
{
@@ -246,6 +248,8 @@ class KlipperScreen(Gtk.Window):
self.printers[ind][name]["moonraker_host"],
self.printers[ind][name]["moonraker_port"],
self.printers[ind][name]["moonraker_api_key"],
self.printers[ind][name]["moonraker_path"],
self.printers[ind][name]["moonraker_ssl"],
)
if self.files is None:
self.files = KlippyFiles(self)