HaloLight Backend API implementation using Python, FastAPI, SQLAlchemy 2.0, and PostgreSQL.
- FastAPI 0.115+ - Modern, fast web framework with automatic OpenAPI documentation
- SQLAlchemy 2.0 - Powerful ORM with type hints support
- Alembic - Database migration management
- Pydantic v2 - Data validation using Python type annotations
- JWT Authentication - Secure token-based authentication
- Password Hashing - bcrypt password hashing
- CORS Support - Cross-Origin Resource Sharing enabled
- Docker Support - Containerized deployment with Docker Compose
- PostgreSQL - Production-ready relational database
- Framework: FastAPI 0.115+
- ORM: SQLAlchemy 2.0.36+
- Database: PostgreSQL 16
- Migrations: Alembic 1.14+
- Validation: Pydantic v2
- Authentication: python-jose (JWT)
- Password Hashing: passlib + bcrypt
- Server: Uvicorn with uvloop
- Python 3.11 or higher
- PostgreSQL 16 or higher
- pip or poetry for dependency management
git clone https://github.com/halolight/halolight-api-python.git
cd halolight-api-pythonpython -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activatepip install -r requirements.txtcp .env.example .envEdit .env and update the following:
DATABASE_URL=postgresql://user:password@localhost:5432/halolight
JWT_SECRET_KEY=your-secret-key-change-this-in-production# Create initial migration
alembic revision --autogenerate -m "Initial migration"
# Apply migrations
alembic upgrade head# Development mode (with auto-reload)
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
# Production mode
uvicorn app.main:app --host 0.0.0.0 --port 8000 --workers 4The API will be available at:
- API: http://localhost:8000
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/api/redoc
# Start all services (PostgreSQL + API)
docker-compose up -d
# View logs
docker-compose logs -f api
# Stop services
docker-compose down
# Stop and remove volumes
docker-compose down -v# Build image
docker build -t halolight-api .
# Run container
docker run -p 8000:8000 \
-e DATABASE_URL=postgresql://user:password@host:5432/halolight \
-e JWT_SECRET_KEY=your-secret-key \
halolight-apiGET /- API informationGET /health- Health check
POST /api/auth/register- Register new userPOST /api/auth/login- Login userPOST /api/auth/logout- Logout user (placeholder)
GET /api/users/me- Get current user profile (authenticated)GET /api/users- List all users with pagination (admin only)GET /api/users/{user_id}- Get user by ID (admin only)POST /api/users- Create user (admin only)PATCH /api/users/{user_id}- Update user (admin only)DELETE /api/users/{user_id}- Delete user (admin only)
curl -X POST http://localhost:8000/api/auth/register \
-H "Content-Type: application/json" \
-d '{
"email": "user@example.com",
"password": "password123",
"name": "John Doe"
}'curl -X POST http://localhost:8000/api/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "user@example.com",
"password": "password123"
}'curl -X GET http://localhost:8000/api/users/me \
-H "Authorization: Bearer YOUR_JWT_TOKEN"curl -X GET "http://localhost:8000/api/users?page=1&page_size=10" \
-H "Authorization: Bearer ADMIN_JWT_TOKEN"class User:
id: str # Primary key (auto-generated)
email: str # Unique email address
name: str | None # Optional display name
password: str # Hashed password
role: UserRole # USER or ADMIN
avatar: str | None # Optional avatar URL
status: str # Account status (default: "active")
created_at: datetime # Creation timestamp
updated_at: datetime # Last update timestamphalolight-api-python/
βββ app/
β βββ __init__.py
β βββ main.py # FastAPI application entry point
β βββ api/
β β βββ __init__.py
β β βββ auth.py # Authentication routes
β β βββ users.py # User routes
β β βββ deps.py # Dependency injection
β βββ models/
β β βββ __init__.py
β β βββ user.py # SQLAlchemy User model
β βββ schemas/
β β βββ __init__.py
β β βββ user.py # Pydantic schemas
β βββ services/
β β βββ __init__.py
β β βββ user_service.py # Business logic
β βββ core/
β β βββ __init__.py
β β βββ config.py # Configuration settings
β β βββ security.py # Security utilities
β β βββ database.py # Database connection
β βββ utils/
β βββ __init__.py
βββ alembic/ # Database migrations
β βββ versions/
β βββ env.py
βββ tests/
βββ .env.example
βββ alembic.ini
βββ docker-compose.yml
βββ Dockerfile
βββ pyproject.toml
βββ requirements.txt
βββ README.md
# Install dev dependencies
pip install -r requirements.txt
# Run tests with pytest
pytest
# Run tests with coverage
pytest --cov=app tests/# Format code with Black
black app tests
# Lint with Ruff
ruff check app tests
# Type check with mypy
mypy app# Create new migration
alembic revision --autogenerate -m "description"
# Apply migrations
alembic upgrade head
# Rollback migration
alembic downgrade -1
# View migration history
alembic history| Variable | Description | Default |
|---|---|---|
APP_NAME |
Application name | HaloLight API |
APP_VERSION |
Application version | 1.0.0 |
DEBUG |
Enable debug mode | false |
ENVIRONMENT |
Environment (development/production/test) | development |
API_PREFIX |
API prefix path | /api |
CORS_ORIGINS |
Allowed CORS origins | ["http://localhost:3000"] |
DATABASE_URL |
PostgreSQL connection URL | Required |
DATABASE_ECHO |
Echo SQL queries | false |
JWT_SECRET_KEY |
JWT secret key | Required |
JWT_ALGORITHM |
JWT algorithm | HS256 |
JWT_ACCESS_TOKEN_EXPIRE_MINUTES |
Token expiration time | 10080 (7 days) |
PASSWORD_MIN_LENGTH |
Minimum password length | 6 |
- Passwords are hashed using bcrypt
- JWT tokens are used for authentication
- CORS is configured to allow specific origins
- SQL injection protection via SQLAlchemy ORM
- Input validation via Pydantic schemas
This API follows a layered architecture:
- Routes (
api/) - Handle HTTP requests and responses - Services (
services/) - Business logic and data manipulation - Models (
models/) - Database models and ORM mappings - Schemas (
schemas/) - Request/response validation and serialization - Core (
core/) - Configuration, security, and database setup
- Async/await support for non-blocking I/O
- Connection pooling with SQLAlchemy
- JWT stateless authentication (no database lookups per request)
- Efficient pagination for list endpoints
- Fork the repository
- Create a feature branch:
git checkout -b feature/my-feature - Commit your changes:
git commit -am 'Add new feature' - Push to the branch:
git push origin feature/my-feature - Submit a pull request
- halolight - Next.js reference implementation
- halolight-vue - Vue 3 reference implementation
- halolight-api-node - Node.js/Express API
- docs - Documentation and specifications
π Complete Documentation: https://halolight.docs.h7ml.cn/
ISC
For issues and questions, please open an issue on GitHub.