4.7 KiB
Backend Runtime Rules
This document defines runtime configuration requirements for the generated backend. Generators must produce a project that runs without errors when these rules are followed.
Environment Variables
The backend must have a .env file at the project root (e.g. server/.env or backend package root). This file must not be committed with real credentials; provide an example instead (e.g. .env.example).
Required variables
| Variable | Required | Description |
|---|---|---|
| PORT | Yes | Backend listen port |
| DATABASE_URL | Yes | PostgreSQL connection string for Prisma |
| CORS_ALLOWED_ORIGINS | Yes | Comma-separated SPA origins allowed to call the API |
| KEYCLOAK_ISSUER_URL | Yes | Keycloak issuer URL used for JWT verification |
| KEYCLOAK_AUDIENCE | Yes | Backend audience/client expected in access tokens |
| KEYCLOAK_JWKS_URL | No | Optional explicit JWKS URL |
Example
PORT=3000
DATABASE_URL="postgresql://postgres:postgres@localhost:5432/toir"
CORS_ALLOWED_ORIGINS="http://localhost:5173,https://toir-frontend.greact.ru"
KEYCLOAK_ISSUER_URL="https://sso.greact.ru/realms/toir"
KEYCLOAK_AUDIENCE="toir-backend"
# KEYCLOAK_JWKS_URL="https://sso.greact.ru/realms/toir/protocol/openid-connect/certs"
Generation requirement
When generating the backend, always create:
.env.example— with placeholder DATABASE_URL and instructions..env— with local development example values so the app can start.
If the generator does not create .env, the first run will fail with:
Environment variable not found: DATABASE_URL
Fail-fast requirement
The generated backend must validate required runtime and auth variables at startup.
Rules:
- Missing required auth variables must stop startup immediately.
- The generated backend must not silently fall back to production auth settings in code.
- Auth env validation must be implemented explicitly in generated config/bootstrap code.
Authentication Runtime Rules
The generated backend must verify JWTs against Keycloak using:
- issuer
- audience
- JWKS
JWKS resolution priority must be exactly:
- explicit
KEYCLOAK_JWKS_URL - OIDC discovery
${issuer}/protocol/openid-connect/certs
The generated backend must extract authorization roles only from realm_access.roles.
Prisma Client Generation
After the Prisma schema is generated or modified, the Prisma client must be generated.
Command
npx prisma generate
Lifecycle rule
Add to generated package.json so that prisma generate runs after every npm install:
{
"scripts": {
"postinstall": "prisma generate"
}
}
This ensures that after cloning or installing dependencies, the Prisma client is available without a manual step.
Note: Path may need to be adjusted if Prisma schema lives in a subfolder (e.g. prisma generate is typically run from the package root where prisma/schema.prisma exists).
Database Migration
The generation process must document and support database migration.
Command
npx prisma migrate dev
Run after:
- Prisma schema has been generated or updated.
npx prisma generatehas been run.
Generation requirement
- Include migration in the backend generation pipeline (see
generation/backend-generation.md). - Document in README or post-generation validation that the user must run
npx prisma migrate devbefore first run (or provide a setup script that runs it).
CORS Rules
The generated backend must support the SPA -> API bearer-token model explicitly.
Rules:
- Use
CORS_ALLOWED_ORIGINSas the allowed origin list. - Allow at minimum these example origins in
.env.example:http://localhost:5173https://toir-frontend.greact.ru
- Allow
Authorizationand JSON content headers. - Expose
Content-Rangebecause React Admin depends on it. - Do not enable credentials by default.
Summary
| Requirement | Action |
|---|---|
| Runtime env | Create .env and .env.example with DB, auth, and CORS variables |
| Fail-fast config | Validate required auth/runtime variables at startup |
| Prisma client | Run npx prisma generate; add postinstall script |
| Database schema | Document/run npx prisma migrate dev after schema generation |
| JWT verification | Use issuer + audience + JWKS with explicit resolution priority |
| Role extraction | Use only realm_access.roles |
| CORS | Support SPA -> API bearer flow and expose Content-Range |