Coverage for src/kwai/frontend/apps/_portal.py: 0%

20 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2024-01-01 00:00 +0000

1"""Module that defines the routes for the portal frontend application.""" 

2 

3from pathlib import Path 

4from typing import Annotated 

5 

6from fastapi import APIRouter, Depends, Request 

7from fastapi.templating import Jinja2Templates 

8 

9from kwai.api.dependencies import create_templates 

10from kwai.frontend.dependencies import ViteDependency 

11from kwai.frontend.etag_file_response import EtagFileResponse 

12from kwai.frontend.vite import Vite 

13 

14 

15APP_NAME = "portal" 

16 

17router = APIRouter(prefix=f"/{APP_NAME}") 

18 

19_portal_vite_dependency = ViteDependency(APP_NAME) 

20 

21 

22@router.get("/{path:path}", name=APP_NAME) 

23async def get_app( 

24 path: Path, 

25 request: Request, 

26 templates: Annotated[Jinja2Templates, Depends(create_templates)], 

27 vite: Annotated[Vite, Depends(_portal_vite_dependency)], 

28): 

29 asset_file_path = vite.get_asset_path(path) 

30 if asset_file_path is not None: 

31 return EtagFileResponse(asset_file_path) 

32 

33 url = request.url_for(APP_NAME, path="") 

34 if "x-forwarded-proto" in request.headers: 

35 url = url.replace(scheme=request.headers["x-forwarded-proto"]) 

36 

37 return templates.TemplateResponse( 

38 request, 

39 name="index.jinja2", 

40 context={ 

41 "application": { 

42 "name": APP_NAME, 

43 "url": str(url), 

44 }, 

45 "vite": vite, 

46 }, 

47 )