152 lines
3.8 KiB
Markdown
152 lines
3.8 KiB
Markdown
# FastAPI REST API
|
|
|
|
A modern, fast REST API built with FastAPI and SQLite.
|
|
|
|
## Features
|
|
|
|
- FastAPI framework with all its features
|
|
- SQLAlchemy ORM
|
|
- SQLite database
|
|
- Alembic migrations
|
|
- Pydantic data validation
|
|
- Automatic API documentation with Swagger UI and ReDoc
|
|
- CORS middleware
|
|
- Health check endpoint
|
|
|
|
## Project Structure
|
|
|
|
```
|
|
.
|
|
├── alembic.ini # Alembic configuration
|
|
├── app # Main application package
|
|
│ ├── api # API endpoints
|
|
│ │ └── v1 # API version 1
|
|
│ │ ├── api.py # API router
|
|
│ │ └── endpoints # API endpoint modules
|
|
│ │ └── items.py # Items API endpoints
|
|
│ ├── core # Core modules
|
|
│ │ └── config.py # Application settings
|
|
│ ├── db # Database modules
|
|
│ │ └── database.py # Database connection
|
|
│ ├── models # SQLAlchemy models
|
|
│ │ ├── base.py # Base model
|
|
│ │ └── item.py # Item model
|
|
│ ├── schemas # Pydantic schemas
|
|
│ │ └── item.py # Item schemas
|
|
│ └── services # Business logic
|
|
├── main.py # Application entry point
|
|
├── migrations # Alembic migrations
|
|
│ ├── env.py # Alembic environment
|
|
│ ├── README # Alembic README
|
|
│ ├── script.py.mako # Migration script template
|
|
│ └── versions # Migration versions
|
|
│ └── 01_initial_migration.py # Initial migration
|
|
└── requirements.txt # Project dependencies
|
|
```
|
|
|
|
## Installation
|
|
|
|
1. Clone the repository:
|
|
|
|
```bash
|
|
git clone https://your-repository-url.git
|
|
cd fastapi-rest-api
|
|
```
|
|
|
|
2. Install dependencies:
|
|
|
|
```bash
|
|
pip install -r requirements.txt
|
|
```
|
|
|
|
3. Set up the database:
|
|
|
|
```bash
|
|
# The database will be automatically created at the configured location
|
|
# Make sure the directory exists
|
|
mkdir -p /app/storage/db
|
|
```
|
|
|
|
4. Run the migrations:
|
|
|
|
```bash
|
|
alembic upgrade head
|
|
```
|
|
|
|
## Running the API
|
|
|
|
Start the server with:
|
|
|
|
```bash
|
|
uvicorn main:app --reload
|
|
```
|
|
|
|
The API will be available at [http://localhost:8000](http://localhost:8000).
|
|
|
|
## API Documentation
|
|
|
|
- Swagger UI: [http://localhost:8000/docs](http://localhost:8000/docs)
|
|
- ReDoc: [http://localhost:8000/redoc](http://localhost:8000/redoc)
|
|
|
|
## API Endpoints
|
|
|
|
- `GET /api/v1/items`: Get a list of items
|
|
- `POST /api/v1/items`: Create a new item
|
|
- `GET /api/v1/items/{item_id}`: Get an item by ID
|
|
- `PUT /api/v1/items/{item_id}`: Update an item
|
|
- `DELETE /api/v1/items/{item_id}`: Delete an item
|
|
- `GET /health`: Check API health
|
|
|
|
## Usage Examples
|
|
|
|
### Get list of items
|
|
|
|
```bash
|
|
curl -X 'GET' 'http://localhost:8000/api/v1/items' -H 'accept: application/json'
|
|
```
|
|
|
|
### Create a new item
|
|
|
|
```bash
|
|
curl -X 'POST' \
|
|
'http://localhost:8000/api/v1/items' \
|
|
-H 'accept: application/json' \
|
|
-H 'Content-Type: application/json' \
|
|
-d '{
|
|
"name": "Example Item",
|
|
"description": "This is an example item",
|
|
"is_active": true
|
|
}'
|
|
```
|
|
|
|
### Get an item by ID
|
|
|
|
```bash
|
|
curl -X 'GET' 'http://localhost:8000/api/v1/items/1' -H 'accept: application/json'
|
|
```
|
|
|
|
### Update an item
|
|
|
|
```bash
|
|
curl -X 'PUT' \
|
|
'http://localhost:8000/api/v1/items/1' \
|
|
-H 'accept: application/json' \
|
|
-H 'Content-Type: application/json' \
|
|
-d '{
|
|
"name": "Updated Item",
|
|
"description": "This item has been updated",
|
|
"is_active": true
|
|
}'
|
|
```
|
|
|
|
### Delete an item
|
|
|
|
```bash
|
|
curl -X 'DELETE' 'http://localhost:8000/api/v1/items/1' -H 'accept: application/json'
|
|
```
|
|
|
|
### Check API health
|
|
|
|
```bash
|
|
curl -X 'GET' 'http://localhost:8000/health' -H 'accept: application/json'
|
|
``` |