112 lines
3.3 KiB
Markdown
112 lines
3.3 KiB
Markdown
# Generic REST API
|
|
|
|
This is a REST API built with FastAPI and SQLite. It provides endpoints for managing items with full CRUD operations.
|
|
|
|
## Features
|
|
|
|
- FastAPI framework for high performance
|
|
- SQLite database for data storage
|
|
- SQLAlchemy ORM for database interactions
|
|
- Alembic for database migrations
|
|
- Pydantic for data validation
|
|
- Automatic OpenAPI documentation
|
|
- CORS enabled
|
|
- Health check endpoint
|
|
|
|
## Requirements
|
|
|
|
- Python 3.8+
|
|
- All dependencies listed in `requirements.txt`
|
|
|
|
## Setup
|
|
|
|
1. Clone the repository
|
|
2. Install dependencies:
|
|
|
|
```bash
|
|
pip install -r requirements.txt
|
|
```
|
|
|
|
3. Set up environment variables (optional):
|
|
|
|
```bash
|
|
# Create a .env file or set these in your environment
|
|
NAME=YourNameValue
|
|
```
|
|
|
|
4. Create and migrate database:
|
|
|
|
```bash
|
|
# The storage directory and database will be created automatically when running the app
|
|
```
|
|
|
|
## Running the Application
|
|
|
|
Start the application with:
|
|
|
|
```bash
|
|
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
|
|
```
|
|
|
|
## API Documentation
|
|
|
|
Once the application is running, you can access:
|
|
|
|
- Interactive API documentation: [http://localhost:8000/docs](http://localhost:8000/docs)
|
|
- Alternative API documentation: [http://localhost:8000/redoc](http://localhost:8000/redoc)
|
|
- OpenAPI schema: [http://localhost:8000/openapi.json](http://localhost:8000/openapi.json)
|
|
|
|
## API Endpoints
|
|
|
|
- `GET /`: API information
|
|
- `GET /health`: Health check endpoint
|
|
- `GET /env/name`: Returns the value of the NAME environment variable
|
|
- `GET /api/v1/items`: List all items
|
|
- `POST /api/v1/items`: Create a new item
|
|
- `GET /api/v1/items/{id}`: Get an item by ID
|
|
- `PUT /api/v1/items/{id}`: Update an item
|
|
- `DELETE /api/v1/items/{id}`: Delete an item
|
|
|
|
## Project Structure
|
|
|
|
```
|
|
.
|
|
├── alembic.ini # Alembic configuration
|
|
├── app # Application package
|
|
│ ├── api # API endpoints
|
|
│ │ └── v1 # API version 1
|
|
│ │ ├── api.py # API router setup
|
|
│ │ └── endpoints # Endpoint modules
|
|
│ │ └── items.py # Items endpoints
|
|
│ ├── core # Core application code
|
|
│ │ └── config.py # Application configuration
|
|
│ ├── crud # CRUD operations
|
|
│ │ ├── base.py # Base CRUD class
|
|
│ │ └── item.py # Item CRUD operations
|
|
│ ├── db # Database setup
|
|
│ │ ├── base.py # Base model imports
|
|
│ │ ├── base_class.py # Base model class
|
|
│ │ ├── init_db.py # Database initialization
|
|
│ │ └── session.py # Database session
|
|
│ ├── models # Database models
|
|
│ │ └── item.py # Item model
|
|
│ └── schemas # Pydantic schemas
|
|
│ └── item.py # Item schemas
|
|
├── main.py # Application entry point
|
|
├── migrations # Database migrations
|
|
│ ├── env.py # Alembic environment
|
|
│ ├── README # Migrations README
|
|
│ ├── script.py.mako # Migration script template
|
|
│ └── versions # Migration versions
|
|
└── requirements.txt # Python dependencies
|
|
```
|
|
|
|
## Development
|
|
|
|
To lint and format the code:
|
|
|
|
```bash
|
|
ruff check .
|
|
ruff format .
|
|
```
|