Skip to content

Commit

Permalink
Merge pull request maubot#80 from karthanistyr/feature/ui_support_sub…
Browse files Browse the repository at this point in the history
…path

Make management UI aware of URL subpaths in config
  • Loading branch information
tulir authored Jan 23, 2020
2 parents 7f64d21 + f65f4c5 commit d874f43
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
9 changes: 6 additions & 3 deletions maubot/management/frontend/src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
// 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/>.

export const BASE_PATH = "/_matrix/maubot/v1"
let BASE_PATH = "/_matrix/maubot/v1"

export function setBasePath(basePath) {
BASE_PATH = basePath
}

function getHeaders(contentType = "application/json") {
return {
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions maubot/management/frontend/src/pages/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Main extends Component {
}

async componentWillMount() {
await this.getBasePath()
if (localStorage.accessToken) {
await this.ping()
} else {
Expand All @@ -39,6 +40,20 @@ class Main extends Component {
this.setState({ pinged: true })
}

async getBasePath() {
try {
const resp = await fetch(process.env.PUBLIC_URL + "/paths.json", {
headers: { "Content-Type": "application/json" }
})
const apiPathJson = await resp.json()
const apiPath = apiPathJson.api_path
api.setBasePath(`${apiPath}`)
} catch (err) {
console.error("Failed to get API path:", err)
}
}


async ping() {
try {
const username = await api.ping()
Expand Down
17 changes: 17 additions & 0 deletions maubot/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from typing import Tuple, Dict
import logging
import asyncio
import json
from yarl import URL

from aiohttp import web, hdrs
from aiohttp.abc import AbstractAccessLogger
Expand Down Expand Up @@ -129,6 +131,21 @@ def setup_static_root_files(self, directory: str, ui_base: str) -> None:
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:
public_url_path = URL(public_url).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)

Expand Down

0 comments on commit d874f43

Please sign in to comment.