4.1 KiB
Quick Agent Instructions for Houshan API
This repository is a FastAPI service with Tortoise ORM (Aerich) migrations, several AI integrations, and Docker-based developer workflows. Use these notes to be productive quickly.
-
Big picture: API server in src/main.py mounts routes from
src/routesand models fromsrc/models. DB uses Tortoise ORM configured in src/settings.py and src/config.py. Background jobs use APScheduler (timezone Asia/Tehran). Key AI integrations include Fal (FAL_KEY), Suno (SUNO_API_KEY), OpenAI/Anthropic, and Chroma (seerequirements.txt). -
Run / dev workflows:
- Local (fast): run with Uvicorn:
uvicorn src.main:app --reload --host 0.0.0.0 --port 8000(requires env from.api.env). - Docker: use the Makefile targets which wrap
docker compose:make up— build & start servicesmake logs— follow logsmake shell-app— open a shell inside theappcontainer
docker-compose.ymluses env file.api.envand exposes the API on port 8000 inside the container. See docker-compose.yml.
- Local (fast): run with Uvicorn:
-
Environment & config:
- Environment variables live in
.api.env(referenced bySettings.model_configin src/config.py). Always copy production secrets out of commits;serviceAccountKey.jsonis present and sensitive. - DB URL lives in
DATABASE_URLand is referenced fromsrc/settings.pyandpyproject.tomlvia Aerich config.
- Environment variables live in
-
Migrations:
- Aerich is configured in
pyproject.toml(tool.aerich.tortoise_orm = "settings.TORTOISE_ORM"). Typical commands (run with properDATABASE_URLin env):aerich init -t src.settings.TORTOISE_ORMaerich migrate --name "describe"aerich upgrade
- Migrations live under
migrations/models.
- Aerich is configured in
-
Code layout & conventions:
src/routes– routers are registered centrally byregister_routers(app)in src/main.py.src/services– contains pluggable service modules (storage, tool, chatbot, etc.). Prefer calling service functions rather than re-implementing integrations.src/tasks– background jobs and scheduled tasks (scheduled fromsrc/main.pyvia APScheduler).- Use async/await consistently; many IO layers (httpx, aioboto3, asyncpg, tortoise) are async-first.
-
Integration tips & patterns (project-specific):
- Fal video generation: endpoint implemented in src/main.py under
/public/generate-video. It expectsFAL_KEYand usesFAL_BASE_URL. Keep the key in.api.env. - Suno: there is a webhook
/suno/callbackthat writes toSunoTask(seesrc/main.py). When testing webhooks locally, route requests to the container or usengrok. - Storage:
src/services/storage.pyabstracts file storage (S3/compatible). Use it for large files instead of returning raw streams. - Database model lookups use
await Model.get_or_none(...)— follow that idiom to avoid exceptions.
- Fal video generation: endpoint implemented in src/main.py under
-
Testing & debugging:
- No automated test harness found. Use
uvicornlocally ormake up+make logsfor smoke tests. - For DB schema issues, run
aerich migrate/upgradeand inspectmigrations/models. - To inspect runtime logs or DB state in containers:
make logsandmake shell-app.
- No automated test harness found. Use
-
Where to change things (examples):
- Add a new API router: create file in
src/routes, export arouter, and ensureregister_routersimports it. - Add a scheduled job: add function in
src/tasks/*and register it insrc/main.pywithscheduler.add_job(..., CronTrigger(..., timezone=tehran_tz)).
- Add a new API router: create file in
-
Caveats & gotchas:
- The project is async-first; avoid blocking calls (do CPU-heavy work in separate workers).
- Several keys and external services (Fal, Suno, Chroma, Google, MailerSend) are used—stub or mock them when necessary.
- Aerich migrations may be sensitive to
TORTOISE_ORMpath; keeppyproject.tomlandsrc/settings.pyin sync.
If any section is unclear or you'd like more examples (e.g., how to add a route or run aerich locally on Windows), tell me which part to expand.