
- 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
3.6 KiB
3.6 KiB
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
- Clone the repository:
git clone <repository-url>
cd genericrestapi
- Install the dependencies:
pip install -r requirements.txt
- Run the database migrations:
alembic upgrade head
Running the API
Start the FastAPI application:
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 itemsPOST /api/v1/items
: Create a new itemGET /api/v1/items/{id}
: Get item detailsPUT /api/v1/items/{id}
: Update an itemDELETE /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 .