diff --git a/metacatalog_api/apps/explorer/create.py b/metacatalog_api/apps/explorer/create.py index d379fef..40b3102 100644 --- a/metacatalog_api/apps/explorer/create.py +++ b/metacatalog_api/apps/explorer/create.py @@ -10,7 +10,7 @@ @create_router.get('/create/entries.html') def new_entry_page(request: Request): - return templates.TemplateResponse(request=request, name="add_entry.html", context={"path": server.uri_prefix}) + return templates.TemplateResponse(request=request, name="add_entry.html", context={"path": server.app_prefix}) @create_router.get("/utils/leaflet_draw.html") @@ -23,16 +23,16 @@ def leaflet_draw(request: Request, geom: str = 'marker'): @create_router.get('/create/authors.html') def new_author(request: Request): - return templates.TemplateResponse(request=request, name="author.html", context={"path": server.uri_prefix}) + return templates.TemplateResponse(request=request, name="author.html", context={"path": server.app_prefix}) @create_router.get('/create/details.html') def new_details(request: Request): - return templates.TemplateResponse(request=request, name="details.html", context={"path": server.uri_prefix}) + return templates.TemplateResponse(request=request, name="details.html", context={"path": server.app_prefix}) @create_router.get('/create/datasources.html') def new_datasource(request: Request): # load the datasource types types = core.datatypes() - return templates.TemplateResponse(request=request, name="add_datasource.html", context={"types": types, "path": server.uri_prefix}) + return templates.TemplateResponse(request=request, name="add_datasource.html", context={"types": types, "path": server.app_prefix}) diff --git a/metacatalog_api/apps/explorer/read.py b/metacatalog_api/apps/explorer/read.py index bec429b..09f869c 100644 --- a/metacatalog_api/apps/explorer/read.py +++ b/metacatalog_api/apps/explorer/read.py @@ -12,7 +12,7 @@ templates = Jinja2Templates(directory=Path(__file__).parent / 'templates') # add static files -explorer_router.mount("/static", StaticFiles(directory=Path(__file__).parent / "templates" / "static"), name="static") +# explorer_router.mount("/static", StaticFiles(directory=Path(__file__).parent / "templates" / "static"), name="static") @explorer_router.get('/entries.html') @@ -26,13 +26,13 @@ def get_entries_page(request: Request, offset: int = 0, limit: int = 100, search entries = core.entries(offset, limit, search=search, full_text=full_text, title=title, variable=variable) # check if we should return html - return templates.TemplateResponse(request=request, name="entries.html", context={"entries": entries, "path": server.uri_prefix}) + return templates.TemplateResponse(request=request, name="entries.html", context={"entries": entries, "path": server.app_prefix}) @explorer_router.get('/locations.html') def get_entries_geojson_page(request: Request): # check if we should return html - return templates.TemplateResponse(request=request, name="map.html", context={"path": server.uri_prefix}) + return templates.TemplateResponse(request=request, name="map.html", context={"path": server.app_prefix}) @explorer_router.get('/entries/{id}.html') @@ -44,7 +44,7 @@ def get_entry_page(id: int, request: Request): raise HTTPException(status_code=404, detail=f"Entry of not found") # check if we should return html - return templates.TemplateResponse(request=request, name="entry.html", context={"entry": entries[0], "path": server.uri_prefix}) + return templates.TemplateResponse(request=request, name="entry.html", context={"entry": entries[0], "path": server.app_prefix}) @explorer_router.get('/entries/{id}.xml') @@ -56,7 +56,7 @@ def get_entry_radar_xml(id: int, request: Request): if len(entries) == 0: raise HTTPException(status_code=404, detail=f"Entry of not found") - return templates.TemplateResponse(request=request, name="entry.radar.xml", context={"entry": entries[0], "path": server.uri_prefix}, media_type='application/xml') + return templates.TemplateResponse(request=request, name="entry.radar.xml", context={"entry": entries[0], "path": server.app_prefix}, media_type='application/xml') @explorer_router.get('/licenses.html') @@ -71,7 +71,7 @@ def get_licenses_page(request: Request, license_id: int | None = None): if license_id is not None: return templates.TemplateResponse(request=request, name="license.html", context={"license": licenses.model_dump()}) else: - return templates.TemplateResponse(request=request, name="licenses.html", context={"licenses": licenses, "path": server.uri_prefix}) + return templates.TemplateResponse(request=request, name="licenses.html", context={"licenses": licenses, "path": server.app_prefix}) @explorer_router.get('/authors.html') @@ -87,13 +87,13 @@ def get_authors_page(request: Request, entry_id: int | None = None, author_id: i return templates.TemplateResponse( request=request, name="author.html", - context={"author": authors, 'variant': 'fixed', 'target': target, "path": server.uri_prefix} + context={"author": authors, 'variant': 'fixed', 'target': target, "path": server.app_prefix} ) return templates.TemplateResponse( request=request, name="authors.html", - context={"authors": authors, 'variant': 'select' if entry_id is None else 'list', 'target': target, "path": server.uri_prefix} + context={"authors": authors, 'variant': 'select' if entry_id is None else 'list', 'target': target, "path": server.app_prefix, "root_path": server.uri_prefix} ) @explorer_router.get('/variables') @@ -107,5 +107,5 @@ def get_variables_page(request: Request, offset: int = None, limit: int = None): return templates.TemplateResponse( request=request, name="variables.html", - context={"variables": variables, "path": server.uri_prefix} + context={"variables": variables, "path": server.app_prefix} ) diff --git a/metacatalog_api/apps/explorer/templates/authors.html b/metacatalog_api/apps/explorer/templates/authors.html index 81e3687..6ae0088 100644 --- a/metacatalog_api/apps/explorer/templates/authors.html +++ b/metacatalog_api/apps/explorer/templates/authors.html @@ -82,7 +82,7 @@

Debug Info:

async fetchExistingAuthors() { try { - const response = await fetch('{{ path }}authors.json'); + const response = await fetch('{{ root_path }}authors.json'); const data = await response.json(); this.existingAuthors = data; } catch (err) { diff --git a/metacatalog_api/default_server.py b/metacatalog_api/default_server.py index 70c6ba1..7a4a7bc 100644 --- a/metacatalog_api/default_server.py +++ b/metacatalog_api/default_server.py @@ -1,4 +1,6 @@ from fastapi import Request +from fastapi.staticfiles import StaticFiles +from pathlib import Path from starlette.middleware.cors import CORSMiddleware from metacatalog_api.server import app, server @@ -27,7 +29,7 @@ def index(request: Request): This example app includes the explorer read and create routes which are powered by the api route """ - return templates.TemplateResponse(request=request, name="index.html", context={"path": server.uri_prefix}) + return templates.TemplateResponse(request=request, name="index.html", context={"path": server.app_prefix}) # add all api routes - currently this is only splitted into read and create @@ -35,6 +37,7 @@ def index(request: Request): app.include_router(api_create_router) # add the default explorer application (the HTML) +app.mount(f"{server.app_prefix}static", StaticFiles(directory=Path(__file__).parent / "apps" / "explorer" / "templates" / "static"), name="static") app.include_router(explorer_router, prefix=f"/{server.app_name}") app.include_router(explorer_create, prefix=f"/{server.app_name}") diff --git a/metacatalog_api/router/api/read.py b/metacatalog_api/router/api/read.py index be4d734..4232a1b 100644 --- a/metacatalog_api/router/api/read.py +++ b/metacatalog_api/router/api/read.py @@ -93,8 +93,8 @@ def get_variables(only_available: bool = False, offset: int = None, limit: int = return variables -@read_router.get('/variable/{id}') -@read_router.get('/variable/{id}.json') +@read_router.get('/variables/{id}') +@read_router.get('/variables/{id}.json') def get_variable(id: int): try: variable = core.variables(id=id) diff --git a/metacatalog_api/server.py b/metacatalog_api/server.py index 533c9fd..b9049ae 100644 --- a/metacatalog_api/server.py +++ b/metacatalog_api/server.py @@ -31,6 +31,11 @@ def uri_prefix(self): if not path.endswith('/'): path += '/' + return path + + @property + def app_prefix(self): + path = self.uri_prefix if self.app_name.startswith('/'): path += self.app_name.strip('/') @@ -50,7 +55,7 @@ def cli_cmd(self, asgi_app: str): # create the server object server = Server() -logger.info(server.uri_prefix, server.root_path, server.app_name) +logger.info(server.app_prefix, server.root_path, server.app_name) # before we initialize the app, we check that the database is installed and up to date