Enterprise-grade FastAPI backend for hotel management with PostgreSQL, AWS S3, and production-ready features.
Fully containerized backend - No Python/PostgreSQL installation needed!
Mac/Linux:
git clone <repository-url>
cd hotel_management_admin
./start-backend.shWindows:
git clone <repository-url>
cd hotel_management_admin
start-backend.bat- ✅ API running at: http://localhost:8050/docs (Swagger UI)
- ✅ PostgreSQL database pre-configured with migrations
- ✅ JWT authentication ready to use
- ✅ CORS enabled for local development (ports 3000, 5173, 8080)
- ✅ Zero backend setup - Only Docker Desktop required
# Automated API tests (Mac/Linux)
./test-api.sh
# Manual test
curl http://localhost:8050/api/v1/health- FRONTEND_QUICKSTART.md - Complete setup guide with API examples
- SHARE_WITH_FRONTEND.md - Quick reference with integration code
- http://localhost:8050/docs - Interactive API documentation
// Login and get JWT token
const response = await fetch('http://localhost:8050/api/v1/auth/token', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams({ username: 'user', password: 'pass' })
});
const { access_token } = await response.json();
// Use token for protected endpoints
const rooms = await fetch('http://localhost:8050/api/v1/rooms', {
headers: { 'Authorization': `Bearer ${access_token}` }
});# Start backend (builds image automatically on first run)
docker compose up -d
# Stop backend
docker compose down
# View logs (real-time)
docker compose logs -f
# Rebuild after backend code updates
git pull && docker compose up -d --build
# Manual build (optional - useful for testing)
docker compose build web
# Fresh start (⚠️ deletes all data)
docker compose down -v && docker compose up -d
# Access database
docker compose exec db psql -U postgres -d hotel_managementNote: The Docker image builds automatically when you first run ./start-backend.sh or docker compose up -d. First build takes ~3-5 minutes, subsequent builds are much faster (~30 seconds).
- Authentication: JWT-based OAuth2 authentication with secure password hashing
- Room Management: CRUD operations for hotel rooms with enum-based room types
- Customer Management: Customer records with document uploads to S3
- Booking Management: Reservations, check-ins, check-outs, payment tracking
- Document Storage: AWS S3 integration with file validation and database-first pattern
- Database: PostgreSQL 17 with Alembic migrations and row-level locking
- Monitoring: Sentry integration for error tracking
- Health Checks: Liveness and readiness probes for Kubernetes/Cloud Run
- Testing: 190+ comprehensive unit, integration, and e2e tests
Three ways to run this project:
| Setup | What You Need | Time | Best For |
|---|---|---|---|
| Hybrid (Recommended) | Python 3.14, UV, Docker | 10 min | Active development - Fast iteration, instant reload |
| Fully Docker | Docker Desktop only | 5 min | Quick start - No local dependencies needed |
| Fully Local | Python 3.14, PostgreSQL 17, UV | 15 min | Traditional setup - Full local control |
IMPORTANT: Create .env file in project root (same directory as README.md and docker-compose.yml).
# Navigate to project root
cd hotel_management_admin
# Create .env file
touch .env # or "type nul > .env" on Windows
# Edit with your preferred editor
code .env # VS Code
# OR: nano .env, vim .env, notepad .envCopy this template to your .env file:
# API Configuration
PROJECT_NAME="Hotel Management Admin"
API_V1_STR="/api/v1"
ENVIRONMENT="development"
# Database Configuration
# For Docker setup: use PG_HOST=db
# For Local/Hybrid: use PG_HOST=localhost
PG_HOST=localhost
PG_PORT=5432
PG_USERNAME=postgres
PG_PASSWORD=postgres
PG_DB=hotel_management
PG_SCHEMA=public
# Security (CHANGE IN PRODUCTION)
SECRET_KEY=dev-secret-key-change-in-production
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30
# AWS S3 - REPLACE WITH YOUR VALUES
AWS_ACCESS_KEY_ID=your_aws_access_key
AWS_SECRET_ACCESS_KEY=your_aws_secret_key
AWS_S3_BUCKET_NAME=your-bucket-name
AWS_S3_REGION=us-east-1
# File Upload
MAX_FILE_SIZE=10485760
ALLOWED_FILE_TYPES=["application/pdf", "image/jpeg", "image/png"]
# Optional - Monitoring
SENTRY_DSN=
# Optional - CORS
BACKEND_CORS_ORIGINS=["http://localhost:3000"]
# Server Port
# For Docker: use PORT=8080
# For Local/Hybrid: use PORT=8000
PORT=8000Key settings by setup type:
- Docker: Set
PG_HOST=dbandPORT=8080 - Local/Hybrid: Keep
PG_HOST=localhostandPORT=8000
Best for active development - App runs locally for instant reload, database in Docker for isolation.
# macOS
brew install python@3.14 docker
curl -LsSf https://astral.sh/uv/install.sh | sh
# Linux
sudo apt install python3.14 python3.14-venv docker.io docker-compose-v2
curl -LsSf https://astral.sh/uv/install.sh | sh
# Windows: Download and install
# Python: https://www.python.org/downloads/
# Docker: https://www.docker.com/products/docker-desktop
# UV: powershell -c "irm https://astral.sh/uv/install.ps1 | iex"git clone <your-repo-url>
cd hotel_management_admin
uv syncdocker compose up -d db
docker compose ps # Verify 'db' is runningCopy the environment template to .env with these settings:
PG_HOST=localhost(app connects to Docker DB via localhost)PORT=8000
uv run alembic upgrade head
uv run python initial_data.py # Optional: creates admin user
uv run uvicorn app.main:app --reload- Swagger UI: http://localhost:8000/docs
- Health Check: http://localhost:8000/api/v1/health
✅ Instant code reload (no Docker rebuild) ✅ Easy debugging with local Python ✅ Isolated database (no conflicts) ✅ Best developer experience
Quickest start - Everything in containers, no local dependencies.
# macOS
brew install --cask docker
# Linux
sudo apt install docker.io docker-compose-v2
sudo usermod -aG docker $USER && newgrp docker
# Windows: Download from https://www.docker.com/products/docker-desktopgit clone <your-repo-url>
cd hotel_management_admin
touch .envCopy the environment template to .env with these settings:
PG_HOST=db(Docker internal networking)PORT=8080
docker compose up -d
docker compose logs -f # Watch startup logs- Swagger UI: http://localhost:8050/docs
- Health Check: http://localhost:8050/api/v1/health/ready
# View logs
docker compose logs -f app
docker compose logs -f db
# Stop everything
docker compose down
# Fresh start (deletes all data - removes persistent volumes)
docker compose down -v && docker compose up -d
# Rebuild after code changes
docker compose up -d --build
# Database shell
docker compose exec db psql -U postgres -d hotel_managementTraditional setup - Full local control, best if you already have PostgreSQL installed.
# macOS
brew install python@3.14 postgresql@17
curl -LsSf https://astral.sh/uv/install.sh | sh
brew services start postgresql@17
# Linux
sudo apt install python3.14 python3.14-venv postgresql-17
curl -LsSf https://astral.sh/uv/install.sh | sh
sudo systemctl start postgresql && sudo systemctl enable postgresql
# Windows
# Download and install:
# - Python: https://www.python.org/downloads/
# - PostgreSQL: https://www.postgresql.org/download/windows/
# - UV: powershell -c "irm https://astral.sh/uv/install.ps1 | iex"# macOS/Linux
createdb hotel_management
# Windows (psql)
psql -U postgres
CREATE DATABASE hotel_management;
\qgit clone <your-repo-url>
cd hotel_management_admin
uv sync
touch .envCopy the environment template to .env with these settings:
PG_HOST=localhost(local PostgreSQL)PORT=8000
uv run alembic upgrade head
uv run python initial_data.py # Optional: creates admin user
uv run uvicorn app.main:app --reload- Swagger UI: http://localhost:8000/docs
- Health Check: http://localhost:8000/api/v1/health
All three setup options require AWS S3 credentials for document uploads to work.
# Option A: Using AWS Console
# 1. Go to: https://s3.console.aws.amazon.com/
# 2. Click "Create bucket"
# 3. Bucket name: hotel-management-uploads-<your-name>
# 4. Region: us-east-1 (or your preferred region)
# 5. Block all public access: ENABLED (recommended)
# 6. Click "Create bucket"
# Option B: Using AWS CLI
aws s3 mb s3://hotel-management-uploads-yourname --region us-east-1# Using AWS Console:
# 1. Go to: https://console.aws.amazon.com/iam/
# 2. Click "Users" → "Add users"
# 3. User name: hotel-management-app
# 4. Access type: Access key - Programmatic access
# 5. Attach policy: AmazonS3FullAccess (dev only)
# For production: Use custom policy with restricted bucket access
# See: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_examples_s3_rw-bucket.html
# 6. Save the Access Key ID and Secret Access KeyAWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE
AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
AWS_S3_BUCKET_NAME=hotel-management-uploads-yourname
AWS_S3_REGION=us-east-1# After starting the app, test file upload:
# 1. Login and get token from http://localhost:8000/docs
# 2. Create a customer
# 3. Upload a document to that customer
# 4. Check AWS S3 console - file should appear in bucketThe project includes 190+ tests covering unit, integration, and end-to-end scenarios with 75%+ code coverage.
# Run all tests
uv run pytest tests/ -v
# Run by test type
uv run pytest tests/unit/ -v # ~150 unit tests
uv run pytest tests/integration/ -v # ~34 integration tests
uv run pytest tests/e2e/ -v # ~9 e2e tests
# Run with coverage report
uv run pytest tests/ --cov=app --cov-report=html --cov-report=term
# Open HTML coverage report
open htmlcov/index.html
# Run specific test file
uv run pytest tests/unit/test_file_validator.py -v
# Run specific test function
uv run pytest tests/unit/test_file_validator.py::test_validate_file_success -v
# Faster execution (parallel mode)
uv run pytest tests/ -n auto- unit/: Services, validators, utilities (mocked dependencies)
- integration/: API endpoints with real database (mocked S3 via moto)
- e2e/: Complete workflows (login → upload → verify)
See CLAUDE.md for complete architecture details and patterns.
| Component | Technology | Version |
|---|---|---|
| Framework | FastAPI | 0.115+ |
| Language | Python | 3.14 |
| Database | PostgreSQL | 17 |
| ORM | SQLAlchemy | 2.0+ |
| Migrations | Alembic | 1.14+ |
| Storage | AWS S3 | boto3 1.35+ |
| Auth | JWT | python-jose 3.3+ |
| Testing | pytest | 8.0+ |
| Linting | Ruff | 0.8+ |
| Type Checking | MyPy | 1.12+ |
| Monitoring | Sentry | 1.40+ |
app/
├── main.py # FastAPI app + Sentry integration
├── api/
│ ├── routes.py # Central router
│ ├── dependencies/ # Dependency injection (SessionDep, CurrentUserDep)
│ └── endpoints/ # API endpoints by resource
├── core/
│ ├── config.py # Pydantic Settings
│ ├── security.py # JWT + password hashing
│ └── logging.py # Structured JSON logging
├── crud/ # Repository pattern (generic CRUD operations)
├── models/ # SQLAlchemy models + Pydantic schemas
├── services/ # Business logic (S3, file validation)
└── db/ # Database setup (get_db dependency)
tests/
├── conftest.py # Pytest fixtures
├── unit/ # Unit tests
├── integration/ # Integration tests
└── e2e/ # End-to-end tests
alembic/ # Database migrations
All endpoints prefixed with /api/v1:
POST /auth/token- Login (OAuth2 compatible)POST /auth/register- Create a staff user (requires login; no public sign-up)GET /auth/users- List all users (protected)
GET /rooms- List all roomsPOST /create-room- Create new room
GET /customers- List customersPOST /create-customer- Create customer
GET /bookings- List bookingsPOST /create-booking- Create bookingPATCH /bookings/{id}/check-in- Check-in guestPATCH /bookings/{id}/check-out- Check-out guestPATCH /bookings/{id}/cancel- Cancel booking
POST /upload-document/{customer_id}- Upload to S3DELETE /documents/{customer_id}- Delete from S3
GET /health- Liveness probe (always returns 200 if app is running)GET /health/ready- Readiness probe (checks database connectivity)
Note: uv run <command> executes commands in the project's virtual environment.
# Format code
uv run ruff format app/ tests/
# Lint and auto-fix
uv run ruff check --fix app/ tests/
# Type check (pragmatic config for SQLAlchemy)
uv run python -m mypy app/# Create migration after modifying models
uv run alembic revision --autogenerate -m "description"
# Apply migrations
uv run alembic upgrade head
# Rollback one version
uv run alembic downgrade -1
# Check current version
uv run alembic current
# View migration history
uv run alembic historyCreate default admin user for development:
uv run python initial_data.pyCreates: username: admin, password: admin123 (if no users exist). Override with FIRST_ADMIN_USERNAME / FIRST_ADMIN_PASSWORD; in production FIRST_ADMIN_PASSWORD is required.
The backend_pre_start.py script runs before the app starts (in Docker) to verify database connectivity:
# Run manually
uv run python backend_pre_start.pyFeatures:
- Retries DB connection up to 300 times (5 minutes)
- Logs all retry attempts
- Exits with error code if DB is unreachable
# Add a new Python package
uv add package-name # Runtime dependency
uv add --dev package-name # Development dependency
# Update all dependencies
uv lock --upgrade
# Sync environment with lockfile (after pulling changes)
uv sync
# View database schema
uv run python -c "from app.models import *; from app.db.base_db import Base; print(Base.metadata.tables)"
# Connect to database with psql
psql -h localhost -U postgres -d hotel_management
# Inside psql:
# \dt - List tables
# \d users - Describe users table
# \q - QuitDatabase connection fails:
# Check if PostgreSQL is running
docker ps # or: brew services list
# Test database connection manually
psql -h localhost -U postgres -d hotel_management
# Reset database (WARNING: DELETES ALL DATA - removes persistent volumes)
docker compose down -v # -v flag removes volumes
docker compose up -d db # Start fresh database
uv run alembic upgrade head # Recreate schema from migrationsImport errors or module not found:
# Ensure virtual environment is in sync with lockfile
uv sync
# Verify Python version
python --version # Should be 3.14+Alembic migration conflicts:
# Check current database migration version
uv run alembic current
# View migration history to identify conflicts
uv run alembic history
# Downgrade to specific version (if needed)
uv run alembic downgrade <revision_id>
# Then reapply migrations
uv run alembic upgrade headS3 upload fails:
- Verify AWS credentials are set correctly in
.env - Check S3 bucket exists:
aws s3 ls s3://your-bucket-name - Verify region is correct:
AWS_S3_REGION=us-east-1 - Ensure IAM user has
s3:PutObjectands3:DeleteObjectpermissions
This project follows FastAPI best practices and modern Python patterns:
- Dependency injection with type aliases (
SessionDep,CurrentUserDep,S3ServiceDep) - Database-first uploads to prevent orphaned S3 files (validate DB → upload S3 → update DB in single transaction)
- Generic CRUD repository for DRY database operations (
app/crud/base.py) - OAuth2 JWT authentication (FastAPI official pattern with
OAuth2PasswordBearer) - Minimalist exception handling (let FastAPI handle exceptions; only catch specific ones like
ClientError,IntegrityError) - Modern type hints (Python 3.10+ syntax:
list[T],dict[K, V],X | None)
See CLAUDE.md for detailed examples, rationale, and implementation guidelines.
- JWT Authentication: OAuth2-compatible token-based auth
- Password Hashing: bcrypt with cost factor 12
- File Validation: Magic bytes verification, size limits, path sanitization
- Path Traversal Prevention: Filename sanitization
- Database-first Uploads: Prevents orphaned S3 files through transactional consistency
- CORS: Configurable allowed origins
- Sentry: Error tracking and performance monitoring
Set these in production (in addition to the base configuration):
ENVIRONMENT=production
SECRET_KEY=<strong-random-key> # Generate with: openssl rand -hex 32
FIRST_ADMIN_PASSWORD=<initial-admin-password> # Used once, when the database has no users
HOTEL_TIMEZONE=Asia/Kolkata # Default; business dates and check-in/out times use this zone
SENTRY_DSN=<your-sentry-dsn>
PG_HOST=<production-db-host>
PG_PASSWORD=<strong-db-password>
AWS_ACCESS_KEY_ID=<production-key>
AWS_SECRET_ACCESS_KEY=<production-secret>On start the container:
- Waits for the database (
backend_pre_start.py) - Applies migrations (
alembic upgrade head) — works on an empty database - Creates the first admin user if there are no users (
initial_data.py) - Starts uvicorn on
${PORT}
Dependencies are installed from uv.lock (uv sync --frozen --no-dev).
Configure your orchestrator to use:
- Liveness:
GET /health- Returns 200 if app is running - Readiness:
GET /health/ready- Returns 200 only if database is accessible
Example Kubernetes config:
livenessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /health/ready
port: 8080
initialDelaySeconds: 10
periodSeconds: 5Automatically enabled if SENTRY_DSN is set:
export SENTRY_DSN="https://your-dsn@sentry.io/project-id"
export ENVIRONMENT="production"Features:
- Error tracking with full stack traces
- Performance monitoring (10% sample rate in production)
- SQLAlchemy query tracking
- FastAPI request tracing
All logs are output as JSON for easy parsing by log aggregators:
{
"timestamp": "2024-12-07 21:00:00,000",
"level": "INFO",
"name": "app.main",
"message": "Application started"
}- Fork the repository
- Create a feature branch
- Make your changes
- Run tests:
uv run pytest - Run linting:
uv run ruff check --fix app/ - Submit a pull request
- CLAUDE.md - Architecture principles, patterns, development guidelines, and AI agent context
- GEMINI.md - Alternative AI agent instructions and project conventions
MIT