Skip to content

Fastapi blog with jwt authentication, tests and alembic (Using pydantic and sqlalchemy v2)

License

Notifications You must be signed in to change notification settings

KiyoshiSama/fastapi-blog-sqlalchemy-v2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FastAPI Blog with SQLAlchemy

This repository contains a full-featured blogging application built with FastAPI, SQLAlchemy, and PostgreSQL. The application is designed to provide a foundation for a modern web application, including user authentication, CRUD operations for blog posts, and more.

Table of Contents

Features

  • User Authentication: Secure user authentication with password hashing.
  • CRUD Operations: Create, read, update, and delete posts and user profiles.
  • Dependency Injection: Efficient dependency management using FastAPI's dependency injection system.
  • Asynchronous Support: Leverages Python's asyncio for non-blocking I/O operations.
  • Database Integration: Uses SQLAlchemy as the ORM and PostgreSQL as the database.
  • Testing: Comprehensive test suite using pytest, including fixtures for setup and teardown.

Installation

To get started with this project, follow the steps below:

Prerequisites

  • Python 3.9+
  • PostgreSQL
  • virtualenv (optional but recommended)

Clone the Repository

git clone https://github.com/KiyoshiSama/fastapi-blog-sqlalchemy-v2.git
cd fastapi-blog-sqlalchemy-v2

Create and Activate a Virtual Environment

python -m venv venv
source venv/bin/activate  # On Windows use `venv\Scripts\activate`

Install Dependencies

pip install -r requirements.txt

Running the Application

Before running the application, you need to set up the database and configure environment variables.

Database Setup

  1. Ensure PostgreSQL is running on your machine.
  2. Create a new PostgreSQL database.
psql -U postgres
CREATE DATABASE fastapi_blog;

Environment Variables

Create a ".env" file in the root directory of the project and add the following configuration. Adjust the values according to your setup:

DATABASE_URL=postgresql+asyncpg://<username>:<password>@localhost:5432/fastapi_blog
SECRET_KEY=<your_secret_key>
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30

Run Database Migrations

alembic upgrade head

Run the Application

uvicorn app.main:app --reload

Running Tests

This project uses "pytest" for testing. To run the tests, use the following command:

pytest

Licence

This project is licensed under the MIT License. See the LICENSE file for more information.