
- Set up project structure and dependencies - Implement database models with SQLAlchemy - Set up Alembic migrations - Create FastAPI application with CORS support - Implement API endpoints (CRUD operations) - Add health check endpoint - Update README with setup and usage instructions
117 lines
3.6 KiB
Markdown
117 lines
3.6 KiB
Markdown
# Generic REST API
|
|
|
|
A generic REST API built with FastAPI and SQLite, providing CRUD operations for managing items.
|
|
|
|
## Features
|
|
|
|
- FastAPI framework with automatic documentation (OpenAPI)
|
|
- SQLAlchemy ORM for database interactions
|
|
- Alembic for database migrations
|
|
- SQLite database (no external database required)
|
|
- CORS support (enabled for all origins)
|
|
- Health check endpoint
|
|
- RESTful API design
|
|
- Pydantic models for validation
|
|
|
|
## Requirements
|
|
|
|
- Python 3.8+
|
|
- pip for package installation
|
|
|
|
## Project Structure
|
|
|
|
```
|
|
.
|
|
├── alembic.ini # Alembic configuration
|
|
├── app # Application package
|
|
│ ├── api # API endpoints
|
|
│ │ ├── deps.py # API dependencies
|
|
│ │ └── v1 # API version 1
|
|
│ │ ├── api.py # API router
|
|
│ │ └── endpoints # API endpoint modules
|
|
│ │ ├── health.py # Health check endpoint
|
|
│ │ └── items.py # Items CRUD endpoints
|
|
│ ├── core # Core modules
|
|
│ │ └── config.py # Application configuration
|
|
│ ├── crud # CRUD operations
|
|
│ │ ├── base.py # Base CRUD operations
|
|
│ │ └── item.py # Item-specific CRUD
|
|
│ ├── db # Database
|
|
│ │ ├── base.py # Base model imports
|
|
│ │ ├── base_class.py # Base model class
|
|
│ │ └── session.py # Database session
|
|
│ ├── models # SQLAlchemy models
|
|
│ │ └── item.py # Item model
|
|
│ └── schemas # Pydantic schemas
|
|
│ └── item.py # Item schemas
|
|
├── main.py # Application entry point
|
|
├── migrations # Alembic migrations
|
|
│ ├── env.py # Alembic environment
|
|
│ ├── script.py.mako # Migration script template
|
|
│ └── versions # Migration versions
|
|
│ └── 001_create_item_table.py # Initial migration
|
|
└── requirements.txt # Package requirements
|
|
```
|
|
|
|
## Installation
|
|
|
|
1. Clone the repository:
|
|
|
|
```bash
|
|
git clone <repository-url>
|
|
cd genericrestapi
|
|
```
|
|
|
|
2. Install the dependencies:
|
|
|
|
```bash
|
|
pip install -r requirements.txt
|
|
```
|
|
|
|
3. Run the database migrations:
|
|
|
|
```bash
|
|
alembic upgrade head
|
|
```
|
|
|
|
## Running the API
|
|
|
|
Start the FastAPI application:
|
|
|
|
```bash
|
|
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
|
|
```
|
|
|
|
The API will be available at `http://localhost:8000`.
|
|
|
|
## API Documentation
|
|
|
|
Once the application is running, you can access the API documentation:
|
|
|
|
- Swagger UI: `http://localhost:8000/docs`
|
|
- ReDoc: `http://localhost:8000/redoc`
|
|
- OpenAPI JSON: `http://localhost:8000/openapi.json`
|
|
|
|
## API Endpoints
|
|
|
|
### Items
|
|
|
|
- `GET /api/v1/items`: List all items
|
|
- `POST /api/v1/items`: Create a new item
|
|
- `GET /api/v1/items/{id}`: Get item details
|
|
- `PUT /api/v1/items/{id}`: Update an item
|
|
- `DELETE /api/v1/items/{id}`: Delete an item
|
|
|
|
### Health
|
|
|
|
- `GET /api/v1/health`: Check API health status
|
|
|
|
## Environment Variables
|
|
|
|
This application does not require any environment variables to run, as it uses SQLite as the database which is automatically configured.
|
|
|
|
## Development
|
|
|
|
- The SQLite database is stored at `/app/storage/db/db.sqlite`
|
|
- Database migrations can be created using Alembic: `alembic revision -m "description"`
|
|
- Use Ruff for linting: `ruff check .` |