apps.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import logging
  2. from pathlib import Path
  3. from typing import Optional
  4. from pydantic import BaseModel
  5. logger = logging.getLogger("fastapi_cli")
  6. class AppConfig(BaseModel):
  7. app_id: str
  8. team_id: str
  9. def get_app_config(path_to_deploy: Path) -> Optional[AppConfig]:
  10. config_path = path_to_deploy / ".fastapicloud/cloud.json"
  11. logger.debug("Looking for app config at: %s", config_path)
  12. if not config_path.exists():
  13. logger.debug("App config file doesn't exist")
  14. return None
  15. logger.debug("App config loaded successfully")
  16. return AppConfig.model_validate_json(config_path.read_text(encoding="utf-8"))
  17. README = """
  18. > Why do I have a folder named ".fastapicloud" in my project? 🤔
  19. The ".fastapicloud" folder is created when you link a directory to a FastAPI Cloud project.
  20. > What does the "cloud.json" file contain?
  21. The "cloud.json" file contains:
  22. - The ID of the FastAPI app that you linked ("app_id")
  23. - The ID of the team your FastAPI Cloud project is owned by ("team_id")
  24. > Should I commit the ".fastapicloud" folder?
  25. No, you should not commit the ".fastapicloud" folder to your version control system.
  26. That's why there's a ".gitignore" file in this folder.
  27. """
  28. def write_app_config(path_to_deploy: Path, app_config: AppConfig) -> None:
  29. config_path = path_to_deploy / ".fastapicloud/cloud.json"
  30. readme_path = path_to_deploy / ".fastapicloud/README.md"
  31. gitignore_path = path_to_deploy / ".fastapicloud/.gitignore"
  32. logger.debug("Writing app config to: %s", config_path)
  33. logger.debug("App config data: %s", app_config)
  34. config_path.parent.mkdir(parents=True, exist_ok=True)
  35. config_path.write_text(
  36. app_config.model_dump_json(),
  37. encoding="utf-8",
  38. )
  39. readme_path.write_text(README, encoding="utf-8")
  40. gitignore_path.write_text("*")
  41. logger.debug("App config files written successfully")