# BeFree Deployment Guide This document describes how to deploy the BeFree Bank Statement Processor backend (Docker, Railway, Render, Fly.io) and frontend (Vercel) to staging and production environments. --- ## 1. Backend Deployment The backend is built as a containerized FastAPI application using the provided [Dockerfile](file:///home/luv/Desktop/new/Dockerfile). ### 1.1 Docker Container Build ```bash # Build the production Docker image docker build -t befree-backend:latest . # Run the container locally docker run -p 7860:7860 --env-file .env befree-backend:latest ``` ### 1.2 Environment Variables Configuration The container configuration is managed through environment variables. Configure these in your host provider dashboard (Railway, Render, Fly.io, etc.): | Variable Name | Required | Default | Description | | :--- | :--- | :--- | :--- | | `GOOGLE_API_KEY` | Yes | None | Google Gemini API key for fallback classification. | | `DATABASE_URL` | Yes | None | PostgreSQL connection string (must use sync scheme e.g. `postgresql://...`). | | `JWT_SECRET_KEY` | Yes | None | Hexadecimal key (min 32 chars) for JWT tokens encryption. | | `JWT_ALGORITHM` | No | `HS256` | JWT signing algorithm. | | `SEED_SECRET` | Yes | None | Authorization header value for seeding initial users. | | `PORT` | No | `7860` | Port on which the FastAPI application listens. | | `ENVIRONMENT` | No | `development` | Set to `production` to disable interactive OpenAPI Swagger `/docs`. | ### 1.3 Database Setup BeFree supports two database tiers depending on the environment: - **Development & UAT**: NeonDB (Serverless PostgreSQL free tier works perfectly). Set the connection string with SSL required: `postgresql://user:pass@ep-xxx.neon.tech/neondb?sslmode=require`. - **Production**: Azure Database for PostgreSQL (Flexible Server). Set the connection string: `postgresql://user:pass@server.postgres.database.azure.com/db?sslmode=require`. ### 1.4 Running Migrations Database migrations are handled by `auth/migrations.py`. This script adds required columns to the database tables using safe `IF NOT EXISTS` clauses: ```bash # Run migrations inside the container python auth/migrations.py ``` This is executed automatically during the container startup sequence or can be run manually in your CI/CD pipeline post-deploy. ### 1.5 Seeding Initial Users To populate the database with initial employee accounts: 1. Make a POST request to `/api/v1/auth/seed-users` with the header `X-Seed-Secret` matching the `SEED_SECRET` environment variable. 2. The server will seed the default logins (`Befree_employee1`, `Befree_employee2`, `Befree_employee3`) with default roles and active flags. 3. Users can also be seeded directly on the server host console by running: ```bash python auth/seed_users.py ``` ### 1.6 Port & Health Checks - **Port Mapping**: The backend reads `$PORT` set by container hosts (e.g. Render, Railway) and defaults to `7860` (HuggingFace compatible) or `8000` (local development). - **Health Check Endpoint**: `/health` handles automated status monitoring. The health check is configured in the Dockerfile: ```bash HEALTHCHECK --interval=30s --timeout=5s --retries=3 \ CMD python -c "import urllib.request, os; port = os.environ.get('PORT', '7860'); urllib.request.urlopen(f'http://localhost:{port}/health')" || exit 1 ``` --- ## 2. Frontend Deployment (Vercel) The frontend is a standalone Next.js 15 Single Page Application located inside the `befree-frontend/` directory. ### 2.1 Deployment to Vercel Vercel is the recommended hosting platform for the frontend. - **Vercel Config**: Configured via [befree-frontend/vercel.json](file:///home/luv/Desktop/new/befree-frontend/vercel.json) to handle rewrites and redirect routes to Next.js routes. - **Required Environment Variable**: - `NEXT_PUBLIC_API_URL`: Set this in the Vercel dashboard to your deployed backend URL (e.g., `https://befree-backend.railway.app`). This variable is injected at build time. ### 2.2 Compilation and Execution To manually build the frontend: ```bash cd befree-frontend npm install npm run build npm start ``` --- ## 3. Local Development Quick-Start For rapid local testing and development, you can spin up the full stack using the quick start script: ### 3.1 Prerequisite Sibling Repositories The system utilizes internal packages declared as editable dependencies in `requirements.txt`. For local setup, ensure you have the following sibling directories cloned and available relative to this project root: - `bank-extractor/` - `bank-hitl/` ### 3.2 Executing local stack Ensure you have Python 3.11+ installed. Run: ```bash # Set up venv, installs all requirements and launches servers ./run.sh ``` This spins up the backend API on `http://localhost:8000` and the frontend Next.js dev server on `http://localhost:3000`.