Skip to content

eugeneliukindev/FastAPI-Base-App-With-JWT

Repository files navigation

FastAPI Base App With JWT 🚀

A lightweight, ready-to-use template for building APIs with FastAPI. Get started in minutes without the hassle of complex setup!

Why Use This Starter Kit?

This template is designed to streamline your FastAPI development by eliminating:

  • 🛠️ Database configuration
  • ⚙️ Environment setup
  • 📂 Tedious project initialization and structure
  • 🗂️ Manual setup of SQLAlchemy models and Pydantic schemas

Features

  • Type safety with Mypy support
  • 🧹 Code quality ensured by Ruff linter and formatter
  • 📦 Dependency management via Poetry
  • 🗄️ Asynchronous SQLAlchemy with * AsyncPG* driver for high-performance work with * PostgreSQL*
  • 🐳 Dockerized Postgres for quick database setup
  • 🔄 Alembic migrations for seamless database schema management
  • 🔐 JWT authentication using RS256 keys
  • 🛡️ Pre-commit hooks using Ruff to automatically check code before commit

Getting Started

Follow these steps to set up and run the project locally.

Prerequisites

  • 🐍 Python 3.12+
  • 📦 Poetry for dependency management
  • 🐳 Docker (optional, for Postgres setup)

Installation

  1. Clone the Repository 📥

    Using HTTPS:

    git clone https://github.com/eugeneliukindev/FastAPI-Base-App-With-JWT

    Using SSH (recommended):

    git clone git@github.com:eugeneliukindev/FastAPI-Base-App-With-JWT
  2. Install Dependencies ⬇️

    This project uses Poetry for dependency management. See pyproject.toml for details.

    Optional: Configure Poetry to create a virtual environment in the project root:

    poetry config virtualenvs.in-project true

    Install dependencies:

    With dev dependencies (for development):

    poetry install

    Without dev dependencies (for production):

    poetry install --without dev
  3. Set Up the Database 🗄️

    Use the provided Docker Compose file to spin up a Postgres database:

    docker compose up -d

    Note: Ensure Docker is installed and running.

  4. Run Alembic Migrations 🔄

    Apply an existing database migration using Alembic:

    alembic upgrade head
  5. Start the Application 🚀

    Run the FastAPI server:

    python main.py

    Your API is now running! Access it at http://localhost:8000 (or the configured port).

Usage

  • 📚 API Documentation: Auto-generated by FastAPI at /docs (Swagger UI) or /redoc
  • 🛠️ Development Tools:
    • Run mypy . for type checking
    • Run ruff check . for linting
    • Run ruff format . for code formatting

Additional Notes

  • Important Note About .env

    The .env file is used to store sensitive data like database credentials. For demonstration purposes, it's not included in .gitignore. For security reasons, we strongly recommend adding .env to .gitignore to prevent accidentally committing sensitive data to the repository:

    echo ".env" >> .gitignore
  • Generate RSA keys for JWT (RS256) ⚠️

    Generate private key 🔑:

    openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:2048

    Extract the public key 🔑:

    openssl rsa -pubout -in private_key.pem -out public_key.pem

    Create a folder where these keys will be stored, for example /jwt-keys 📁 Next add this folder to .gitignore:

    echo "/jwt-keys" >> .gitignore

    These keys can be used to sign and verify JWT tokens.

    Do not use keys placed in the repository! This is done as an example!❗❗❗

  • Initializing pre-commit Hooks

    If you installed dev dependencies via poetry install, pre-commit was included in the installation. To activate it, run:

    pre-commit install

Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/YourFeature)
  3. Commit your changes (git commit -m 'Add YourFeature')
  4. Push to the branch (git push origin feature/YourFeature)
  5. Open a Pull Request

License

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


Happy coding! 🎉 If you have questions or need help, feel free to open an issue.

About

Starter template for creating API on FastAPI!

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published