3.0 KiB
3.0 KiB
REST API Service
A REST API service built with FastAPI and SQLite.
Features
- FastAPI for high-performance API endpoints
- SQLite database for simple data storage
- Alembic for database migrations
- Pydantic for data validation
- CORS middleware configured
- Health check endpoint
- API documentation with OpenAPI
Project Structure
.
├── alembic.ini # Alembic configuration
├── app # Application code
│ ├── api # API endpoints
│ │ ├── v1 # API version 1
│ │ │ ├── api.py # API router
│ │ │ └── endpoints # API endpoint modules
│ │ │ └── items.py # Items endpoints
│ │ └── health.py # Health check endpoint
│ ├── core # Core modules
│ │ └── config.py # Application configuration
│ ├── db # Database code
│ │ └── base.py # Database connection code
│ ├── 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 # Alembic script template
│ └── versions # Migration versions
│ └── 01_create_items_table.py # Initial migration
└── requirements.txt # Python dependencies
Installation
- Clone the repository:
git clone <repository-url>
cd <repository-directory>
- Create a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
- Install dependencies:
pip install -r requirements.txt
- Configure environment variables:
Create a .env
file based on .env.example
.
Database Migrations
Initialize the database:
alembic upgrade head
Running the API
Start the API server:
uvicorn main:app --reload
The API will be available at http://localhost:8000.
- API documentation: http://localhost:8000/docs
- ReDoc documentation: http://localhost:8000/redoc
- OpenAPI schema: http://localhost:8000/openapi.json
- Health check: http://localhost:8000/health
API Endpoints
Items
GET /api/v1/items/
: List all itemsPOST /api/v1/items/
: Create a new itemGET /api/v1/items/{item_id}
: Get a specific itemPUT /api/v1/items/{item_id}
: Update a specific itemDELETE /api/v1/items/{item_id}
: Delete a specific item
Environment Variables
APP_NAME
: Application nameAPI_V1_STR
: API v1 path prefixCORS_ORIGINS
: CORS allowed origins