Skip to content

About

REST API for a social app with JWT auth, built with FastAPI, PostgreSQL and Docker

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

14 Commits

Folders and files

Repository files navigation

fastapi-learn

Python FastAPI PostgreSQL Docker

A REST API for a simple social media app, built while learning backend development with FastAPI. Users can sign up, log in, create posts, and vote on posts.

Features

  • User accounts with passwords hashed with bcrypt (passlib)
  • JWT authentication: log in to get a token, then send it with every request
  • Posts: create, read, update and delete (only the owner can edit or delete a post)
  • Votes: like or unlike a post, with vote counts included when you fetch posts
  • Search and pagination on posts (search, limit, skip)
  • Database migrations with Alembic
  • Docker setup with the API and PostgreSQL in separate containers
  • Interactive API docs generated automatically by FastAPI

Tech stack

Part Tool
Web framework FastAPI
Database PostgreSQL
ORM SQLAlchemy
Migrations Alembic
Validation Pydantic
Auth python-jose (JWT), passlib
Deployment Docker, Docker Compose

API endpoints

Method Endpoint Description Auth
POST /users/ Create an account
GET /users/{id} Get a user
POST /auth/login Log in and get an access token
GET /posts/ List your posts with vote counts 🔒
GET /posts/{id} Get one post with its vote count 🔒
POST /posts/ Create a post 🔒
PUT /posts/{id} Update your post 🔒
DELETE /posts/{id} Delete your post 🔒
POST /vote/ Vote (dir: 1) or remove your vote (dir: 0) 🔒

🔒 = needs the header Authorization: Bearer <token>

Running it

1. Create a .env file

DATABASE_HOSTNAME=postgres
DATABASE_PORT=5432
DATABASE_USERNAME=postgres
DATABASE_PASSWORD=postgres123
DATABASE_NAME=fastapi
SECRET_KEY=change-me-to-a-long-random-string
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30

Use DATABASE_HOSTNAME=localhost instead if you run the API without Docker.

2a. With Docker

docker compose up --build

2b. Without Docker

You need a running PostgreSQL database first.

python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
alembic upgrade head
uvicorn app.main:app --reload

3. Try it

Open http://localhost:8000/docs to see every endpoint and test it in the browser.

Project structure

app/
├── main.py        # App setup, CORS, routers
├── config.py      # Settings loaded from .env
├── database.py    # Database connection
├── models.py      # SQLAlchemy tables: User, Post, Vote
├── schemas.py     # Pydantic request/response models
├── oauth2.py      # JWT creation and checking
├── utils.py       # Password hashing
└── routers/       # auth, user, post, vote
alembic/           # Database migrations

About

REST API for a social app with JWT auth, built with FastAPI, PostgreSQL and Docker

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages