Skip to content

Commit

Permalink
add subpath
Browse files Browse the repository at this point in the history
  • Loading branch information
freshavocado7 committed Apr 9, 2024
1 parent 4dff213 commit 72f8d4c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
22 changes: 12 additions & 10 deletions capella_model_explorer/backend/explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import capellambse
import yaml
from fastapi import FastAPI, Request
from fastapi import APIRouter, FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
Expand Down Expand Up @@ -46,30 +46,32 @@ def __post_init__(self):
)
self.app.state.templates = Jinja2Templates(directory=PATH_TO_FRONTEND)

self.configure_routes()
router = APIRouter()
self.configure_routes(router)
self.app.include_router(router, prefix="/myapp")

def configure_routes(self):
self.app.mount(
def configure_routes(self, router: APIRouter):
router.mount(
"/assets",
StaticFiles(
directory=PATH_TO_FRONTEND.joinpath("assets"), html=True
),
)

@self.app.get("/api/views")
@router.get("/api/views")
def read_templates():
# list all templates in the templates folder from .yaml
self.grouped_templates, self.templates = index_templates(
self.templates_path
)
return self.grouped_templates

@self.app.get("/api/objects/{uuid}")
@router.get("/api/objects/{uuid}")
def read_object(uuid: str):
obj = self.model.by_uuid(uuid)
return {"idx": obj.uuid, "name": obj.name, "type": obj.xtype}

@self.app.get("/api/views/{template_name}")
@router.get("/api/views/{template_name}")
def read_template(template_name: str):
template_name = urlparse.unquote(template_name)
if not template_name in self.templates:
Expand All @@ -96,7 +98,7 @@ def read_template(template_name: str):
base["error"] = str(e)
return base

@self.app.get("/api/views/{template_name}/{object_id}")
@router.get("/api/views/{template_name}/{object_id}")
def render_template(template_name: str, object_id: str):
content = None
object = None
Expand Down Expand Up @@ -137,7 +139,7 @@ def render_template(template_name: str, object_id: str):
)
return HTMLResponse(content=error_message)

@self.app.get("/api/model-info")
@router.get("/api/model-info")
def model_info():
info = self.model.info
return {
Expand All @@ -149,7 +151,7 @@ def model_info():
"badge": self.model.description_badge,
}

@self.app.get("/{rest_of_path:path}")
@router.get("/{rest_of_path:path}")
async def catch_all(request: Request, rest_of_path: str):
del rest_of_path
return self.app.state.templates.TemplateResponse(
Expand Down
3 changes: 3 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
sed -i 's#__ROUTE_PREFIX__#'"$ROUTE_PREFIX"'#g' /app/frontend/dist/env.js
exec "$@"
3 changes: 3 additions & 0 deletions frontend/public/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
window.env = {
ROUTE_PREFIX: '__ROUTE_PREFIX__',
};
16 changes: 11 additions & 5 deletions frontend/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@ import { HomeView } from './views/HomeView'

function App() {
const [count, setCount] = useState(0)


{
/*const BACKEND_URL = window.env.BACKEND_URL;*/
}
return (
<Router>
<Routes>
<Route path="/" element={<HomeView/>} />
{/* <Route path="/views" element={<WiredTemplatesList endpoint="http://localhost:8000/api/views" />} /> */}
<Route path="/:templateName" element={<TemplateView endpoint="http://localhost:8000/api/views/" />} />
<Route path="/:templateName/:objectID" element={<TemplateView endpoint="http://localhost:8000/api/views/" />} />
<Route path="/" element={
<Routes>
<Route index element={<HomeView/>} />
<Route path="/:templateName" element={<TemplateView endpoint="http://localhost:8000/api/views/" />} />
<Route path="/:templateName/:objectID" element={<TemplateView endpoint="http://localhost:8000/api/views/" />} />
</Routes>
} />
</Routes>
</Router>
)
Expand Down
11 changes: 9 additions & 2 deletions frontend/src/components/WiredTemplatesList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import React, {useState, useEffect} from 'react';
import { useNavigate } from 'react-router-dom';
import { useNavigate, useLocation } from 'react-router-dom';
import { ViewsList } from './ViewsList';
import { API_BASE_URL } from '../APIConfig';

Expand All @@ -11,6 +11,7 @@ export const WiredTemplatesList = () => {
const [templates, setTemplates] = useState([])
const [error, setError] = useState(null);
const navigate = useNavigate();
const location = useLocation();

useEffect(() => {
const fetchTemplates = async () => {
Expand Down Expand Up @@ -42,4 +43,10 @@ export const WiredTemplatesList = () => {
}
</div>);
}
return <ViewsList templates={templates} cardClickCallback={(idx) => navigate(`/${idx}`, {state: {idx: idx}})} />;}
return (
<ViewsList
templates={templates}
cardClickCallback={(idx) => navigate(`/${location.pathname}/${idx}`, {state: {idx: idx}})}
/>
);
}

0 comments on commit 72f8d4c

Please sign in to comment.