Skip to content

Commit

Permalink
Add REST server configuration in configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
mmouchous-ledger committed Jan 21, 2025
1 parent 05d9685 commit 1caeafd
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 8 deletions.
10 changes: 10 additions & 0 deletions laserstudio/config_schema/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@
"items": {
"$ref": "probe.schema.json"
}
},
"restserver": {
"allOf": [
{
"$ref": "restserver.schema.json"
},
{
"description": "The REST server configuration."
}
]
}
}
}
4 changes: 2 additions & 2 deletions laserstudio/config_schema/rest.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
22 changes: 22 additions & 0 deletions laserstudio/config_schema/restserver.schema.json
Original file line number Diff line number Diff line change
@@ -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
}
}
}
2 changes: 1 addition & 1 deletion laserstudio/laserstudio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 10 additions & 5 deletions laserstudio/restserver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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()


Expand All @@ -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:
Expand Down

0 comments on commit 1caeafd

Please sign in to comment.