A modern FastAPI backend for CampHub with async database operations, JWT authentication, and PostgreSQL support.
- 🚀 FastAPI - Modern, fast web framework for building APIs
- 🔐 JWT Authentication - Secure token-based authentication
- 📊 PostgreSQL + AsyncPG - Async database operations
- 📋 SQLModel - Type-safe database models with Pydantic
- 📝 API Documentation - Auto-generated with Swagger UI
app/
├── core/
│ ├── config.py # Settings and configuration
│ ├── security.py # JWT and password hashing
│ └── deps.py # Dependencies (auth, db)
├── models/
│ ├── __init__.py # Database setup and exports
│ ├── user_model.py # User model
│ ├── faculty_model.py # Faculty model
│ └── role_model.py # Role model
├── routers/
│ └── endpoints/
│ ├── auth_route.py # Authentication endpoints
│ └── user_route.py # User management endpoints
├── schemas/
│ ├── user_schema.py # User request/response schemas
│ ├── faculty_schema.py # Faculty schemas
│ └── role_schema.py # Role schemas
└── main.py # FastAPI application entry point
- Python 3.12
- Docker and Docker Compose
- Git
| Role | Password | |
|---|---|---|
| Admin | admin@camphub.com | admin123 |
| Professor | prof.smith@camphub.com | prof123 |
| Student | student.doe@camphub.com | student123 |
-
Clone the repository
git clone https://github.com/krahyor/backend_camphub.git cd backend_camphub -
Create and activate virtual environment
# Create virtual environment python -m venv .venv # Activate virtual environment # Windows: venv/Scripts/activate # macOS/Linux: source venv/bin/activate
-
Install Poetry in the virtual environment
# Install Poetry using pip in the activated venv pip install poetry # Verify Poetry installation poetry --version
-
Install project dependencies with Poetry
# Install all dependencies from pyproject.toml poetry install -
Setup PostgreSQL Database with Docker Compose
# Start PostgreSQL and pgadmin container (database will be created automatically) docker-compose up -d # Verify database is running docker-compose ps
The Docker setup automatically creates:
- Database:
camphub_db - Username:
camphub_user - Password:
camphub_pass - Port:
5432
- Database:
-
Environment Configuration
- Copy
.env.exampleto.env(if exists) or create.envfile:
cp .env.example .env
- Edit
.envfile with your database credentials (for Docker setup):
SQLDB_URL=postgresql+asyncpg://postgres:postgres@localhost:5432/camphub SECRET_KEY=your-super-secret-key-change-in-production JWT_SECRET_KEY=your-jwt-secret-key-change-in-production JWT_ACCESS_TOKEN_EXPIRE_MINUTES=60 JWT_REFRESH_TOKEN_EXPIRE_MINUTES=10080 JWT_ALGORITHM=HS256
- Copy
-
Initialize Database
# Run database initialization (make sure venv is activated) poetry run python -c "import asyncio; from app.core.db import init_db; asyncio.run(init_db())" # Or Use poetry run python init_db.py
This FastAPI project doesn't require a traditional "build" process, but here are the preparation steps:
# Make sure venv is activated first
# Install development dependencies (if not already included)
poetry add --group dev pytest pytest-asyncio httpx black flake8
# Verify installation
poetry run python -c "from app.main import app; print('✅ App imports successfully')"# Make sure venv is activated first
# Install production server
poetry add gunicorn
# Export requirements.txt if needed (for Docker or other deployment)
poetry export -f requirements.txt --output requirements.txt
# Run database migrations/initialization
poetry run python -c "import asyncio; from app.core.db import init_db; asyncio.run(init_db())"# If you have Dockerfile
docker build -t camphub-backend .-
Start PostgreSQL with Docker Compose
# Start the database (if not already running) docker-compose up -d postgres # Check if database is running docker-compose ps
-
Start the development server
# Make sure virtual environment is activated first # Windows: venv\Scripts\activate # macOS/Linux: source venv/bin/activate # Run using FastAPI dev command (Recommended) poetry run fastapi dev app/main.py # Alternative: using uvicorn directly poetry run uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
-
Access the application
- API: http://localhost:8000
- Interactive API docs: http://localhost:8000/docs
- Alternative docs: http://localhost:8000/redoc
-
Using Gunicorn (Linux/macOS/WSL)
# Make sure venv is activated, then run with Poetry poetry run gunicorn app.main:app -w 4 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000 -
Using Uvicorn
# Make sure venv is activated, then run with Poetry poetry run uvicorn app.main:app --host 0.0.0.0 --port 8000 --workers 4
docker run -p 8000:8000 camphub-backendPOST /api/auth/signup- User registrationPOST /api/auth/signin- User login (email + password)
GET /api/user- Get current user profilePUT /api/user- Update user profile
GET /docs- Swagger UI documentationGET /redoc- ReDoc documentation
The application creates a demo user on first startup:
- Email: demo@mail.com
- Password: 123456
- Username: user_demo
| Variable | Description | Default |
|---|---|---|
SQLDB_URL |
PostgreSQL connection string | Required |
SECRET_KEY |
Application secret key | Required |
JWT_SECRET_KEY |
JWT signing key | Required |
JWT_ACCESS_TOKEN_EXPIRE_MINUTES |
Access token expiry | 60 |
JWT_REFRESH_TOKEN_EXPIRE_MINUTES |
Refresh token expiry | 10080 |
JWT_ALGORITHM |
JWT algorithm | HS256 |
- User: Student/Professor with email authentication
- Faculty: Academic faculties/departments
- Role: User roles (1=Professor, 2=Student)
# Make sure venv is activated first
poetry run pytest# Make sure venv is activated first
# Format code
poetry run black app/
# Lint code
poetry run flake8 app/# Make sure venv is activated first
# Add runtime dependency
poetry add package-name
# Add development dependency
poetry add --group dev package-name
# Update dependencies
poetry update# Activate venv (do this first)
# Windows:
venv\Scripts\activate
# macOS/Linux:
source venv/bin/activate
# Deactivate venv
deactivate
# Show Poetry environment info
poetry env info
# Show installed packages
poetry show-
Database Connection Error
- Check PostgreSQL is running
- Verify credentials in
.env - Ensure database exists
-
Import Errors
- Activate venv first:
venv\Scripts\activate(Windows) orsource venv/bin/activate(macOS/Linux) - Install missing dependencies:
poetry install
- Activate venv first:
-
Permission Errors
- Check database user permissions
- Verify file permissions
-
Poetry Issues
- Make sure venv is activated first
- Update Poetry:
pip install --upgrade poetry(in activated venv) - Clear cache:
poetry cache clear pypi --all - Reinstall dependencies:
poetry install --no-cache
# Make sure venv is activated first, then view application logs
poetry run uvicorn app.main:app --log-level debug