From a4ffe78f61dd7208c1fbf63a5f8965c5ade059e8 Mon Sep 17 00:00:00 2001 From: Automated Action Date: Wed, 14 May 2025 10:26:10 +0000 Subject: [PATCH] Create REST API with FastAPI and SQLAlchemy - Set up project structure with FastAPI - Create SQLite database with SQLAlchemy - Implement Alembic for database migrations - Add CRUD operations for items resource - Create health endpoint - Update documentation generated with BackendIM... (backend.im) --- README.md | 72 ++++++++++++++++++- alembic.ini | 41 +++++++++++ alembic/README | 1 + alembic/env.py | 80 ++++++++++++++++++++++ alembic/script.py.mako | 24 +++++++ alembic/versions/001_create_items_table.py | 36 ++++++++++ app/api/__init__.py | 0 app/api/endpoints/__init__.py | 5 ++ app/api/endpoints/items.py | 53 ++++++++++++++ app/api/health.py | 8 +++ app/crud/item.py | 39 +++++++++++ app/db/base.py | 3 + app/db/base_class.py | 12 ++++ app/db/session.py | 23 +++++++ app/models/item.py | 11 +++ app/schemas/item.py | 25 +++++++ main.py | 16 +++++ requirements.txt | 6 ++ 18 files changed, 453 insertions(+), 2 deletions(-) create mode 100644 alembic.ini create mode 100644 alembic/README create mode 100644 alembic/env.py create mode 100644 alembic/script.py.mako create mode 100644 alembic/versions/001_create_items_table.py create mode 100644 app/api/__init__.py create mode 100644 app/api/endpoints/__init__.py create mode 100644 app/api/endpoints/items.py create mode 100644 app/api/health.py create mode 100644 app/crud/item.py create mode 100644 app/db/base.py create mode 100644 app/db/base_class.py create mode 100644 app/db/session.py create mode 100644 app/models/item.py create mode 100644 app/schemas/item.py create mode 100644 main.py create mode 100644 requirements.txt diff --git a/README.md b/README.md index e8acfba..5f363a4 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,71 @@ -# FastAPI Application +# Generic REST API -This is a FastAPI application bootstrapped by BackendIM, the AI-powered backend generation platform. +A RESTful API built with FastAPI and SQLAlchemy. + +## Features + +- FastAPI for high-performance API with automatic OpenAPI documentation +- SQLAlchemy ORM for database operations +- SQLite database for storage +- Alembic for database migrations +- Pydantic models for request/response validation +- CRUD operations for the 'items' resource +- Health check endpoint + +## Project Structure + +``` +├── alembic/ # Database migration scripts +├── app/ # Application source code +│ ├── api/ # API endpoints +│ │ ├── endpoints/ # API routes +│ ├── crud/ # CRUD operations +│ ├── db/ # Database configurations +│ ├── models/ # SQLAlchemy models +│ └── schemas/ # Pydantic schemas +├── storage/ # Storage directory +│ └── db/ # Database files +├── alembic.ini # Alembic configuration +├── main.py # Application entry point +└── requirements.txt # Project dependencies +``` + +## Installation and Setup + +1. Clone the repository + +2. Install the dependencies: + ```bash + pip install -r requirements.txt + ``` + +3. Apply the database migrations: + ```bash + alembic upgrade head + ``` + +4. Run the application: + ```bash + uvicorn main:app --reload + ``` + +## API Documentation + +Once the application is running, you can access the API documentation at: + +- Swagger UI: [http://localhost:8000/docs](http://localhost:8000/docs) +- ReDoc: [http://localhost:8000/redoc](http://localhost:8000/redoc) + +## API Endpoints + +### Items Resource + +- `GET /items`: Get all items (paginated) +- `POST /items`: Create a new item +- `GET /items/{item_id}`: Get a specific item by ID +- `PUT /items/{item_id}`: Update an item +- `DELETE /items/{item_id}`: Delete an item + +### Health Check + +- `GET /health`: Check API health status \ No newline at end of file diff --git a/alembic.ini b/alembic.ini new file mode 100644 index 0000000..07edd8e --- /dev/null +++ b/alembic.ini @@ -0,0 +1,41 @@ +[alembic] +script_location = alembic +prepend_sys_path = . +version_path_separator = os + +# SQLite URL example +sqlalchemy.url = sqlite:////app/storage/db/db.sqlite + +[loggers] +keys = root,sqlalchemy,alembic + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = WARN +handlers = console +qualname = + +[logger_sqlalchemy] +level = WARN +handlers = +qualname = sqlalchemy.engine + +[logger_alembic] +level = INFO +handlers = +qualname = alembic + +[handler_console] +class = StreamHandler +args = (sys.stderr,) +level = NOTSET +formatter = generic + +[formatter_generic] +format = %(levelname)-5.5s [%(name)s] %(message)s +datefmt = %H:%M:%S \ No newline at end of file diff --git a/alembic/README b/alembic/README new file mode 100644 index 0000000..efdfa49 --- /dev/null +++ b/alembic/README @@ -0,0 +1 @@ +Generic single-database configuration for SQLAlchemy. \ No newline at end of file diff --git a/alembic/env.py b/alembic/env.py new file mode 100644 index 0000000..d3650d8 --- /dev/null +++ b/alembic/env.py @@ -0,0 +1,80 @@ +import os +import sys +from logging.config import fileConfig + +from sqlalchemy import engine_from_config +from sqlalchemy import pool + +from alembic import context + +sys.path.insert(0, os.path.dirname(os.path.dirname(__file__))) + +# this is the Alembic Config object, which provides +# access to the values within the .ini file in use. +config = context.config + +# Interpret the config file for Python logging. +# This line sets up loggers basically. +fileConfig(config.config_file_name) + +# add your model's MetaData object here +# for 'autogenerate' support +from app.db.base import Base # noqa +target_metadata = Base.metadata + +# other values from the config, defined by the needs of env.py, +# can be acquired: +# my_important_option = config.get_main_option("my_important_option") +# ... etc. + + +def run_migrations_offline(): + """Run migrations in 'offline' mode. + + This configures the context with just a URL + and not an Engine, though an Engine is acceptable + here as well. By skipping the Engine creation + we don't even need a DBAPI to be available. + + Calls to context.execute() here emit the given string to the + script output. + + """ + url = config.get_main_option("sqlalchemy.url") + context.configure( + url=url, + target_metadata=target_metadata, + literal_binds=True, + dialect_opts={"paramstyle": "named"}, + ) + + with context.begin_transaction(): + context.run_migrations() + + +def run_migrations_online(): + """Run migrations in 'online' mode. + + In this scenario we need to create an Engine + and associate a connection with the context. + + """ + connectable = engine_from_config( + config.get_section(config.config_ini_section), + prefix="sqlalchemy.", + poolclass=pool.NullPool, + ) + + with connectable.connect() as connection: + context.configure( + connection=connection, target_metadata=target_metadata + ) + + with context.begin_transaction(): + context.run_migrations() + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() \ No newline at end of file diff --git a/alembic/script.py.mako b/alembic/script.py.mako new file mode 100644 index 0000000..1e4564e --- /dev/null +++ b/alembic/script.py.mako @@ -0,0 +1,24 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} + +""" +from alembic import op +import sqlalchemy as sa +${imports if imports else ""} + +# revision identifiers, used by Alembic. +revision = ${repr(up_revision)} +down_revision = ${repr(down_revision)} +branch_labels = ${repr(branch_labels)} +depends_on = ${repr(depends_on)} + + +def upgrade(): + ${upgrades if upgrades else "pass"} + + +def downgrade(): + ${downgrades if downgrades else "pass"} \ No newline at end of file diff --git a/alembic/versions/001_create_items_table.py b/alembic/versions/001_create_items_table.py new file mode 100644 index 0000000..ce34b49 --- /dev/null +++ b/alembic/versions/001_create_items_table.py @@ -0,0 +1,36 @@ +"""create items table + +Revision ID: 001 +Revises: +Create Date: 2025-05-14 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '001' +down_revision = None +branch_labels = None +depends_on = None + + +def upgrade(): + op.create_table( + 'item', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('title', sa.String(length=100), nullable=False), + sa.Column('description', sa.Text(), nullable=True), + sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False), + sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_item_id'), 'item', ['id'], unique=False) + op.create_index(op.f('ix_item_title'), 'item', ['title'], unique=False) + + +def downgrade(): + op.drop_index(op.f('ix_item_title'), table_name='item') + op.drop_index(op.f('ix_item_id'), table_name='item') + op.drop_table('item') \ No newline at end of file diff --git a/app/api/__init__.py b/app/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/api/endpoints/__init__.py b/app/api/endpoints/__init__.py new file mode 100644 index 0000000..1049369 --- /dev/null +++ b/app/api/endpoints/__init__.py @@ -0,0 +1,5 @@ +from fastapi import APIRouter +from app.api.endpoints import items + +router = APIRouter() +router.include_router(items.router, prefix="/items", tags=["items"]) \ No newline at end of file diff --git a/app/api/endpoints/items.py b/app/api/endpoints/items.py new file mode 100644 index 0000000..f43ab3d --- /dev/null +++ b/app/api/endpoints/items.py @@ -0,0 +1,53 @@ +from typing import List, Optional +from fastapi import APIRouter, Depends, HTTPException, status +from sqlalchemy.orm import Session +from app.crud import item as crud +from app.schemas.item import Item, ItemCreate, ItemUpdate +from app.db.session import get_db + +router = APIRouter() + + +@router.get("/", response_model=List[Item]) +def read_items( + skip: int = 0, limit: int = 100, db: Session = Depends(get_db) +): + items = crud.get_items(db, skip=skip, limit=limit) + return items + + +@router.post("/", response_model=Item, status_code=status.HTTP_201_CREATED) +def create_item( + item: ItemCreate, db: Session = Depends(get_db) +): + return crud.create_item(db=db, item=item) + + +@router.get("/{item_id}", response_model=Item) +def read_item( + item_id: int, db: Session = Depends(get_db) +): + db_item = crud.get_item(db, item_id=item_id) + if db_item is None: + raise HTTPException(status_code=404, detail="Item not found") + return db_item + + +@router.put("/{item_id}", response_model=Item) +def update_item( + item_id: int, item: ItemUpdate, db: Session = Depends(get_db) +): + db_item = crud.update_item(db, item_id=item_id, item=item) + if db_item is None: + raise HTTPException(status_code=404, detail="Item not found") + return db_item + + +@router.delete("/{item_id}", response_model=Item) +def delete_item( + item_id: int, db: Session = Depends(get_db) +): + db_item = crud.delete_item(db, item_id=item_id) + if db_item is None: + raise HTTPException(status_code=404, detail="Item not found") + return db_item \ No newline at end of file diff --git a/app/api/health.py b/app/api/health.py new file mode 100644 index 0000000..68019af --- /dev/null +++ b/app/api/health.py @@ -0,0 +1,8 @@ +from fastapi import APIRouter + +router = APIRouter() + + +@router.get("/health", tags=["health"]) +def health_check(): + return {"status": "ok", "message": "API is running"} \ No newline at end of file diff --git a/app/crud/item.py b/app/crud/item.py new file mode 100644 index 0000000..ff3f0c9 --- /dev/null +++ b/app/crud/item.py @@ -0,0 +1,39 @@ +from sqlalchemy.orm import Session +from typing import List, Optional +from app.models.item import Item +from app.schemas.item import ItemCreate, ItemUpdate + + +def get_item(db: Session, item_id: int) -> Optional[Item]: + return db.query(Item).filter(Item.id == item_id).first() + + +def get_items(db: Session, skip: int = 0, limit: int = 100) -> List[Item]: + return db.query(Item).offset(skip).limit(limit).all() + + +def create_item(db: Session, item: ItemCreate) -> Item: + db_item = Item(**item.dict()) + db.add(db_item) + db.commit() + db.refresh(db_item) + return db_item + + +def update_item(db: Session, item_id: int, item: ItemUpdate) -> Optional[Item]: + db_item = get_item(db, item_id) + if db_item: + update_data = item.dict(exclude_unset=True) + for key, value in update_data.items(): + setattr(db_item, key, value) + db.commit() + db.refresh(db_item) + return db_item + + +def delete_item(db: Session, item_id: int) -> Optional[Item]: + db_item = get_item(db, item_id) + if db_item: + db.delete(db_item) + db.commit() + return db_item \ No newline at end of file diff --git a/app/db/base.py b/app/db/base.py new file mode 100644 index 0000000..1523192 --- /dev/null +++ b/app/db/base.py @@ -0,0 +1,3 @@ +# Import all the models, so that alembic can detect them +from app.db.base_class import Base +from app.models.item import Item \ No newline at end of file diff --git a/app/db/base_class.py b/app/db/base_class.py new file mode 100644 index 0000000..d0b33cc --- /dev/null +++ b/app/db/base_class.py @@ -0,0 +1,12 @@ +from typing import Any +from sqlalchemy.ext.declarative import as_declarative, declared_attr + + +@as_declarative() +class Base: + id: Any + __name__: str + + @declared_attr + def __tablename__(cls) -> str: + return cls.__name__.lower() \ No newline at end of file diff --git a/app/db/session.py b/app/db/session.py new file mode 100644 index 0000000..9351743 --- /dev/null +++ b/app/db/session.py @@ -0,0 +1,23 @@ +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker +from pathlib import Path + +# Create db directory if it doesn't exist +DB_DIR = Path("/app") / "storage" / "db" +DB_DIR.mkdir(parents=True, exist_ok=True) + +SQLALCHEMY_DATABASE_URL = f"sqlite:///{DB_DIR}/db.sqlite" + +engine = create_engine( + SQLALCHEMY_DATABASE_URL, + connect_args={"check_same_thread": False} +) +SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) + + +def get_db(): + db = SessionLocal() + try: + yield db + finally: + db.close() \ No newline at end of file diff --git a/app/models/item.py b/app/models/item.py new file mode 100644 index 0000000..8d8e8ff --- /dev/null +++ b/app/models/item.py @@ -0,0 +1,11 @@ +from sqlalchemy import Column, Integer, String, Text, DateTime +from sqlalchemy.sql import func +from app.db.base_class import Base + + +class Item(Base): + id = Column(Integer, primary_key=True, index=True) + title = Column(String(100), index=True) + description = Column(Text, nullable=True) + created_at = Column(DateTime(timezone=True), server_default=func.now()) + updated_at = Column(DateTime(timezone=True), onupdate=func.now()) \ No newline at end of file diff --git a/app/schemas/item.py b/app/schemas/item.py new file mode 100644 index 0000000..eb857a6 --- /dev/null +++ b/app/schemas/item.py @@ -0,0 +1,25 @@ +from pydantic import BaseModel +from datetime import datetime +from typing import Optional + + +class ItemBase(BaseModel): + title: str + description: Optional[str] = None + + +class ItemCreate(ItemBase): + pass + + +class ItemUpdate(ItemBase): + title: Optional[str] = None + + +class Item(ItemBase): + id: int + created_at: datetime + updated_at: Optional[datetime] = None + + class Config: + orm_mode = True \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..669c18b --- /dev/null +++ b/main.py @@ -0,0 +1,16 @@ +from fastapi import FastAPI +from app.api.endpoints import router as api_router +from app.api.health import router as health_router + +app = FastAPI( + title="Generic REST API", + description="A generic REST API built with FastAPI and SQLAlchemy", + version="0.1.0", +) + +app.include_router(api_router) +app.include_router(health_router) + +if __name__ == "__main__": + import uvicorn + uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..bdfbac8 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,6 @@ +fastapi==0.103.1 +uvicorn==0.23.2 +sqlalchemy==2.0.21 +pydantic==2.4.2 +alembic==1.12.0 +ruff==0.0.292 \ No newline at end of file