2025-05-17 16:21:55 +00:00

142 lines
3.3 KiB
Markdown

# Simple REST API
A simple REST API built with FastAPI and SQLite.
## Features
- FastAPI with automatic OpenAPI documentation
- SQLite database with SQLAlchemy ORM
- Alembic migrations
- CRUD operations for items
- Health check endpoint
- Input validation and error handling
- Pagination and filtering
## Project Structure
```
.
├── alembic.ini # Alembic configuration
├── app # Application package
│ ├── api # API endpoints
│ │ ├── endpoints # API route handlers
│ │ └── router.py # API router
│ ├── core # Core application code
│ │ └── config.py # Application settings
│ ├── crud # Database CRUD operations
│ ├── database # Database configuration
│ │ ├── crud_base.py # Base CRUD class
│ │ └── session.py # Database session
│ ├── models # SQLAlchemy models
│ │ └── item.py # Item model
│ ├── schemas # Pydantic schemas
│ │ └── item.py # Item schemas
│ └── utils # Utility functions
├── main.py # Application entry point
├── migrations # Alembic migrations
│ ├── env.py # Alembic environment
│ ├── README # Migrations README
│ ├── script.py.mako # Migration script template
│ └── versions # Migration versions
│ └── *.py # Migration scripts
└── requirements.txt # Project dependencies
```
## Getting Started
### Prerequisites
- Python 3.8 or higher
- SQLite (included with Python)
### Installation
1. Clone the repository:
```bash
git clone https://github.com/yourusername/simple-rest-api.git
cd simple-rest-api
```
2. Create a virtual environment:
```bash
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
```
3. Install dependencies:
```bash
pip install -r requirements.txt
```
4. Apply database migrations:
```bash
alembic upgrade head
```
### Running the API
Start the application with:
```bash
uvicorn main:app --reload
```
The API will be available at http://localhost:8000.
- API Documentation: http://localhost:8000/docs
- Alternative Documentation: http://localhost:8000/redoc
## API Endpoints
### Health Check
- `GET /api/v1/health` - Check if the API is running
### Items
- `GET /api/v1/items` - List all items (with pagination and filtering)
- `GET /api/v1/items/{item_id}` - Get a specific item
- `POST /api/v1/items` - Create a new item
- `PUT /api/v1/items/{item_id}` - Update an item
- `DELETE /api/v1/items/{item_id}` - Delete an item
## Environment Variables
You can configure the application using environment variables:
- `PROJECT_NAME` - Application name (default: "SimpleRestAPI")
- `BACKEND_CORS_ORIGINS` - CORS origins, comma-separated (default: empty)
## Development
### Adding a New Migration
```bash
alembic revision --autogenerate -m "description of changes"
```
### Applying Migrations
```bash
alembic upgrade head
```
### Code Linting
```bash
ruff .
```
### Code Auto-Formatting
```bash
ruff . --fix
```
## License
This project is licensed under the MIT License.