Coverage for src/kwai/frontend/dependencies.py: 0%

20 statements  

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

1"""Module for defining dependencies for the frontend applications.""" 

2 

3from pathlib import Path 

4from typing import Annotated 

5 

6from fastapi import Depends, HTTPException, status 

7 

8from kwai.core.settings import Settings, get_settings 

9from kwai.frontend.vite import DevelopmentVite, ProductionVite, Vite 

10 

11 

12class ViteDependency: 

13 """Vite is a dependency.""" 

14 

15 def __init__(self, application_name: str): 

16 self._application_name = application_name 

17 

18 def __call__(self, settings: Annotated[Settings, Depends(get_settings)]) -> Vite: 

19 """Create a Vite environment for this application.""" 

20 app_setting = next( 

21 ( 

22 setting 

23 for setting in settings.frontend.apps 

24 if setting.name == self._application_name 

25 ), 

26 None, 

27 ) 

28 if app_setting is None: 

29 raise HTTPException( 

30 status_code=status.HTTP_400_BAD_REQUEST, 

31 detail=f"Application {self._application_name} is not configured.", 

32 ) 

33 

34 if settings.frontend.test: 

35 if app_setting.vite_server is None: 

36 raise HTTPException( 

37 status_code=status.HTTP_400_BAD_REQUEST, 

38 detail=f"Setting 'vite_server' not set for application {self._application_name}", 

39 ) 

40 return DevelopmentVite(app_setting.vite_server) 

41 

42 manifest_path = ( 

43 Path(settings.frontend.path) 

44 / "apps" 

45 / self._application_name 

46 / "dist" 

47 / ".vite" 

48 / "manifest.json" 

49 ) 

50 if not manifest_path.exists(): 

51 raise HTTPException( 

52 status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, 

53 detail=f"Manifest file {manifest_path} not found", 

54 ) 

55 

56 return ProductionVite( 

57 manifest_path, 

58 Path(settings.frontend.path) / "apps" / self._application_name, 

59 )