Skip to content

OpenExt-Labs/python-fast-api-server

Repository files navigation

FastAPI Server Tutorial

This tutorial will guide you through the setup, installation, and usage of the FastAPI server with consistent response models.

Table of Contents

  1. Introduction
  2. Setup and Installation
  3. Response Models
  4. API Endpoints
  5. Running the Server
  6. Testing the Endpoints

Introduction

This FastAPI server uses a consistent response structure for all API endpoints, ensuring that responses always include error, message, and data fields. This structure improves the API's usability and makes it easier to handle responses on the client side.

Setup and Installation

Prerequisites

  • Python 3.8 or higher
  • Poetry for dependency management

Installation

  1. Clone the repository:

    git clone https://github.com/yourusername/fastapi-server.git
    cd fastapi-server
  2. Install dependencies:

    poetry install
  3. Create a .env file:

    touch .env.local

    Example .env.local content:

    MYSQL_ROOT_PASSWORD=
    MYSQL_DATABASE=
    MYSQL_USER=
    MYSQL_PASSWORD=
    MYSQL_HOST=
    MYSQL_PORT=
    PROFILER_SECRET_P=9383273
    
    ACCESS_TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJzYW5nbGUxIiwiZXhwIjoxNzE4MjA0OTE3fQ.rH7Ux72gfZCCfErDW_YzdG4A9BhOidFszPRoFN-w6K0
    INVALID_TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.z-jTfyTJX1V3kEfdiq824clRo_WbSmuPhm5mLvu5F5E

Running the Server

  1. Run the FastAPI server:

    poetry run uvicorn src.main:app --reload
    • The server will start running at http://localhost:8000.
    • You can access the API documentation at http://localhost:8000/docs.
    • Profiler will be available at http://localhost:8000/profiler?p=.
  2. Run the FastAPI server with Docker:

    docker-compose up
    • The server will start running at http://localhost:8000.
    • You can access the API documentation at http://localhost:8000/docs.
    • Profiler will be available at http://localhost:8000/profiler?p=.

Lint

  1. Run the linter:
    poetry run autopep8 --in-place --aggressive --aggressive **/*.py