1- <p align =" center " >
2- <img src =" https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png " alt =" FAST API " />
3- <h2 align =" center " > FastAPI Template </h2 >
4- <h4 align =" center " > A template for beginners </h4 >
1+ # FastAPI SQLAlchemy PostgreSQL Template
52
6- ---
7- ## About
8- This is a beginner-friendly template for getting started with FastAPI and SQLAlchemy.
3+ Minimal starter template for a CRUD API with FastAPI + SQLAlchemy + PostgreSQL.
4+
5+ [ ![ Python] ( https://img.shields.io/badge/Python-3.10+-3776AB?style=for-the-badge&logo=python&logoColor=white )] ( https://www.python.org/ )
6+ [ ![ FastAPI] ( https://img.shields.io/badge/FastAPI-0.135-009688?style=for-the-badge&logo=fastapi&logoColor=white )] ( https://fastapi.tiangolo.com/ )
7+ [ ![ SQLAlchemy] ( https://img.shields.io/badge/SQLAlchemy-2.0-D71F00?style=for-the-badge&logo=sqlalchemy&logoColor=white )] ( https://www.sqlalchemy.org/ )
8+ [ ![ PostgreSQL] ( https://img.shields.io/badge/PostgreSQL-16-336791?style=for-the-badge&logo=postgresql&logoColor=white )] ( https://www.postgresql.org/ )
9+ [ ![ Pytest] ( https://img.shields.io/badge/Pytest-9.0-0A9EDC?style=for-the-badge&logo=pytest&logoColor=white )] ( https://docs.pytest.org/ )
910
1011## Features
11- - [x] Database connection using SQLAlchemy
12- - [x] FastAPI server
13- - [x] Unit testing with PyTest
14- - [x] Basic CRUD for posts
12+
13+ - FastAPI app with CORS enabled
14+ - SQLAlchemy ORM integration
15+ - Postgres-ready configuration via ` DATABASE_URL `
16+ - CRUD endpoints for ` posts `
17+ - Pytest suite (DB mocked for local test runs)
18+ - Docker + Docker Compose local setup
1519
1620## Requirements
21+
1722- Python 3.10+
1823- ` pip `
19- - PostgreSQL database
24+ - PostgreSQL (only needed when running app without Docker)
2025
21- ## Setup
22- 1 . Create and activate a virtual environment:
26+ ## Local Setup (Without Docker)
27+
28+ 1 . Create and activate venv:
2329
2430``` bash
2531python3 -m venv .venv
@@ -32,58 +38,56 @@ source .venv/bin/activate
3238pip install -r requirements.txt
3339```
3440
35- 3 . Set environment variables:
36-
37- | Key | Value |
38- | --- | --- |
39- | ` DATABASE_URL ` | ` postgresql://user:password@host:port/db ` |
40-
41- Example (` .env ` ):
41+ 3 . Add env vars:
4242
4343``` env
4444DATABASE_URL=postgresql://postgres:postgres@localhost:5432/fastapi_template
4545```
4646
47- 4 . Run the API:
47+ 4 . Run API:
4848
4949``` bash
5050uvicorn main:app --reload
5151```
5252
53- ## API Docs (Swagger / OpenAPI)
54- Once the app is running locally, open:
55- - Swagger UI: ` http://127.0.0.1:8000/docs `
56- - ReDoc: ` http://127.0.0.1:8000/redoc `
57- - OpenAPI JSON: ` http://127.0.0.1:8000/openapi.json `
53+ ## API Docs
54+
55+ When the server is running:
56+
57+ - Swagger UI: [ http://127.0.0.1:8000/docs ] ( http://127.0.0.1:8000/docs )
58+ - ReDoc: [ http://127.0.0.1:8000/redoc ] ( http://127.0.0.1:8000/redoc )
59+ - OpenAPI JSON: [ http://127.0.0.1:8000/openapi.json ] ( http://127.0.0.1:8000/openapi.json )
60+
61+ ## Tests
5862
59- ## Running tests
6063``` bash
6164pytest
6265```
6366
64- ## Formatting (Black)
65- Run formatter:
67+ ## Formatting
6668
6769``` bash
6870black .
6971```
7072
71- ## Docker (Local)
72- 1 . Create ` .env ` from example:
73+ ## Docker (Recommended for Local Run)
74+
75+ 1 . Create ` .env ` :
7376
7477``` bash
7578cp .env.example .env
7679```
7780
78- 2 . Start API + Postgres with Docker Compose :
81+ 2 . Build and start :
7982
8083``` bash
8184docker compose up --build
8285```
8386
84- 3 . API will be available at:
85- - ` http://127.0.0.1:8000 `
86- - Swagger: ` http://127.0.0.1:8000/docs `
87- - ReDoc: ` http://127.0.0.1:8000/redoc `
87+ 3 . Access:
88+
89+ - API: [ http://127.0.0.1:8000 ] ( http://127.0.0.1:8000 )
90+ - Swagger: [ http://127.0.0.1:8000/docs ] ( http://127.0.0.1:8000/docs )
91+ - ReDoc: [ http://127.0.0.1:8000/redoc ] ( http://127.0.0.1:8000/redoc )
8892
89- ` DATABASE_URL ` is passed to the API container from ` .env ` through ` docker-compose.yml ` .
93+ ` DATABASE_URL ` is passed to the API container from ` docker-compose.yml ` and can be overridden via ` .env ` .
0 commit comments