From 19a20721e868640a858234b63de30100779cc21d Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Wed, 6 Mar 2019 22:35:51 +0200 Subject: [PATCH] Pass public URL of webapp to plugins --- example-config.yaml | 2 ++ maubot/config.py | 1 + maubot/instance.py | 7 +++++-- maubot/plugin_base.py | 4 +++- maubot/server.py | 11 +++++++---- setup.py | 2 +- 6 files changed, 19 insertions(+), 8 deletions(-) diff --git a/example-config.yaml b/example-config.yaml index f4bc3d6..c296f2b 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -22,6 +22,8 @@ server: # The IP and port to listen to. hostname: 0.0.0.0 port: 29316 + # Public base URL where the server is visible. + public_url: https://example.com # The base management API path. base_path: /_matrix/maubot/v1 # The base path for the UI. diff --git a/maubot/config.py b/maubot/config.py index b1ee67f..57e6552 100644 --- a/maubot/config.py +++ b/maubot/config.py @@ -38,6 +38,7 @@ class Config(BaseFileConfig): copy("plugin_directories.db") copy("server.hostname") copy("server.port") + copy("server.public_url") copy("server.listen") copy("server.base_path") copy("server.ui_base_path") diff --git a/maubot/instance.py b/maubot/instance.py index 10db272..af7d86a 100644 --- a/maubot/instance.py +++ b/maubot/instance.py @@ -60,6 +60,7 @@ class PluginInstance: inst_db: sql.engine.Engine inst_db_tables: Dict[str, sql.Table] inst_webapp: web.Application + inst_webapp_url: str started: bool def __init__(self, db_instance: DBPlugin): @@ -73,6 +74,7 @@ class PluginInstance: self.inst_db = None self.inst_db_tables = None self.inst_webapp = None + self.inst_webapp_url = None self.base_cfg = None self.cache[self.id] = self @@ -113,7 +115,7 @@ class PluginInstance: db_path = os.path.join(self.mb_config["plugin_directories.db"], self.id) self.inst_db = sql.create_engine(f"sqlite:///{db_path}.db") if self.loader.meta.webapp: - self.inst_webapp = self.webserver.get_instance_subapp(self.id) + self.inst_webapp, self.inst_webapp_url = self.webserver.get_instance_subapp(self.id) self.log.debug("Plugin instance dependencies loaded") self.loader.references.add(self) self.client.references.add(self) @@ -168,7 +170,8 @@ class PluginInstance: self.config = config_class(self.load_config, lambda: self.base_cfg, self.save_config) self.plugin = cls(client=self.client.client, loop=self.loop, http=self.client.http_client, instance_id=self.id, log=self.log, config=self.config, - database=self.inst_db, webapp=self.inst_webapp) + database=self.inst_db, webapp=self.inst_webapp, + webapp_url=self.inst_webapp_url) try: await self.plugin.start() except Exception: diff --git a/maubot/plugin_base.py b/maubot/plugin_base.py index 1e40bd9..ce2d2f4 100644 --- a/maubot/plugin_base.py +++ b/maubot/plugin_base.py @@ -37,7 +37,8 @@ class Plugin(ABC): def __init__(self, client: 'MaubotMatrixClient', loop: AbstractEventLoop, http: ClientSession, instance_id: str, log: Logger, config: Optional['BaseProxyConfig'], - database: Optional[Engine], webapp: Optional[Application]) -> None: + database: Optional[Engine], webapp: Optional[Application], + webapp_url: Optional[str]) -> None: self.client = client self.loop = loop self.http = http @@ -46,6 +47,7 @@ class Plugin(ABC): self.config = config self.database = database self.webapp = webapp + self.webapp_url = webapp_url self._handlers_at_startup = [] async def start(self) -> None: diff --git a/maubot/server.py b/maubot/server.py index ed3eebf..2bab5a7 100644 --- a/maubot/server.py +++ b/maubot/server.py @@ -13,6 +13,7 @@ # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +from typing import Tuple import logging import asyncio @@ -49,14 +50,16 @@ class MaubotServer: self.runner = web.AppRunner(self.app, access_log_class=AccessLogger) - def get_instance_subapp(self, instance_id: str) -> web.Application: + def get_instance_subapp(self, instance_id: str) -> Tuple[web.Application, str]: + subpath = self.config["server.plugin_base_path"].format(id=instance_id) + url = self.config["server.public_url"] + subpath try: - return self.subapps[instance_id] + return self.subapps[instance_id], url except KeyError: app = web.Application(loop=self.loop) - self.app.add_subapp(self.config["server.plugin_base_path"].format(id=instance_id), app) + self.app.add_subapp(subpath, app) self.subapps[instance_id] = app - return app + return app, url def remove_instance_webapp(self, instance_id: str) -> None: try: diff --git a/setup.py b/setup.py index a25d1a8..b58bfca 100644 --- a/setup.py +++ b/setup.py @@ -21,7 +21,7 @@ setuptools.setup( packages=setuptools.find_packages(), install_requires=[ - "mautrix>=0.4.dev20,<0.5", + "mautrix>=0.4.dev24,<0.5", "aiohttp>=3.0.1,<4", "SQLAlchemy>=1.2.3,<2", "alembic>=1.0.0,<2",