
- Set up FastAPI application with proper project structure - Configure SQLite database with SQLAlchemy ORM - Implement user model with CRUD operations - Add Alembic for database migrations - Create comprehensive API endpoints for user management - Configure CORS middleware for cross-origin requests - Add health check endpoint and service information - Set up Ruff for code linting and formatting - Update README with complete documentation Co-Authored-By: BackendIM <noreply@backendim.com>
123 lines
3.3 KiB
Markdown
123 lines
3.3 KiB
Markdown
# REST API Service
|
|
|
|
A REST API service built with FastAPI, featuring user management, SQLite database, and comprehensive API documentation.
|
|
|
|
## Features
|
|
|
|
- **FastAPI Framework**: Modern, fast web framework for building APIs
|
|
- **SQLite Database**: Lightweight, file-based database with SQLAlchemy ORM
|
|
- **User Management**: Complete CRUD operations for user entities
|
|
- **Database Migrations**: Alembic integration for database schema management
|
|
- **API Documentation**: Auto-generated OpenAPI/Swagger documentation
|
|
- **CORS Support**: Cross-Origin Resource Sharing enabled for all origins
|
|
- **Health Check**: Built-in health monitoring endpoint
|
|
- **Code Quality**: Ruff linting and formatting
|
|
|
|
## Project Structure
|
|
|
|
```
|
|
├── main.py # FastAPI application entry point
|
|
├── requirements.txt # Python dependencies
|
|
├── pyproject.toml # Ruff configuration
|
|
├── alembic.ini # Alembic configuration
|
|
├── alembic/ # Database migrations
|
|
│ ├── versions/ # Migration files
|
|
│ └── env.py # Alembic environment
|
|
└── app/
|
|
├── api/
|
|
│ └── v1/
|
|
│ ├── api.py # API router aggregation
|
|
│ └── endpoints/ # API endpoints
|
|
│ └── users.py
|
|
├── crud/ # Database operations
|
|
│ └── user.py
|
|
├── db/ # Database configuration
|
|
│ ├── base.py # SQLAlchemy base
|
|
│ └── session.py # Database session
|
|
├── models/ # Database models
|
|
│ └── user.py
|
|
└── schemas/ # Pydantic schemas
|
|
└── user.py
|
|
```
|
|
|
|
## API Endpoints
|
|
|
|
### Base Endpoints
|
|
- `GET /` - Service information and links
|
|
- `GET /health` - Health check endpoint
|
|
- `GET /docs` - Interactive API documentation (Swagger UI)
|
|
- `GET /redoc` - Alternative API documentation (ReDoc)
|
|
- `GET /openapi.json` - OpenAPI specification
|
|
|
|
### User Management
|
|
- `GET /api/v1/users/` - List all users (with pagination)
|
|
- `GET /api/v1/users/{user_id}` - Get user by ID
|
|
- `POST /api/v1/users/` - Create new user
|
|
- `PUT /api/v1/users/{user_id}` - Update user
|
|
- `DELETE /api/v1/users/{user_id}` - Delete user
|
|
|
|
## Installation
|
|
|
|
1. Install dependencies:
|
|
```bash
|
|
pip install -r requirements.txt
|
|
```
|
|
|
|
2. Run database migrations:
|
|
```bash
|
|
alembic upgrade head
|
|
```
|
|
|
|
3. Start the server:
|
|
```bash
|
|
uvicorn main:app --reload
|
|
```
|
|
|
|
The API will be available at `http://localhost:8000`
|
|
|
|
## Environment Variables
|
|
|
|
No environment variables are required for basic operation. The application uses SQLite database stored in `/app/storage/db/db.sqlite`.
|
|
|
|
## Development
|
|
|
|
### Code Quality
|
|
Run linting and formatting:
|
|
```bash
|
|
ruff check --fix .
|
|
```
|
|
|
|
### Database Migrations
|
|
Create a new migration:
|
|
```bash
|
|
alembic revision -m "Description of changes"
|
|
```
|
|
|
|
Apply migrations:
|
|
```bash
|
|
alembic upgrade head
|
|
```
|
|
|
|
## API Usage Examples
|
|
|
|
### Create a User
|
|
```bash
|
|
curl -X POST "http://localhost:8000/api/v1/users/" \
|
|
-H "Content-Type: application/json" \
|
|
-d '{
|
|
"email": "user@example.com",
|
|
"full_name": "John Doe",
|
|
"password": "secretpassword"
|
|
}'
|
|
```
|
|
|
|
### Get All Users
|
|
```bash
|
|
curl "http://localhost:8000/api/v1/users/"
|
|
```
|
|
|
|
### Health Check
|
|
```bash
|
|
curl "http://localhost:8000/health"
|
|
```
|