Skip to content

jpcadena/fastapi-boilerplate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fastapi-boilerplate


Logo

Fastapi boilerplate

Fastapi boilerplate
Explore the docs »

Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. API Documentation
  5. OpenAPI TypeScript Codegen
  6. Contributing
  7. Security
  8. Code of Conduct
  9. License
  10. Contact

About The Project

Project

This backend project is a FastAPI-based template designed to serve as a robust, reliable, and RESTful API backend. It is crucial for facilitating user authentication, real-time monitoring, data processing, and an advanced alerting system. By leveraging the principles of RESTful architecture, this API ensures standardized communication and a scalable, maintainable infrastructure for the associated mobile application. This project is not only vital for the functionality it provides but also for ensuring the scalability and maintainability of the overall solution.

(back to top)

Built with

This project is built using a suite of modern, powerful technologies and frameworks to ensure high performance, ease of development, and robustness:

Python FastAPI Pydantic Starlette Uvicorn postgresql Redis JWT HTML5 CSS3 isort Black Ruff MyPy pre-commit GitHub Actions Poetry Pycharm Visual Studio Code Markdown Swagger UI License: MIT

Components

The system is composed of the following main components, each playing a crucial role in providing a scalable, maintainable, and robust application:

  • FastAPI Backend: Serves as the core of the RESTful API, handling incoming HTTP requests, processing data, and sending responses. It's designed for high performance and encourages standard API development practices.
  • PostgreSQL & Redis: PostgreSQL is used for structured relational data storage, while Redis serves as a fast, in-memory data store for caching and temporary data to enhance performance.
  • JWT Authentication: Implements secure authentication mechanisms using JSON Web Tokens (JWT), ensuring secure access to the API.

This project adheres to RESTful design principles, ensuring an intuitive and standard approach to API development. This design facilitates easy integration, scalability, and interaction with various clients, including web and mobile applications. The API provides clear, resource-oriented URLs, uses HTTP response codes to indicate API errors, and employs JWT for secure, stateless authentication.

(back to top)

Getting started

Prerequisites

Installation

  1. Clone the repository

    git clone https://github.com/jpcadena/fastapi-boilerplate.git
  2. Change the directory to root project

    cd fastapi-boilerplate
  3. Install Poetry package manager

    pip install poetry
  4. Install the project's dependencies

    poetry install
  5. Activate the environment

    poetry shell

(back to top)

Usage

  1. Setting up environment variables:

    If you find a .env.sample in the project directory, make a copy of it and rename to .env.

    cp .env.sample .env

    This .env file will be used to manage your application's environment variables.

  2. Configuring your credentials:

    Open the .env file in a text editor and replace the placeholder values with your actual credentials.

    # .env file
    POSTGRES_USER=your_database_user
    SECRET_KEY=your_api_key

    Be sure to save the file after making these changes.

  3. Generating RSA keys

    To ensure secure communication in this project, RSA keys are used. Before running the application, you need to generate a public and private RSA key pair. We've provided a Python script to automatically generate these keys. You can find the script at app\services\infrastructure\encryption.py. To generate your keys, simply run:

    python app\services\infrastructure\encryption.py.py

    This will create public_key.pem and private_key.pem files in your specified directory.

    Once the keys are generated, the application will use them for cryptographic operations. Ensure that these files are kept secure and are not exposed publicly. The default configuration expects these keys in the root directory of the project.

  4. Starting the server:

    To start the local server on your machine, run the following command in your terminal:

    uvicorn main:app --reload

    The --reload flag enables hot reloading, which means the server will automatically update whenever you make changes to the code.

  5. Interacting with the app:

    Once your server is running, you can interact with it using any API client like Postman or your web browser. You can send GET, POST, PUT, DELETE requests to the API endpoints as defined in your main.py file. For example, to get all users, you can send a GET request to http://localhost:8000/api/v1/users.

  6. Using Swagger UI:

    FastAPI provides automatic interactive API documentation using Swagger UI. Once your server is up and running, you can go to http://localhost:8000/docs in your web browser to access it. From there, you can explore and interact with your API directly.

(back to top)

OpenAPI TypeScript Codegen

To facilitate the interaction between the frontend and the API, we use the OpenAPI TypeScript Codegen to generate TypeScript models and APIs based on our OpenAPI specification file.

React Native Installation

First, you need to install the OpenAPI TypeScript Codegen as a devDependency. In your project directory, run:

npm install openapi-typescript-codegen --save-dev

Codegen Usage

Add the following to your package.json file:

"generate-client": "openapi --input C:/Users/user/fastapi-boilerplate/openapi.json --output . /src/client --client axios"

To generate the TypeScript code, you need to use the openapi.json specification file. Run the following command in your terminal:

npm run generate-client

(back to top)

Contributing

GitHub

Please read our contributing guide for details on our code of conduct, and the process for submitting pull requests to us.

(back to top)

Security

For security considerations and best practices, please refer to our Security Guide for a detailed guide.

(back to top)

Code of Conduct

We enforce a code of conduct for all maintainers and contributors. Please read our Code of Conduct to understand the expectations before making any contributions.

(back to top)

License

Distributed under the MIT License. See LICENSE for more information.

(back to top)

Contact

  • LinkedIn

  • Outlook

(back to top)