From 3c7b4fc09c86776a39d53d0919bb0e1e233caa66 Mon Sep 17 00:00:00 2001 From: Antoine Mazeas Date: Tue, 7 Jan 2020 19:04:40 +0100 Subject: [PATCH 1/5] Make management UI aware of URL subpaths in config Signed-off-by: Antoine Mazeas --- maubot/management/frontend/src/api.js | 9 ++++++--- maubot/management/frontend/src/pages/Main.js | 16 +++++++++++++++ maubot/server.py | 21 +++++++++++++++++++- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/maubot/management/frontend/src/api.js b/maubot/management/frontend/src/api.js index dc4bbc0..61fcbd8 100644 --- a/maubot/management/frontend/src/api.js +++ b/maubot/management/frontend/src/api.js @@ -14,7 +14,11 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -export const BASE_PATH = "/_matrix/maubot/v1" +var BASE_PATH = "/_matrix/maubot/v1" + +export function setBasePath(basePath) { + BASE_PATH = basePath +} function getHeaders(contentType = "application/json") { return { @@ -241,8 +245,7 @@ export async function doClientAuth(server, type, username, password) { } export default { - BASE_PATH, - login, ping, getFeatures, remoteGetFeatures, + login, ping, setBasePath, getFeatures, remoteGetFeatures, openLogSocket, debugOpenFile, debugOpenFileEnabled, updateDebugOpenFileEnabled, getInstances, getInstance, putInstance, deleteInstance, diff --git a/maubot/management/frontend/src/pages/Main.js b/maubot/management/frontend/src/pages/Main.js index 33660d9..3434fe1 100644 --- a/maubot/management/frontend/src/pages/Main.js +++ b/maubot/management/frontend/src/pages/Main.js @@ -31,6 +31,7 @@ class Main extends Component { } async componentWillMount() { + await this.getBasePath() if (localStorage.accessToken) { await this.ping() } else { @@ -39,6 +40,21 @@ class Main extends Component { this.setState({ pinged: true }) } + async getBasePath() { + try { + const resp = await fetch("./paths.json", { + headers: { "Content-Type": "application/json" } + }) + const apiPathJson = await resp.json() + const apiPath = apiPathJson.api_path + console.log(apiPath) + api.setBasePath(`${apiPath}`) + } catch (err) { + console.error(err) + } + } + + async ping() { try { const username = await api.ping() diff --git a/maubot/server.py b/maubot/server.py index fa84e9d..e06307e 100644 --- a/maubot/server.py +++ b/maubot/server.py @@ -16,6 +16,8 @@ from typing import Tuple, Dict import logging import asyncio +import json +from urllib.parse import urlparse from aiohttp import web, hdrs from aiohttp.abc import AbstractAccessLogger @@ -63,7 +65,8 @@ class MaubotServer: def get_instance_subapp(self, instance_id: str) -> Tuple[PluginWebApp, str]: subpath = self.config["server.plugin_base_path"] + instance_id - url = self.config["server.public_url"] + subpath + path_prefix = self.config["server.public_url_path_prefix"].rstrip("/") + url = self.config["server.public_url"] + path_prefix + subpath try: return self.plugin_routes[subpath], url except KeyError: @@ -129,6 +132,22 @@ class MaubotServer: self.app.router.add_get(f"{ui_base}/{file}", lambda _: web.Response(body=data, content_type=mime)) + # also set up a resource path for the public url path prefix config + # cut the prefix path from public_url + public_url = self.config["server.public_url"] + base_path = self.config["server.base_path"] + public_url_path = "" + if public_url != "": + url_parts = urlparse(public_url) + public_url_path = url_parts.path.rstrip("/") + + # assemble with base_path + api_path = f"{public_url_path}{base_path}" + + path_prefix_response_body = json.dumps({"api_path": api_path.rstrip("/")}) + self.app.router.add_get(f"{ui_base}/paths.json", lambda _: web.Response(body=path_prefix_response_body, + content_type="application/json")) + def add_route(self, method: Method, path: PathBuilder, handler) -> None: self.app.router.add_route(method.value, str(path), handler) From 9d587e32f2f9687c059d5a55814b4f7551ebeefc Mon Sep 17 00:00:00 2001 From: Antoine Mazeas Date: Tue, 7 Jan 2020 19:21:37 +0100 Subject: [PATCH 2/5] Readd BASE_PATH export Signed-off-by: Antoine Mazeas --- maubot/management/frontend/src/api.js | 1 + 1 file changed, 1 insertion(+) diff --git a/maubot/management/frontend/src/api.js b/maubot/management/frontend/src/api.js index 61fcbd8..2ee7127 100644 --- a/maubot/management/frontend/src/api.js +++ b/maubot/management/frontend/src/api.js @@ -245,6 +245,7 @@ export async function doClientAuth(server, type, username, password) { } export default { + BASE_PATH, login, ping, setBasePath, getFeatures, remoteGetFeatures, openLogSocket, debugOpenFile, debugOpenFileEnabled, updateDebugOpenFileEnabled, From 3b603a21802ed2dcf7f91120fd99e426088f01fe Mon Sep 17 00:00:00 2001 From: Antoine Mazeas Date: Tue, 7 Jan 2020 19:36:17 +0100 Subject: [PATCH 3/5] Revert obsolete change Signed-off-by: Antoine Mazeas --- maubot/server.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/maubot/server.py b/maubot/server.py index e06307e..423a954 100644 --- a/maubot/server.py +++ b/maubot/server.py @@ -65,8 +65,7 @@ class MaubotServer: def get_instance_subapp(self, instance_id: str) -> Tuple[PluginWebApp, str]: subpath = self.config["server.plugin_base_path"] + instance_id - path_prefix = self.config["server.public_url_path_prefix"].rstrip("/") - url = self.config["server.public_url"] + path_prefix + subpath + url = self.config["server.public_url"] + subpath try: return self.plugin_routes[subpath], url except KeyError: From 3aadf0c2a9352a2709598b68efd1c9fc0b67d68b Mon Sep 17 00:00:00 2001 From: Antoine Mazeas Date: Thu, 9 Jan 2020 14:32:38 +0100 Subject: [PATCH 4/5] Swap urllib for yarl; make use of process.env Signed-off-by: Antoine Mazeas --- maubot/management/frontend/src/pages/Main.js | 2 +- maubot/server.py | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/maubot/management/frontend/src/pages/Main.js b/maubot/management/frontend/src/pages/Main.js index 3434fe1..dd5a9aa 100644 --- a/maubot/management/frontend/src/pages/Main.js +++ b/maubot/management/frontend/src/pages/Main.js @@ -42,7 +42,7 @@ class Main extends Component { async getBasePath() { try { - const resp = await fetch("./paths.json", { + const resp = await fetch(process.env.PUBLIC_URL + "/paths.json", { headers: { "Content-Type": "application/json" } }) const apiPathJson = await resp.json() diff --git a/maubot/server.py b/maubot/server.py index 423a954..efb0a13 100644 --- a/maubot/server.py +++ b/maubot/server.py @@ -17,7 +17,7 @@ from typing import Tuple, Dict import logging import asyncio import json -from urllib.parse import urlparse +from yarl import URL from aiohttp import web, hdrs from aiohttp.abc import AbstractAccessLogger @@ -136,9 +136,8 @@ class MaubotServer: public_url = self.config["server.public_url"] base_path = self.config["server.base_path"] public_url_path = "" - if public_url != "": - url_parts = urlparse(public_url) - public_url_path = url_parts.path.rstrip("/") + if public_url: + public_url_path = URL(public_url).path.rstrip("/") # assemble with base_path api_path = f"{public_url_path}{base_path}" From f65f4c540bf439247e754efef12984fcb26ec1c2 Mon Sep 17 00:00:00 2001 From: Antoine Mazeas Date: Mon, 20 Jan 2020 11:19:41 +0100 Subject: [PATCH 5/5] Review fixes based on comments Signed-off-by: Antoine Mazeas --- maubot/management/frontend/src/api.js | 3 +-- maubot/management/frontend/src/pages/Main.js | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/maubot/management/frontend/src/api.js b/maubot/management/frontend/src/api.js index 2ee7127..1f720e0 100644 --- a/maubot/management/frontend/src/api.js +++ b/maubot/management/frontend/src/api.js @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -var BASE_PATH = "/_matrix/maubot/v1" +let BASE_PATH = "/_matrix/maubot/v1" export function setBasePath(basePath) { BASE_PATH = basePath @@ -245,7 +245,6 @@ export async function doClientAuth(server, type, username, password) { } export default { - BASE_PATH, login, ping, setBasePath, getFeatures, remoteGetFeatures, openLogSocket, debugOpenFile, debugOpenFileEnabled, updateDebugOpenFileEnabled, diff --git a/maubot/management/frontend/src/pages/Main.js b/maubot/management/frontend/src/pages/Main.js index dd5a9aa..a2162a4 100644 --- a/maubot/management/frontend/src/pages/Main.js +++ b/maubot/management/frontend/src/pages/Main.js @@ -47,10 +47,9 @@ class Main extends Component { }) const apiPathJson = await resp.json() const apiPath = apiPathJson.api_path - console.log(apiPath) api.setBasePath(`${apiPath}`) } catch (err) { - console.error(err) + console.error("Failed to get API path:", err) } }