add .env vars

pull/25/head
ahmed.mujtaba 2026-08-24 21:05:53 +05:00
parent f1597792ed
commit e114aeb0cf
5 changed files with 177 additions and 84 deletions

5
.env Normal file
View File

@ -0,0 +1,5 @@
# Compose pointer only — NO secrets here.
# Makes `docker compose up -d --build` read ${FRONTEND_PORT}, ${BACKEND_PORT}, …
# from backend/.env without typing --env-file every time.
# All credentials and app config live in backend/.env.
COMPOSE_ENV_FILES=./backend/.env

7
.gitignore vendored
View File

@ -34,10 +34,13 @@ __pycache__/
venv/ venv/
env/ env/
# Environment / secrets # Environment / secrets — app secrets only in backend/.env (never commit it).
.env # Root .env is a Compose pointer (COMPOSE_ENV_FILES) with no secrets; keep it.
backend/.env
.env.* .env.*
!.env
!.env.example !.env.example
!backend/.env.example
!frontend/.env.development !frontend/.env.development
!frontend/.env.production !frontend/.env.production

117
DOCKER.md
View File

@ -1,10 +1,25 @@
# Docker # Docker
```bash
docker compose up -d --build
```
Root `.env` is a **pointer only** (`COMPOSE_ENV_FILES=./backend/.env`) so Compose
interpolates `${FRONTEND_PORT}`, `${BACKEND_PORT}`, … from **`backend/.env`**.
All secrets and app config live in `backend/.env` (also injected into containers
via `env_file`).
## How the browser reaches the API ## How the browser reaches the API
The SPA is on **http://127.0.0.1:5173** (nginx → `backend-api` on the Compose | Surface | URL |
network). The API is also published on **http://127.0.0.1:8000** for host tools |---|---|
and `npm run dev` (`VITE_API_BASE=http://127.0.0.1:8000`). | SPA | http://127.0.0.1:5173 |
| API (host) | http://127.0.0.1:8000 |
nginx on `:5173` also proxies API paths to `backend-api` (same-origin when
`VITE_API_BASE` is empty).
In `backend/.env`:
```env ```env
FRONTEND_PORT=5173 FRONTEND_PORT=5173
@ -12,17 +27,8 @@ BACKEND_PORT=8000
FRONTEND_URL=http://127.0.0.1:5173 FRONTEND_URL=http://127.0.0.1:5173
``` ```
Sole env file: **`backend/.env`** (no repo-root `.env`). Always pass it for
Compose variable substitution:
```bash
docker compose --env-file ./backend/.env up -d --build
```
## Local (host Postgres) ## Local (host Postgres)
In `backend/.env`:
```env ```env
PROD_ENV=false PROD_ENV=false
DB_USERNAME=... DB_USERNAME=...
@ -31,115 +37,90 @@ DB_HOST=localhost
DB_PORT=5432 DB_PORT=5432
DB_NAME=hrms DB_NAME=hrms
DB_SSLMODE= DB_SSLMODE=
FRONTEND_PORT=8080 FRONTEND_PORT=5173
BACKEND_PORT=8000
FRONTEND_URL=http://127.0.0.1:5173
``` ```
Containers set `IN_DOCKER=1`. With `PROD_ENV=false`, `db_setup` rewrites Containers set `IN_DOCKER=1`. With `PROD_ENV=false`, `db_setup` rewrites
`localhost` / `127.0.0.1``host.docker.internal` for the connection URL only `localhost` / `127.0.0.1``host.docker.internal` for the connection URL only
(SSL off unless `DB_SSLMODE` is set). Host Postgres must accept Docker-bridge (SSL off unless `DB_SSLMODE` is set).
clients (`listen_addresses`, `pg_hba`).
```bash ```bash
cp backend/.env.example backend/.env # set JWT, OpenAI, DB_*, PROD_ENV=false cp backend/.env.example backend/.env # set JWT, OpenAI, DB_*, PROD_ENV=false
docker compose --env-file ./backend/.env up -d --build docker compose up -d --build
``` ```
| Service | Host access | | Service | Host access |
|---|---| |---|---|
| `frontend` | `${FRONTEND_PORT:-80}` (all interfaces) | | `frontend` | `${FRONTEND_PORT:-5173}` |
| `backend-api` | Compose network only (`backend-api:8000`); nginx proxies | | `backend-api` | `${BACKEND_PORT:-8000}` |
| `ats-engine` | Compose network only (`ats-engine:8100`) | | `ats-engine` / `redis` | Compose network (optional host-ports overlay) |
| `redis` | Compose network only (`redis:6379`) |
| `postgres` | not started (optional `--profile postgres`) | | `postgres` | not started (optional `--profile postgres`) |
Optional loopback publishes for host tools (curl / redis-cli / Postman): Optional loopback publishes for ATS / Redis:
```bash ```bash
docker compose --env-file ./backend/.env -f docker-compose.yml -f docker-compose.host-ports.yml up -d docker compose -f docker-compose.yml -f docker-compose.host-ports.yml up -d
``` ```
If bind fails on Windows because Cursor/VS Code still holds `:80` / `:8100` /
`:6379` after a previous run, clear **Ports** in the IDE or set free values in
`backend/.env` (`FRONTEND_PORT`, and with the overlay `ATS_PORT` / `REDIS_PORT` /
`BACKEND_PORT`).
Optional live-reload / bind mounts: Optional live-reload / bind mounts:
```bash ```bash
docker compose --env-file ./backend/.env -f docker-compose.yml -f docker-compose.dev.yml up -d --build docker compose -f docker-compose.yml -f docker-compose.dev.yml up -d --build
``` ```
Optional Compose Postgres (empty volume — not host data): Optional Compose Postgres (empty volume — not host data):
```bash ```bash
docker compose --env-file ./backend/.env --profile postgres up -d postgres docker compose --profile postgres up -d postgres
# set DB_HOST=postgres in backend/.env, then recreate backend services # set DB_HOST=postgres in backend/.env, then recreate backend services
``` ```
## Production (RDS) ## Production (RDS)
In `backend/.env`, set `PROD_ENV=true` and point plain `DB_*` at RDS (no In `backend/.env`, set `PROD_ENV=true` and point plain `DB_*` at RDS. Blank
prefixed credential sets). Blank `DB_SSLMODE` → SSL `require`. Host is never `DB_SSLMODE` → SSL `require` (or set `DB_SSLMODE=require` explicitly).
rewritten.
```bash ```bash
cp backend/.env.example backend/.env docker compose up -d --build
# Edit backend/.env: PROD_ENV=true, DB_* = RDS, JWT_SECRET_KEY, FRONTEND_PORT=80, … docker compose ps
docker compose --env-file ./backend/.env up -d --build
docker compose --env-file ./backend/.env ps
``` ```
Browser → `http://<host>/` → nginx (same-origin) → `backend-api:8000`. First boot against RDS can take a few minutes while Alembic applies drift; the
CV files live in the `attachments-data` named volume (shared by API + workers). API healthcheck `start_period` is 180s so Compose does not mark it unhealthy too early.
### Schema / migrations (automatic) ### Schema / migrations (automatic)
On every `backend-api` start: On every `backend-api` start:
1. Fresh empty Postgres → create all tables from models and stamp a marker. 1. Fresh empty Postgres → create all tables from models and stamp a marker.
2. Otherwise → `alembic upgrade head` if any revision files exist in the image 2. Otherwise → `alembic upgrade head` if any revision files exist in the image.
(they normally do not — versions stay gitignored and are excluded from builds).
3. If `DB_AUTOGENERATE=true` → detect ORM drift and apply DDL **in-memory**. 3. If `DB_AUTOGENERATE=true` → detect ORM drift and apply DDL **in-memory**.
4. Apply any pending `backend/migrations/manual/*.sql` (seed/RBAC batches only). 4. Apply any pending `backend/migrations/manual/*.sql`.
Toggle in `backend/.env`: `DB_AUTO_MIGRATE` / `DB_AUTOGENERATE` (default `true`). Toggle in `backend/.env`: `DB_AUTO_MIGRATE` / `DB_AUTOGENERATE`.
### Verify ### Verify
```bash ```bash
docker compose --env-file ./backend/.env config docker compose config
curl -sf http://127.0.0.1:${FRONTEND_PORT:-8080}/health curl -sf http://127.0.0.1:5173/health
curl -sf -o /dev/null -w "%{http_code}\n" http://127.0.0.1:${FRONTEND_PORT:-8080}/ curl -sf http://127.0.0.1:8000/health
docker compose --env-file ./backend/.env logs -f backend-api docker compose logs -f backend-api
``` ```
### Secrets ### Secrets
- Never bake `.env` into images (`.dockerignore` already excludes them). - Never bake `backend/.env` into images.
- Require a strong `DB_PASSWORD` and `JWT_SECRET_KEY` before any real deploy. - Root `.env` must stay a pointer (`COMPOSE_ENV_FILES`) — no passwords there.
- Only `backend/.env` holds app + Compose substitution values.
- Do not put `DB_HOST` under Compose `environment:` (empty override blanks RDS). - Do not put `DB_HOST` under Compose `environment:` (empty override blanks RDS).
### TLS
This stack serves HTTP on the frontend port. Terminate TLS at a reverse proxy or
cloud load balancer in front of that port.
## Data migration
The optional Compose Postgres volume starts empty. To move an existing host database:
```bash
pg_dump -Fc hrms > hrms.dump
pg_restore -h 127.0.0.1 -p 5433 -U postgres -d hrms --clean --if-exists hrms.dump
```
## Useful commands ## Useful commands
```bash ```bash
docker compose --env-file ./backend/.env logs -f backend-api docker compose logs -f backend-api
docker compose --env-file ./backend/.env logs -f taskiq-worker docker compose restart backend-api
docker compose --env-file ./backend/.env restart backend-api docker compose down
docker compose --env-file ./backend/.env down # keep volumes docker compose down -v
docker compose --env-file ./backend/.env down -v # wipe volumes
``` ```

105
backend/.env Normal file
View File

@ -0,0 +1,105 @@
# true → RDS over SSL (asyncpg). false → local Postgres over asyncpg (no SSH).
PROD_ENV=false
DB_USERNAME=postgres
DB_PASSWORD=ambaig123
DB_HOST=localhost
DB_PORT=5432
DB_NAME=dev_hrms
# Blank: require when PROD_ENV=true, off when local. Override only if needed.
DB_SSLMODE=
# DB_USERNAME=utopiaaiadmin
# DB_PASSWORD=cOIYBbjeRpb5rZigZBYX
# DB_HOST=utopia-ai-hr-ats-portal-db.co658idowpql.us-east-2.rds.amazonaws.com
# DB_PORT=5432
# DB_NAME=dev_hrms
# DB_SSLMODE=require
EMAIL_URL=http://172.16.204.191:5000
EMAIL_API_TOKEN=vn1a9sx6tI3L5LVXgblOiVpxn3gAiJXTVYvO5GICUNo
EMAIL_SYNC_FOLDER=inbox
EMAIL_SYNC_SINCE=
EMAIL_SYNC_CRON=* * * * *
JWT_SECRET_KEY=8B6lep0TuSrGp__2SFDCqKGyEluuUWV7SxvobhG5ECQYvN4o4P0OakRW0VWmiflZ6hgNK7RGdOVmi4H8LnSLwA
JWT_ALGORITHM=HS256
JWT_ACCESS_TOKEN_EXPIRE_MINUTES=30
JWT_REFRESH_TOKEN_EXPIRE_DAYS=7
TEAMS_MAIL_API_URL=http://ec2-13-222-174-68.compute-1.amazonaws.com:8000/mail/send
TEAMS_API_TOKEN=wt139Dg1bm0lXoYszzwCcMmkjQrkuP
RESET_CODE_TTL_SECONDS=60
RESET_CODE_RESEND_SECONDS=30
RESET_CODE_MAX_ATTEMPTS=5
JWT_RESET_TOKEN_EXPIRE_MINUTES=10
# Vite / email links. Docker SPA publishes on FRONTEND_PORT (5173).
FRONTEND_URL=http://127.0.0.1:5173
CONFIRM_EMAIL_PATH=/auth/confirm-email
CONFIRM_TOKEN_TTL_SECONDS=86400
CONFIRM_TOKEN_RESEND_SECONDS=60
BUFFER_API=1aF1d6NQ3qTsjtfA_mdNwqfhK6myTqXK0vwSySiPRrK
BUFFER_API_URL=https://api.buffer.com
BUFFER_CHANNEL_ID=6a73328399afb4434907444f
CLIENT_ID=6a7331dc2d90e07071891bfc
# OpenAI API Key
OPENAI_API_KEY=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
OPENAI_BASE_URL=https://api.openai.com/v1
OPENAI_MAX_OUTPUT_TOKENS=32768
OPENAI_MODEL=gpt-5.4-mini
OPENAI_TIMEOUT=60
OPENAI_MAX_RETRIES=3
OPENAI_CONNECT_RETRIES=3
OPENAI_TEMPERATURE=0.3
REDIS_URL=redis://redis:6379/0
TASKIQ_QUEUE_NAME=inbox
TASKIQ_MAX_RETRIES=3
TASKIQ_RETRY_DELAY=5
TASKIQ_MAX_DELAY=120
TASKIQ_DLQ_STREAM=taskiq:dlq
TASKIQ_IDLE_TIMEOUT_MS=600000
APP_VERSION=dev
# Google Sheet
SPREADSHEET_NAME=Product Hiring
SPREADSHEET_ID=1yjRueJjA1NbW8rS-KsSCJxxnkqh6cPiJjwcmXbTLusk
SPREADSHEET_URL=https://docs.google.com/spreadsheets/d/1yjRueJjA1NbW8rS-KsSCJxxnkqh6cPiJjwcmXbTLusk/edit
# Google Cloud project
GOOGLE_CLOUD_PROJECT=hrms-ats-portal
GOOGLE_ACCOUNT=ahmed.mujtaba@utopiabrands.com
# OAuth client (Desktop)
GOOGLE_CLIENT_ID=679334897177-ufal3rogbg8cgm3qcren6pv20phqd7nl.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=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
GOOGLE_OAUTH_CLIENT_ID_FILE=credentials/client_secret.json
# Application Default Credentials (used by google-auth)
GOOGLE_APPLICATION_CREDENTIALS=credentials/application_default_credentials.json
GOOGLE_REFRESH_TOKEN=1//038IfgCu3D42fCgYIARAAGAMSNwF-L9IrYAZ_DJUqwC9ETwLtH23D46j61gWMwFQjRWPklFZIiLmv7Q3-TOgcNxTrjO30jgkeYOo
GOOGLE_CREDENTIALS_TYPE=authorized_user
BACKEND_URL=http://backend-api:8000
DB_AUTOGENERATE=false
# ---------------------------------------------------------------------------
DB_AUTO_MIGRATE=false
# Compose host ports (docker compose --env-file ./backend/.env …).
FRONTEND_PORT=5173
BACKEND_PORT=8000
ATS_PORT=8100
REDIS_PORT=6379
POSTGRES_PORT=5433
UVICORN_WORKERS=2
# Empty = same-origin; frontend nginx proxies API paths to backend-api.
VITE_API_BASE=

View File

@ -1,26 +1,24 @@
# HR-ATS-Portal — single Compose file for local and production. # HR-ATS-Portal — single Compose file for local and production.
# #
# Sole env file: backend/.env (no repo-root .env). Pass it for Compose # docker compose up -d --build
# variable substitution (${FRONTEND_PORT}, ${DB_*}, …):
# #
# docker compose --env-file ./backend/.env up -d --build # Root `.env` only sets COMPOSE_ENV_FILES=./backend/.env so ${FRONTEND_PORT},
# docker compose --env-file ./backend/.env ps # ${BACKEND_PORT}, ${UVICORN_WORKERS}, … interpolate from backend/.env.
# docker compose --env-file ./backend/.env logs -f backend-api # Secrets and app config live solely in backend/.env (loaded into containers
# via env_file as well).
# #
# Local (PROD_ENV=false, DB_HOST=localhost in backend/.env): # Local (PROD_ENV=false, DB_HOST=localhost in backend/.env):
# Containers reach host Postgres via host.docker.internal (db_setup rewrite # Containers reach host Postgres via host.docker.internal (db_setup rewrite
# when IN_DOCKER=1). API / ATS / Redis stay on the Compose network by default # when IN_DOCKER=1). Frontend :5173, API :8000 by default.
# (avoids IDE/Cursor stale port-forwards fighting Docker on Windows). Opt in:
# docker compose --env-file ./backend/.env -f docker-compose.yml -f docker-compose.host-ports.yml up -d
# #
# Prod (PROD_ENV=true, DB_* = RDS in backend/.env — edit manually): # Prod (PROD_ENV=true, DB_* = RDS in backend/.env — edit manually):
# Same command. No host rewrite; SSL require when DB_SSLMODE is blank. # Same command. No host rewrite; SSL require when DB_SSLMODE is blank.
# #
# Optional Compose Postgres (empty volume, not host/RDS data): # Optional Compose Postgres:
# docker compose --env-file ./backend/.env --profile postgres up -d postgres # docker compose --profile postgres up -d postgres
# #
# Optional live-reload / bind mounts (not required day-to-day): # Optional live-reload / bind mounts:
# docker compose --env-file ./backend/.env -f docker-compose.yml -f docker-compose.dev.yml up -d --build # docker compose -f docker-compose.yml -f docker-compose.dev.yml up -d --build
# #
# See DOCKER.md for env checklist and verification. # See DOCKER.md for env checklist and verification.
@ -164,8 +162,9 @@ services:
] ]
interval: 15s interval: 15s
timeout: 5s timeout: 5s
retries: 5 retries: 12
start_period: 40s # RDS / first-boot Alembic can run well past 40s before /health answers.
start_period: 180s
# --- bulk ATS scoring engine (standalone service form of app/) -------------------- # --- bulk ATS scoring engine (standalone service form of app/) --------------------
ats-engine: ats-engine: