Make management UI aware of URL subpaths in config

Signed-off-by: Antoine Mazeas <antoine@karthanis.net>
This commit is contained in:
Antoine Mazeas 2020-01-07 19:04:40 +01:00
parent 8a711e0c8e
commit 3c7b4fc09c
3 changed files with 42 additions and 4 deletions

View File

@ -14,7 +14,11 @@
// You should have received a copy of the GNU Affero General Public License // 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/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
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") { function getHeaders(contentType = "application/json") {
return { return {
@ -241,8 +245,7 @@ export async function doClientAuth(server, type, username, password) {
} }
export default { export default {
BASE_PATH, login, ping, setBasePath, getFeatures, remoteGetFeatures,
login, ping, getFeatures, remoteGetFeatures,
openLogSocket, openLogSocket,
debugOpenFile, debugOpenFileEnabled, updateDebugOpenFileEnabled, debugOpenFile, debugOpenFileEnabled, updateDebugOpenFileEnabled,
getInstances, getInstance, putInstance, deleteInstance, getInstances, getInstance, putInstance, deleteInstance,

View File

@ -31,6 +31,7 @@ class Main extends Component {
} }
async componentWillMount() { async componentWillMount() {
await this.getBasePath()
if (localStorage.accessToken) { if (localStorage.accessToken) {
await this.ping() await this.ping()
} else { } else {
@ -39,6 +40,21 @@ class Main extends Component {
this.setState({ pinged: true }) 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() { async ping() {
try { try {
const username = await api.ping() const username = await api.ping()

View File

@ -16,6 +16,8 @@
from typing import Tuple, Dict from typing import Tuple, Dict
import logging import logging
import asyncio import asyncio
import json
from urllib.parse import urlparse
from aiohttp import web, hdrs from aiohttp import web, hdrs
from aiohttp.abc import AbstractAccessLogger from aiohttp.abc import AbstractAccessLogger
@ -63,7 +65,8 @@ class MaubotServer:
def get_instance_subapp(self, instance_id: str) -> Tuple[PluginWebApp, str]: def get_instance_subapp(self, instance_id: str) -> Tuple[PluginWebApp, str]:
subpath = self.config["server.plugin_base_path"] + instance_id 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: try:
return self.plugin_routes[subpath], url return self.plugin_routes[subpath], url
except KeyError: except KeyError:
@ -129,6 +132,22 @@ class MaubotServer:
self.app.router.add_get(f"{ui_base}/{file}", lambda _: web.Response(body=data, self.app.router.add_get(f"{ui_base}/{file}", lambda _: web.Response(body=data,
content_type=mime)) 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: def add_route(self, method: Method, path: PathBuilder, handler) -> None:
self.app.router.add_route(method.value, str(path), handler) self.app.router.add_route(method.value, str(path), handler)