
- Set up project structure with FastAPI - Configure SQLAlchemy with SQLite - Implement user and item models - Set up Alembic for database migrations - Create CRUD operations for models - Implement API endpoints for users and items - Add authentication functionality - Add health check endpoint - Configure Ruff for linting - Update README with comprehensive documentation
139 lines
4.1 KiB
Markdown
139 lines
4.1 KiB
Markdown
# Generic REST API Service
|
|
|
|
A robust and scalable REST API service built with FastAPI and SQLite.
|
|
|
|
## Features
|
|
|
|
- **Modern FastAPI Framework**: Fully typed and async-ready API framework
|
|
- **SQLAlchemy ORM**: Object-relational mapping for database interactions
|
|
- **Alembic Migrations**: Automated database schema migrations
|
|
- **Pydantic Models**: Request and response validation
|
|
- **Dependency Injection**: Clean, modular code structure
|
|
- **OpenAPI Documentation**: Auto-generated, interactive API docs
|
|
- **Health Check Endpoint**: API status monitoring
|
|
- **User Authentication**: Basic auth implementation
|
|
|
|
## Project Structure
|
|
|
|
```
|
|
.
|
|
├── alembic.ini # Alembic configuration
|
|
├── app # Main application package
|
|
│ ├── api # API endpoints
|
|
│ │ ├── dependencies # FastAPI dependencies
|
|
│ │ └── v1 # API version 1
|
|
│ │ ├── api.py # API router
|
|
│ │ └── endpoints # API endpoint modules
|
|
│ ├── core # Core application code
|
|
│ │ ├── app.py # FastAPI application factory
|
|
│ │ ├── config.py # Application configuration
|
|
│ │ └── security.py # Security utilities
|
|
│ ├── crud # CRUD operations
|
|
│ ├── db # Database utilities
|
|
│ │ └── session.py # Database session management
|
|
│ ├── models # SQLAlchemy models
|
|
│ └── schemas # Pydantic schemas
|
|
├── main.py # Application entry point
|
|
├── migrations # Alembic migrations
|
|
│ ├── env.py # Alembic environment
|
|
│ ├── script.py.mako # Alembic script template
|
|
│ └── versions # Migration versions
|
|
└── requirements.txt # Project dependencies
|
|
```
|
|
|
|
## Getting Started
|
|
|
|
### Prerequisites
|
|
|
|
- Python 3.8 or higher
|
|
- pip (Python package manager)
|
|
|
|
### Installation
|
|
|
|
1. Clone the repository:
|
|
```
|
|
git clone https://github.com/yourusername/genericrestapiservice.git
|
|
cd genericrestapiservice
|
|
```
|
|
|
|
2. Create a virtual environment (optional but recommended):
|
|
```
|
|
python -m venv venv
|
|
source venv/bin/activate # On Windows: venv\Scripts\activate
|
|
```
|
|
|
|
3. Install dependencies:
|
|
```
|
|
pip install -r requirements.txt
|
|
```
|
|
|
|
4. Run database migrations:
|
|
```
|
|
alembic upgrade head
|
|
```
|
|
|
|
5. Start the development server:
|
|
```
|
|
uvicorn main:app --reload
|
|
```
|
|
|
|
The API will be available at http://localhost:8000.
|
|
|
|
### API Documentation
|
|
|
|
Once the application is running, you can access:
|
|
|
|
- Interactive API documentation: http://localhost:8000/docs
|
|
- Alternative API documentation: http://localhost:8000/redoc
|
|
|
|
## API Endpoints
|
|
|
|
### Authentication
|
|
|
|
- `POST /api/v1/auth/login` - Get access token
|
|
- `GET /api/v1/auth/me` - Get current user info
|
|
|
|
### Users
|
|
|
|
- `GET /api/v1/users/` - List all users (admin only)
|
|
- `POST /api/v1/users/` - Create a new user
|
|
- `GET /api/v1/users/{user_id}` - Get user details
|
|
- `PUT /api/v1/users/{user_id}` - Update user (admin only)
|
|
- `DELETE /api/v1/users/{user_id}` - Delete user (admin only)
|
|
|
|
### Items
|
|
|
|
- `GET /api/v1/items/` - List items (filtered by user)
|
|
- `POST /api/v1/items/` - Create a new item
|
|
- `GET /api/v1/items/{id}` - Get item details
|
|
- `PUT /api/v1/items/{id}` - Update item
|
|
- `DELETE /api/v1/items/{id}` - Delete item
|
|
|
|
### Health Check
|
|
|
|
- `GET /health` - API health status
|
|
- `GET /api/v1/health` - API health status (versioned)
|
|
|
|
## Development
|
|
|
|
### Adding New Models
|
|
|
|
1. Define SQLAlchemy model in `app/models/`
|
|
2. Define Pydantic schemas in `app/schemas/`
|
|
3. Create CRUD operations in `app/crud/`
|
|
4. Create a new migration with Alembic:
|
|
```
|
|
alembic revision --autogenerate -m "Add new model"
|
|
```
|
|
5. Create API endpoints in `app/api/v1/endpoints/`
|
|
6. Register endpoints in `app/api/v1/api.py`
|
|
|
|
### Running Tests
|
|
|
|
```
|
|
pytest
|
|
```
|
|
|
|
## License
|
|
|
|
This project is licensed under the MIT License - see the LICENSE file for details. |