# Backend (FastAPI) This service exposes the HTTP + WebSocket APIs and database access required by the weighbridge system. It targets **Python 3.11+**. ## Quickstart ```bash cd /Users/wuzilong/work/00code/simple-dibang-xiaoshou/backend python -m venv .venv source .venv/bin/activate # Windows: .venv\Scripts\activate pip install -U pip uv uv pip install -e ".[dev]" uv run fastapi dev app/main.py # auto reload server ``` ## Layout - `app/main.py` – FastAPI application entrypoint. - `app/core/config.py` – environment-driven settings. - `app/db/session.py` – SQLModel engine/session helpers. - `app/api/routes` – routers grouped by bounded context. - `app/services` – business logic (batch number generation, printing jobs, etc.). - `tests/` – pytest suites (API, domain, migrations). ## Environment Copy `.env.example` to `.env` and adjust values for your environment. ### Required Variables | Variable | Description | Example | | --- | --- | --- | | `DATABASE_URL` | MySQL database connection string | `mysql+asyncmy://user:pass@localhost:3306/dibang?charset=utf8mb4` | | `JWT_SECRET` | HS256 secret for signing JWTs (must be strong in production) | `your-strong-secret-key-here` | ### Optional Variables | Variable | Description | Default | | --- | --- | --- | | `ENV` | Environment: `dev`, `test`, or `prod` | `dev` | | `ACCESS_TOKEN_EXPIRES_MINUTES` | Access token expiration time (minutes) | `60` | | `REFRESH_TOKEN_EXPIRES_DAYS` | Refresh token expiration time (days) | `14` | | `HOST` | Server bind address | `0.0.0.0` | | `PORT` | Server port | `8000` | | `CORS_ORIGINS` | Allowed CORS origins (comma-separated, use `*` for all) | `*` | | `CORS_ALLOW_CREDENTIALS` | Allow credentials in CORS | `true` | | `CORS_ALLOW_METHODS` | Allowed HTTP methods (comma-separated, use `*` for all) | `*` | | `CORS_ALLOW_HEADERS` | Allowed HTTP headers (comma-separated, use `*` for all) | `*` | | `DB_POOL_SIZE` | Database connection pool size | `10` | | `DB_MAX_OVERFLOW` | Max overflow connections | `20` | | `DB_POOL_TIMEOUT` | Connection pool timeout (seconds) | `30` | | `DB_POOL_RECYCLE` | Connection recycle time (seconds) | `3600` | | `DB_POOL_PRE_PING` | Enable connection pre-ping | `true` | See `env.example` for detailed comments and examples. ## Tooling - **Lint/format**: `ruff check app`, `ruff format app`. - **Type check**: `mypy app`. - **Tests**: `pytest`. - **Migrations**: `alembic revision --autogenerate -m ""`. --- The scaffold follows the architecture plan in `frontend_backend_framework_plan.md`, making it easy to plug into the new PC and mobile frontends.