Pass public URL of webapp to plugins

This commit is contained in:
Tulir Asokan 2019-03-06 22:35:51 +02:00
parent c6287e6626
commit 19a20721e8
6 changed files with 19 additions and 8 deletions

View File

@ -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.

View File

@ -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")

View File

@ -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:

View File

@ -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:

View File

@ -13,6 +13,7 @@
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
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:

View File

@ -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",