/inventory-tracking

back-end for tracking items in an inventory

Primary LanguagePython

Inventory Management

Build and Test

Interactive API at: www.inventoryapi.ducnguyen.dev/docs

๐Ÿ’ป Back-end Tech Stack

  • Python 3.8
  • FastAPI
  • MongoDB
  • Docker and Compose

๐ŸŒŸ Features

For clients:

  • CRUD Functionality. Users are able to:
    • Create new item in the inventory
    • View all items in the inventory
    • View item by ID
    • Delete item by ID
    • Update item by ID
  • Export all products in the inventory to a CSV file.

For developers:

  • Swagger UI auto generated from OpenAPI, allows visualizing and interacting with the API without a front-end.
  • Async functionalities.
  • Easy, extendable models and schemas.
  • Schemas are validated by Pydantic.
  • Wrapped ObjectID (_id field) from MongoDB by a custom field, compatible with openapi standard. More information here.
  • Environment variables are parsed by .env, validated by Pydantic.
  • Containerized with Docker and Compose for easy deployment.
  • pre-commit hook for auto linting before git commit.
  • Api tests done by Pytest and Tavern.
  • CI runs tests on push by GitHub Actions.

๐Ÿ› ๏ธ Setup

Clone the repo: git clone https://github.com/DukeNgn/inventory-tracking.git

.env

  • cp example.env .env to create environment file.
  • Change the content of the .env file to the local environment. For example:
    APP_NAME="Inventory Tracking API"
    MONGO_URI=mongodb://localhost:27017
    
    is a valid configuration for the default MongoDB.

Build using docker and docker-compose (recommended):

  • docker-compose up --build -d to start building the containers in the background.
  • Go to localhost:8000/docs to access the interactive Swagger UI of the API.

Build without Docker:

  • Install poetry - the Python dependency management.
  • poetry shell to create a virtual environment of the project.
  • poetry install to install dependencies.
  • uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload to start the API service in development mode (hot reload).
  • Go to localhost:8000/docs to access the interactive Swagger UI of the API.

While developing, run pre-commit install before committing any changes. pre-commit runs a style check through all changed files before committing. The rules of pre-commits are in .pre-commit-config.yml file.

๐Ÿงช Testing

With Docker container built:

  • chmod +x run_tests.sh && ./run_tests.sh to run tests inside the container.

Local environment:

  • Start the app on local port 8000 (refer to the build instruction above).
  • chmod +x run_tests.sh && ./run_tests.sh to run tests using script.
  • Without script, tests can be ran with command pytest app/tests.

๐Ÿ—‚๏ธ Directory Structure

  • app is the main directory containing all the source code.
  • ./app/main.py: the main start up file.
  • ./run_tests.sh: the script file to run tests.
  • Each of the subfolder in ./app has a README.md for more details.

๐Ÿณ Deployment

The project is containerized and deployed with docker-compose on my personal Digital Ocean VM.