diff --git a/backend/app/core/config.py b/backend/app/core/config.py index 39a1fac6..ac18a634 100644 --- a/backend/app/core/config.py +++ b/backend/app/core/config.py @@ -8,6 +8,7 @@ LICENSE_IDENTIFIER = "AGPL-3.0-or-later" LICENSE_URL = "https://spdx.org/licenses/AGPL-3.0-or-later.html" ROOT_PATH = "/api/v1" +FRONTEND_DIR = "/app/frontend/dist" def check_required_env_vars(): diff --git a/backend/app/core/router.py b/backend/app/core/router.py index dee9daf0..6c47d26b 100644 --- a/backend/app/core/router.py +++ b/backend/app/core/router.py @@ -1,4 +1,4 @@ -from fastapi import APIRouter +from fastapi import APIRouter, HTTPException, status import core.config as core_config import core.utils as core_utils @@ -7,46 +7,6 @@ router = APIRouter() -@router.get("/login", include_in_schema=False) -async def read_index_login(): - return core_utils.return_frontend_index() - - -@router.get("/activity/{activity_id}", include_in_schema=False) -async def read_index_activity(): - return core_utils.return_frontend_index() - - -@router.get("/gears", include_in_schema=False) -async def read_index_gears(): - return core_utils.return_frontend_index() - - -@router.get("/gear/{gear_id}", include_in_schema=False) -async def read_index_gear(): - return core_utils.return_frontend_index() - - -@router.get("/health", include_in_schema=False) -async def read_index_health(): - return core_utils.return_frontend_index() - - -@router.get("/user/{user_id}", include_in_schema=False) -async def read_index_user(): - return core_utils.return_frontend_index() - - -@router.get("/settings", include_in_schema=False) -async def read_index_settings(): - return core_utils.return_frontend_index() - - -@router.get("/strava/callback", include_in_schema=False) -async def read_index_strava_callback(): - return core_utils.return_frontend_index() - - @router.get( core_config.ROOT_PATH + "/about", ) @@ -64,9 +24,23 @@ async def about(): @router.get("/api/v1/{catchall:path}", include_in_schema=False) def api_not_found(): - return core_utils.return_frontend_index() - - -@router.get("/{catchall:path}", include_in_schema=False) -def api_not_found(): - return core_utils.return_frontend_index() + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="API endpoint not found", + ) + + +@router.get("/user_images/{user_img}", include_in_schema=False) +def user_img_return( + user_img: str, +): + return core_utils.return_user_img_path(user_img) + + +@router.get("/{path:path}", include_in_schema=False) +def frontend_not_found( + path: str, +): + if "." in path.split("/")[-1]: + return core_utils.return_frontend_index(path) + return core_utils.return_frontend_index("index.html") diff --git a/backend/app/core/utils.py b/backend/app/core/utils.py index 60114b8b..98331987 100644 --- a/backend/app/core/utils.py +++ b/backend/app/core/utils.py @@ -1,4 +1,8 @@ -from fastapi.responses import RedirectResponse +from fastapi.responses import FileResponse -def return_frontend_index(): - return RedirectResponse(url="/index.html") \ No newline at end of file +def return_frontend_index(path: str): + return FileResponse("/app/frontend/dist/" + path) + + +def return_user_img_path(user_img: str): + return FileResponse("/app/backend/user_images/" + user_img) \ No newline at end of file diff --git a/backend/app/main.py b/backend/app/main.py index ca1439a4..4db2acb0 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -1,10 +1,8 @@ import os -from fastapi import FastAPI, HTTPException +from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from fastapi.staticfiles import StaticFiles -from fastapi.responses import FileResponse -from fastapi import Request from alembic.config import Config from alembic import command @@ -99,7 +97,7 @@ def create_app() -> FastAPI: # Add a route to serve the user images app.mount("/user_images", StaticFiles(directory="user_images"), name="user_images") app.mount( - "/", StaticFiles(directory="/app/frontend/dist", html=True), name="frontend" + "/", StaticFiles(directory=core_config.FRONTEND_DIR, html=True), name="frontend" ) return app diff --git a/frontend/app/src/components/Users/UserAvatarComponent.vue b/frontend/app/src/components/Users/UserAvatarComponent.vue index 84235298..37eeb5ab 100644 --- a/frontend/app/src/components/Users/UserAvatarComponent.vue +++ b/frontend/app/src/components/Users/UserAvatarComponent.vue @@ -30,7 +30,7 @@ export default { emits: ['userDeleted'], setup(props) { const altText = ref('User Avatar'); - const userPhotoUrl = ref(`${import.meta.env.VITE_ENDURAIN_HOST}/api/v1/${props.user.photo_path}`); + const userPhotoUrl = ref(`${import.meta.env.VITE_ENDURAIN_HOST}/${props.user.photo_path}`); const alignTopValue = ref(props.alignTop); return {