A modern β‘ FastAPI-powered backend for Cartify β a lightweight π e-commerce API with PostgreSQL π, Docker π³, and a scalable architecture π.
- A small FastAPI app serving a couple of endpoints (see
app/main.py). - A startup DB connectivity check: when the app boots it runs a trivial
SELECT 1against your database and stores the result inapp.state.db_connected. - A simple health endpoint at
/healthand a welcome endpoint at/.
- FastAPI for fast, async-capable HTTP APIs
- SQLAlchemy for DB models and schema creation
- Docker-friendly (there's a
docker-compose.ymlat project root)
- Python 3.10+ (3.11 recommended)
- PostgreSQL (local or remote) or a running PostgreSQL service via Docker
- Docker & Docker Compose (if you prefer containerized setup)
Recommended Python packages (example):
- fastapi
- uvicorn[standard]
- sqlalchemy
- psycopg2-binary
You can install them locally with:
python -m venv .venv
source .venv/bin/activate
pip install fastapi uvicorn[standard] sqlalchemy psycopg2-binaryThe app reads its DB URL from the typical DATABASE_URL environment variable. Example values (replace with real credentials):
DATABASE_URL=postgresql://cartify_user:secretpassword@localhost:5432/cartify_dbIf you run with Docker Compose, you can place environment variables into a .env file and Docker Compose will pick it up.
This project includes docker-compose.yml. The simplest way to bring up the app and a Postgres DB is:
# build and start containers
docker compose up --build -d
# watch logs (optional)
docker compose logs -fThe API will be reachable at http://localhost:8000 by default (if the compose file maps that port).
The service will attempt a DB connection on startup and log whether it succeeded. You can verify via the health endpoint:
curl -s http://localhost:8000/health | jqExpected JSON shape:
{
"status": "ok",
"db_connected": true
}If db_connected is false, check database credentials and that Postgres is reachable from the app container or host.
- Create and activate a virtual environment (see earlier).
- Export your
DATABASE_URLvalue in the shell:
export DATABASE_URL="postgresql://cartify_user:secretpassword@localhost:5432/cartify_db"- Start the app with Uvicorn:
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000Visit http://127.0.0.1:8000/ for the welcome endpoint and http://127.0.0.1:8000/health for the health check.
If you prefer using the included Makefile to simplify local setup and running, follow these steps:
make help # see commands
make venv # create .venv
source .venv/bin/activate
make install # install deps
make db-up # start Postgres + Mailhog
make run # run FastAPIThis runs the sequence: create a virtualenv, activate it, install dependencies, start local services via docker-compose (Postgres + Mailhog), and run the FastAPI app with Uvicorn.
GET /β welcome endpoint. Returns a friendly message and the DB connection status.GET /healthβ returns{ "status": "ok", "db_connected": <true|false> }.
- If the app logs show
Database connection failed during startup, double-checkDATABASE_URL, network connectivity, and that Postgres is accepting connections from the app (check host/port and firewall rules). - When using Docker, ensure the DB container is fully ready. You can inspect DB logs with
docker compose logs db(container name may vary).
- Add a
requirements.txtorpyproject.tomlfor dependency management. - Add Alembic for migrations and document how to run them.
- Add tests and a CI pipeline to validate the health and DB connectivity on push.
This project does not include a license file. Add one (for example, an MIT license) if you intend to publish.
# Cartify
A modern β‘ FastAPI-powered backend for Cartify β a lightweight π e-commerce API with PostgreSQL π, Docker π³, and a scalable architecture π.
## What this repo contains
- A small FastAPI app serving a couple of endpoints (see `app/main.py`).
- A startup DB connectivity check: when the app boots it runs a trivial `SELECT 1` against your database and stores the result in `app.state.db_connected`.
- A simple health endpoint at `/health` and a welcome endpoint at `/`.
## Features
- FastAPI for fast, async-capable HTTP APIs
- SQLAlchemy for DB models and schema creation
- Docker-friendly (there's a `docker-compose.yml` at project root)
## Requirements
- Python 3.10+ (3.11 recommended)
- PostgreSQL (local or remote) or a running PostgreSQL service via Docker
- Docker & Docker Compose (if you prefer containerized setup)
Recommended Python packages (example):
- fastapi
- uvicorn[standard]
- sqlalchemy
- psycopg2-binary
You can install them locally with:
```zsh
python -m venv .venv
source .venv/bin/activate
pip install fastapi uvicorn[standard] sqlalchemy psycopg2-binary
The app reads its DB URL from the typical DATABASE_URL environment variable. Example values (replace with real credentials):
DATABASE_URL=postgresql://cartify_user:secretpassword@localhost:5432/cartify_dbIf you run with Docker Compose, you can place environment variables into a .env file and Docker Compose will pick it up.
This project includes docker-compose.yml. The simplest way to bring up the app and a Postgres DB is:
# build and start containers
docker compose up --build -d
# watch logs (optional)
docker compose logs -fThe API will be reachable at http://localhost:8000 by default (if the compose file maps that port).
The service will attempt a DB connection on startup and log whether it succeeded. You can verify via the health endpoint:
curl -s http://localhost:8000/health | jqExpected JSON shape:
{
"status": "ok",
"db_connected": true
}If db_connected is false, check database credentials and that Postgres is reachable from the app container or host.
- Create and activate a virtual environment (see earlier).
- Export your
DATABASE_URLvalue in the shell:
export DATABASE_URL="postgresql://cartify_user:secretpassword@localhost:5432/cartify_db"- Start the app with Uvicorn:
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000Visit http://127.0.0.1:8000/ for the welcome endpoint and http://127.0.0.1:8000/health for the health check.
If you prefer using the included Makefile to simplify local setup and running, follow these steps:
make help # see commands
make venv # create .venv
source .venv/bin/activate
make install # install deps
make db-up # start Postgres + Mailhog
make run # run FastAPIThis runs the sequence: create a virtualenv, activate it, install dependencies, start local services via docker-compose (Postgres + Mailhog), and run the FastAPI app with Uvicorn.
GET /β welcome endpoint. Returns a friendly message and the DB connection status.GET /healthβ returns{ "status": "ok", "db_connected": <true|false> }.
- If the app logs show
Database connection failed during startup, double-checkDATABASE_URL, network connectivity, and that Postgres is accepting connections from the app (check host/port and firewall rules). - When using Docker, ensure the DB container is fully ready. You can inspect DB logs with
docker compose logs db(container name may vary).
- Add a
requirements.txtorpyproject.tomlfor dependency management. - Add Alembic for migrations and document how to run them.
- Add tests and a CI pipeline to validate the health and DB connectivity on push.
This project does not include a license file. Add one (for example, an MIT license) if you intend to publish.