Skip to content

krishnabaishnab/TemplatesFastAPI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ FastAPI Template

Use this template

A production-ready FastAPI template with MongoDB integration, JWT authentication, and LLM support. Perfect for building scalable APIs with modern Python practices.

✨ Features

  • FastAPI Backend - Modern, fast web framework for building APIs
  • MongoDB Integration - Async database operations with Motor
  • JWT Authentication - Secure user authentication and authorization
  • LLM Integration - Support for OpenAI and Gemini APIs
  • Docker Support - Containerized development and deployment
  • Comprehensive Testing - Pytest setup with async support
  • Production Ready - Security, CORS, environment configuration
  • API Documentation - Automatic Swagger/OpenAPI docs at /docs

πŸ—οΈ Project Structure

β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ api/
β”‚   β”‚   β”œβ”€β”€ deps.py              # Dependencies and authentication
β”‚   β”‚   └── v1/
β”‚   β”‚       β”œβ”€β”€ api.py           # Main API router
β”‚   β”‚       └── routes/
β”‚   β”‚           β”œβ”€β”€ auth.py      # Authentication endpoints
β”‚   β”‚           β”œβ”€β”€ user.py      # User management endpoints
β”‚   β”‚           └── llm.py       # LLM integration endpoints
β”‚   β”œβ”€β”€ core/
β”‚   β”‚   β”œβ”€β”€ config.py            # Settings and configuration
β”‚   β”‚   └── security.py          # JWT and password utilities
β”‚   β”œβ”€β”€ db/
β”‚   β”‚   β”œβ”€β”€ mongodb.py           # Database connection
β”‚   β”‚   └── crud/
β”‚   β”‚       └── user.py          # User CRUD operations
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   └── user.py              # Pydantic models
β”‚   β”œβ”€β”€ schemas/
β”‚   β”‚   └── auth.py              # Request/response schemas
β”‚   └── services/
β”‚       β”œβ”€β”€ auth_service.py      # Authentication business logic
β”‚       └── llm_client.py        # LLM integration service
β”œβ”€β”€ tests/
β”‚   β”œβ”€β”€ conftest.py              # Pytest configuration
β”‚   β”œβ”€β”€ test_auth.py             # Authentication tests
β”‚   └── test_users.py            # User endpoint tests
β”œβ”€β”€ main.py                      # FastAPI application entry point
β”œβ”€β”€ requirements.txt             # Python dependencies
β”œβ”€β”€ Dockerfile                   # Production Docker image
β”œβ”€β”€ docker-compose.yml           # Local development setup
β”œβ”€β”€ env.example                  # Environment variables template
└── README.md                    # This file

πŸš€ Quick Start

Prerequisites

  • Python 3.11+
  • MongoDB (local or cloud)
  • Docker & Docker Compose (optional)

Local Development

  1. Clone the repository

    git clone https://github.com/krishnakamalbaishnab/TemplatesFastAPI.git
    cd TemplatesFastAPI
  2. Set up environment variables

    cp env.example .env
    # Edit .env with your configuration
  3. Install dependencies

    pip install -r requirements.txt
  4. Start MongoDB (if using local MongoDB)

    # Using Docker
    docker run -d -p 27017:27017 --name mongodb mongo:7.0
    
    # Or install MongoDB locally
  5. Run the application

    uvicorn main:app --reload
  6. Access the API

Using Docker Compose

  1. Start all services

    docker-compose up -d
  2. Access services

  3. View logs

    docker-compose logs -f app

πŸ”§ Configuration

Environment Variables

Copy env.example to .env and configure:

# Security
SECRET_KEY=your-secret-key-here
ACCESS_TOKEN_EXPIRE_MINUTES=30

# MongoDB
MONGODB_URL=mongodb://localhost:27017
MONGODB_DB_NAME=fastapi_template

# LLM Settings
OPENAI_API_KEY=your-openai-api-key
GEMINI_API_KEY=your-gemini-api-key
LLM_PROVIDER=openai  # or "gemini"

# CORS
BACKEND_CORS_ORIGINS=["http://localhost:3000"]

πŸ“š API Endpoints

Authentication

  • POST /api/v1/auth/register - Register new user
  • POST /api/v1/auth/login - Login with form data
  • POST /api/v1/auth/login-json - Login with JSON

Users

  • GET /api/v1/users/me - Get current user (authenticated)
  • PUT /api/v1/users/me - Update current user (authenticated)
  • GET /api/v1/users/ - Get all users (superuser only)
  • GET /api/v1/users/{user_id} - Get user by ID (superuser only)
  • PUT /api/v1/users/{user_id} - Update user (superuser only)
  • DELETE /api/v1/users/{user_id} - Delete user (superuser only)

LLM Integration

  • POST /api/v1/llm/generate - Generate text (authenticated)
  • POST /api/v1/llm/chat - Chat completion (authenticated)
  • GET /api/v1/llm/models - Get available models (authenticated)

πŸ§ͺ Testing

Run the test suite:

# Run all tests
pytest

# Run with coverage
pytest --cov=app

# Run specific test file
pytest tests/test_auth.py

# Run with verbose output
pytest -v

🐳 Docker Deployment

Build and run with Docker

# Build the image
docker build -t fastapi-template .

# Run the container
docker run -p 8000:8000 --env-file .env fastapi-template

Production deployment

  1. Build production image

    docker build -t fastapi-template:prod .
  2. Run with production settings

    docker run -d \
      --name fastapi-app \
      -p 8000:8000 \
      --env-file .env.prod \
      fastapi-template:prod

πŸ”’ Security Features

  • JWT Authentication - Secure token-based authentication
  • Password Hashing - BCrypt password hashing
  • CORS Protection - Configurable CORS settings
  • Input Validation - Pydantic model validation
  • Environment Configuration - Secure configuration management

🀝 Contributing

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

πŸ“„ License

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

πŸ™ Acknowledgments

πŸ“ž Support

If you have any questions or need help, please open an issue on GitHub.


Made with ❀️ for the FastAPI community

About

A FastAPI starter template with JWT auth, MongoDB, and LLM integration

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published