diff --git a/laserstudio/config_schema/config.schema.json b/laserstudio/config_schema/config.schema.json index f1e7938..70038ba 100644 --- a/laserstudio/config_schema/config.schema.json +++ b/laserstudio/config_schema/config.schema.json @@ -39,6 +39,16 @@ "items": { "$ref": "probe.schema.json" } + }, + "restserver": { + "allOf": [ + { + "$ref": "restserver.schema.json" + }, + { + "description": "The REST server configuration." + } + ] } } } diff --git a/laserstudio/config_schema/rest.schema.json b/laserstudio/config_schema/rest.schema.json index c263804..a931822 100644 --- a/laserstudio/config_schema/rest.schema.json +++ b/laserstudio/config_schema/rest.schema.json @@ -14,8 +14,8 @@ "description": "Port to connect to the REST device.", "minimum": 1, "maximum": 65535, - "example": 4000, - "default": 4000 + "example": 4444, + "default": 4444 }, "api_command": { "type": "string", diff --git a/laserstudio/config_schema/restserver.schema.json b/laserstudio/config_schema/restserver.schema.json new file mode 100644 index 0000000..ecf2e0e --- /dev/null +++ b/laserstudio/config_schema/restserver.schema.json @@ -0,0 +1,22 @@ +{ + "$id": "restserver.schema.json", + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "object", + "title": "REST Server", + "properties": { + "host": { + "type": "string", + "description": "Address on which the REST API must serve. Setting 0.0.0.0 for instance will make the server accessible from any network interface.", + "default": "localhost", + "examples": ["localhost", "0.0.0.0"] + }, + "port": { + "type": "integer", + "description": "Port number the REST server is listening to.", + "minimum": 1, + "maximum": 65535, + "example": 4444, + "default": 4444 + } + } +} diff --git a/laserstudio/laserstudio.py b/laserstudio/laserstudio.py index 676f3b6..3d0db90 100644 --- a/laserstudio/laserstudio.py +++ b/laserstudio/laserstudio.py @@ -115,7 +115,7 @@ def __init__(self, config: Optional[dict]): self.addToolBar(Qt.ToolBarArea.RightToolBarArea, toolbar) # Instantiate proxy for REST command reception - self.rest_proxy = RestProxy(self) + self.rest_proxy = RestProxy(self, config.get("restserver", {})) # Create shortcuts shortcut = QShortcut(Qt.Key.Key_Escape, self) diff --git a/laserstudio/restserver/server.py b/laserstudio/restserver/server.py index 04b3be7..f02ad2d 100644 --- a/laserstudio/restserver/server.py +++ b/laserstudio/restserver/server.py @@ -26,11 +26,11 @@ class RestProxy(QObject): laser studio. """ - def __init__(self, laser_studio: "LaserStudio"): + def __init__(self, laser_studio: "LaserStudio", config: dict): super().__init__() self.laser_studio: LaserStudio = laser_studio self.rest_object = RestServer(self) - self._thread = RestThread() + self._thread = RestThread(config) self.rest_object.moveToThread(self._thread) self._thread.start() @@ -92,8 +92,13 @@ class RestThread(QThread): Subclass of QThread where to launch the Rest server. """ + def __init__(self, config: dict, parent=None) -> None: + super().__init__(parent) + self.host = config.get("host", "localhost") + self.port = config.get("port", LSAPI.PORT) + def run(self): - RestServer.serve(LSAPI.PORT) + RestServer.serve(self.host, self.port) super(RestThread, self).run() @@ -117,13 +122,13 @@ def __init__(self, proxy: Optional[RestProxy], parent: Optional[QObject] = None) RestServer._shared = self @staticmethod - def serve(port: int): + def serve(host: str, port: int): """ Launch flask's REST server on the given port :param port: The HTTP port to listen """ - flask_app.run(host="localhost", port=port) + flask_app.run(host=host, port=port) @staticmethod def invoke(member: str, *args) -> QVariant: