forked from andika/membership-be
Donation Tracking\ Validation Rejection\ Subscription Data Export\ Admin Dashboard Logo\ Admin Navbar Reorganization
601 lines
15 KiB
Markdown
601 lines
15 KiB
Markdown
# LOAF Membership Platform - Backend API
|
||
|
||
FastAPI-based backend service for the LOAF (LGBT Organization and Friends) membership management platform.
|
||
|
||
## Table of Contents
|
||
|
||
- [Setup & Installation](#setup--installation)
|
||
- [Architecture & Code Structure](#architecture--code-structure)
|
||
- [API Documentation](#api-documentation)
|
||
- [Deployment Guide](#deployment-guide)
|
||
- [Troubleshooting](#troubleshooting)
|
||
|
||
---
|
||
|
||
## Setup & Installation
|
||
|
||
### Prerequisites
|
||
|
||
- **Python**: 3.9 or higher
|
||
- **PostgreSQL**: 13 or higher
|
||
- **Cloudflare R2**: Account and credentials (for file storage)
|
||
- **Stripe**: Account and API keys (for payments)
|
||
- **SMTP Server**: For sending emails (Gmail, SendGrid, etc.)
|
||
|
||
### 1. Clone Repository
|
||
|
||
```bash
|
||
cd backend
|
||
```
|
||
|
||
### 2. Create Virtual Environment
|
||
|
||
```bash
|
||
# Create virtual environment
|
||
python -m venv venv
|
||
|
||
# Activate virtual environment
|
||
# On macOS/Linux:
|
||
source venv/bin/activate
|
||
|
||
# On Windows:
|
||
venv\Scripts\activate
|
||
```
|
||
|
||
### 3. Install Dependencies
|
||
|
||
```bash
|
||
pip install -r requirements.txt
|
||
```
|
||
|
||
**Key Dependencies:**
|
||
- `fastapi==0.110.1` - Web framework
|
||
- `uvicorn==0.25.0` - ASGI server
|
||
- `sqlalchemy==2.0.44` - ORM for database
|
||
- `psycopg2-binary==2.9.10` - PostgreSQL adapter
|
||
- `python-jose[cryptography]==3.5.0` - JWT tokens
|
||
- `passlib[bcrypt]==1.7.4` - Password hashing
|
||
- `aiosmtplib==5.0.0` - Async email sending
|
||
- `boto3==1.35.95` - AWS SDK (for Cloudflare R2)
|
||
- `stripe==11.4.0` - Payment processing
|
||
- `python-multipart==0.0.18` - File uploads
|
||
|
||
### 4. Database Setup
|
||
|
||
#### Create PostgreSQL Database
|
||
|
||
```bash
|
||
# Connect to PostgreSQL
|
||
psql -U postgres
|
||
|
||
# Create database
|
||
CREATE DATABASE membership_db;
|
||
|
||
# Create user (optional)
|
||
CREATE USER membership_user WITH PASSWORD 'your_password';
|
||
GRANT ALL PRIVILEGES ON DATABASE membership_db TO membership_user;
|
||
|
||
# Exit psql
|
||
\q
|
||
```
|
||
|
||
#### Run Migrations
|
||
|
||
**⚠️ IMPORTANT:** For complete migration documentation, see **[MIGRATIONS.md](./MIGRATIONS.md)**
|
||
|
||
**For Fresh Database (New Deployment):**
|
||
|
||
```bash
|
||
# Run comprehensive initial schema (includes all base tables)
|
||
psql -U postgres -d membership_db -f migrations/000_initial_schema.sql
|
||
```
|
||
|
||
This single migration creates:
|
||
- All 10 ENUM types (userstatus, userrole, rsvpstatus, etc.)
|
||
- All 17 base tables (users, events, subscriptions, donations, RBAC, etc.)
|
||
- 30+ performance indexes
|
||
- Default storage_usage record
|
||
|
||
**For Existing Database (Incremental Updates):**
|
||
|
||
If you already have a database and need to apply specific updates, see the migration sequence in [MIGRATIONS.md](./MIGRATIONS.md).
|
||
|
||
**Next Steps After Migration:**
|
||
|
||
```bash
|
||
# 1. Seed permissions (59 permissions across 10 modules)
|
||
python seed_permissions_rbac.py
|
||
|
||
# 2. Create superadmin user (interactive)
|
||
python create_admin.py
|
||
```
|
||
|
||
### 5. Environment Configuration
|
||
|
||
Create `.env` file in the backend directory:
|
||
|
||
```bash
|
||
# Database
|
||
DATABASE_URL=postgresql://postgres:password@localhost:5432/membership_db
|
||
|
||
# JWT Configuration
|
||
JWT_SECRET=your-super-secret-key-change-in-production
|
||
JWT_ALGORITHM=HS256
|
||
ACCESS_TOKEN_EXPIRE_MINUTES=30
|
||
|
||
# SMTP Email Configuration
|
||
SMTP_HOST=smtp.gmail.com
|
||
SMTP_PORT=587
|
||
SMTP_USERNAME=your-email@gmail.com
|
||
SMTP_PASSWORD=your-app-password
|
||
SMTP_FROM_EMAIL=noreply@loaf.org
|
||
SMTP_FROM_NAME=LOAF Membership Platform
|
||
|
||
# Frontend URL (for email links and CORS)
|
||
FRONTEND_URL=http://localhost:3000
|
||
|
||
# Cloudflare R2 Storage
|
||
R2_ACCOUNT_ID=your-cloudflare-account-id
|
||
R2_ACCESS_KEY_ID=your-r2-access-key
|
||
R2_SECRET_ACCESS_KEY=your-r2-secret-key
|
||
R2_BUCKET_NAME=loaf-membership
|
||
R2_PUBLIC_URL=https://your-r2-bucket.r2.dev
|
||
|
||
# Stripe Payment Processing
|
||
STRIPE_SECRET_KEY=sk_test_your_stripe_secret_key
|
||
STRIPE_PUBLISHABLE_KEY=pk_test_your_stripe_publishable_key
|
||
STRIPE_WEBHOOK_SECRET=whsec_your_webhook_secret
|
||
|
||
# Server Configuration
|
||
PORT=8000
|
||
HOST=0.0.0.0
|
||
CORS_ORIGINS=http://localhost:3000,http://localhost:3001
|
||
|
||
# Optional: Sentry (error tracking)
|
||
SENTRY_DSN=your-sentry-dsn
|
||
```
|
||
|
||
**Important Notes:**
|
||
- **Gmail SMTP**: Use App Password, not your regular password. Enable 2FA and generate app-specific password at https://myaccount.google.com/apppasswords
|
||
- **JWT_SECRET**: Generate a strong random key: `openssl rand -hex 32`
|
||
- **Stripe Webhook Secret**: Get from Stripe Dashboard <20> Developers <20> Webhooks
|
||
|
||
### 6. Seed Initial Data
|
||
|
||
```bash
|
||
# Seed permissions (must be run after migrations)
|
||
python seed_permissions_rbac.py
|
||
|
||
# Create superadmin user (optional)
|
||
python create_superadmin.py
|
||
```
|
||
|
||
### 7. Start Development Server
|
||
|
||
```bash
|
||
# Start with auto-reload
|
||
uvicorn server:app --reload --host 0.0.0.0 --port 8000
|
||
|
||
# Or using the PORT from .env
|
||
uvicorn server:app --reload --host 0.0.0.0 --port $PORT
|
||
```
|
||
|
||
**Server will be available at:**
|
||
- API: http://localhost:8000
|
||
- Swagger UI: http://localhost:8000/docs
|
||
- ReDoc: http://localhost:8000/redoc
|
||
|
||
---
|
||
|
||
## Architecture & Code Structure
|
||
|
||
### Project Structure
|
||
|
||
```
|
||
backend/
|
||
|