Skip to content

Latest commit

 

History

History
89 lines (66 loc) · 1.63 KB

File metadata and controls

89 lines (66 loc) · 1.63 KB

FAST API

FastAPI Template

A template for beginners


About

This is a beginner-friendly template for getting started with FastAPI and SQLAlchemy.

Features

  • Database connection using SQLAlchemy
  • FastAPI server
  • Unit testing with PyTest
  • Basic CRUD for posts

Requirements

  • Python 3.10+
  • pip
  • PostgreSQL database

Setup

  1. Create and activate a virtual environment:
python3 -m venv .venv
source .venv/bin/activate
  1. Install dependencies:
pip install -r requirements.txt
  1. Set environment variables:
Key Value
DATABASE_URL postgresql://user:password@host:port/db

Example (.env):

DATABASE_URL=postgresql://postgres:postgres@localhost:5432/fastapi_template
  1. Run the API:
uvicorn main:app --reload

API Docs (Swagger / OpenAPI)

Once the app is running locally, open:

  • Swagger UI: http://127.0.0.1:8000/docs
  • ReDoc: http://127.0.0.1:8000/redoc
  • OpenAPI JSON: http://127.0.0.1:8000/openapi.json

Running tests

pytest

Formatting (Black)

Run formatter:

black .

Docker (Local)

  1. Create .env from example:
cp .env.example .env
  1. Start API + Postgres with Docker Compose:
docker compose up --build
  1. API will be available at:
  • http://127.0.0.1:8000
  • Swagger: http://127.0.0.1:8000/docs
  • ReDoc: http://127.0.0.1:8000/redoc

DATABASE_URL is passed to the API container from .env through docker-compose.yml.