Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Inventory Management API

Production-ready RESTful API built with FastAPI, SQLAlchemy, and MySQL. It provides JWT-protected CRUD operations for product inventory, containerized deployment with Docker, and first-class developer ergonomics (typed schemas, env-based config, automatic docs).

Features

  • FastAPI app with automatic Swagger UI at /docs
  • JWT authentication with secure password hashing (Passlib + bcrypt)
  • SQLAlchemy ORM models backed by MySQL
  • Modular routing (users, products) and centralized database session handling
  • Ready-to-run Dockerfile and docker-compose stack (API + MySQL)
  • Environment-based configuration via .env

Tech Stack

  • Python 3.11
  • FastAPI, Uvicorn, SQLAlchemy 2.x, Pydantic v2
  • MySQL 8
  • Docker & docker-compose

Getting Started

1. Create & activate a virtual environment

python -m venv venv
# Windows
venv\Scripts\activate
# macOS/Linux
source venv/bin/activate

2. Install dependencies

pip install -r requirements.txt

3. Configure environment variables

Copy .env (already included) or override via shell variables. Minimum required values:

DATABASE_URL=mysql+pymysql://<user>:<password>@<host>:3306/<db>
SECRET_KEY=<random string>
ACCESS_TOKEN_EXPIRE_MINUTES=30

4. Start the API locally

uvicorn app.main:app --reload

Access docs at http://localhost:8000/docs. Tables are auto-created on startup via SQLAlchemy Base.metadata.create_all, but ensure the configured database exists first.

Docker Workflow

docker-compose up --build

This brings up inventory_api (FastAPI) and inventory_db (MySQL 8) with persistent storage in the db_data volume.

API Overview

  • POST /users/register – create an account
  • POST /users/login – obtain JWT (OAuth2 password flow)
  • GET /users/me – fetch current user profile
  • POST /products/ – create product (auth required)
  • GET /products/ – list authenticated user's products
  • GET /products/{id} – fetch single product
  • PUT /products/{id} – update product
  • DELETE /products/{id} – delete product

All product routes require an Authorization: Bearer <token> header.

Testing the Flow

  1. Register a user via /users/register.
  2. Authenticate via /users/login using username & password (form-data) to receive a token.
  3. Authorize subsequent product requests with the returned token.

Project Structure

app/
  +-- auth.py          # JWT helpers + dependencies
  +-- database.py      # SQLAlchemy engine/session
  +-- main.py          # FastAPI entrypoint
  +-- models.py        # ORM models
  +-- schemas.py       # Pydantic models
  +-- routes/
      +-- users.py     # register/login/me endpoints
      +-- products.py  # JWT-protected CRUD operations

Next Steps / Enhancements

  1. Add middleware for structured logging & request tracing.
  2. Introduce domain-driven modules (clean architecture) if business logic grows.
  3. Write pytest suites (unit + integration) using TestClient and a transient database.
  4. Wire CI (GitHub Actions) for lint/test before deployment.

License

MIT (adjust as needed).

About

Production-ready RESTful API built with FastAPI, SQLAlchemy, and MySQL. It provides JWT-protected CRUD operations for product inventory, containerized deployment with Docker, and first-class developer ergonomics (typed schemas, env-based config, automatic docs).

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages