Skip to content

Commit d8a8ecf

Browse files
committed
Commit
1 parent eb7d2b4 commit d8a8ecf

1 file changed

Lines changed: 112 additions & 179 deletions

File tree

README.md

Lines changed: 112 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -1,240 +1,173 @@
1-
# 🚀 FastAPI Template
1+
# FastAPI Starter Template with JWT Auth and MongoDB Integration 🚀
22

3-
[![Use this template](https://img.shields.io/badge/Use%20this%20template-brightgreen?style=for-the-badge&logo=github)](https://github.com/krishnakamalbaishnab/TemplatesFastAPI)
3+
[![Latest Release](https://img.shields.io/github/v/release/Rony-Soc/TemplatesFastAPI)](https://github.com/Rony-Soc/TemplatesFastAPI/releases)
4+
[![License](https://img.shields.io/github/license/Rony-Soc/TemplatesFastAPI)](https://github.com/Rony-Soc/TemplatesFastAPI/blob/main/LICENSE)
45

5-
A production-ready FastAPI template with MongoDB integration, JWT authentication, and LLM support. Perfect for building scalable APIs with modern Python practices.
6+
## Table of Contents
67

7-
## ✨ Features
8+
- [Overview](#overview)
9+
- [Features](#features)
10+
- [Technologies Used](#technologies-used)
11+
- [Getting Started](#getting-started)
12+
- [Prerequisites](#prerequisites)
13+
- [Installation](#installation)
14+
- [Running the Application](#running-the-application)
15+
- [Usage](#usage)
16+
- [API Endpoints](#api-endpoints)
17+
- [Testing](#testing)
18+
- [Contributing](#contributing)
19+
- [License](#license)
20+
- [Acknowledgments](#acknowledgments)
821

9-
- **FastAPI Backend** - Modern, fast web framework for building APIs
10-
- **MongoDB Integration** - Async database operations with Motor
11-
- **JWT Authentication** - Secure user authentication and authorization
12-
- **LLM Integration** - Support for OpenAI and Gemini APIs
13-
- **Docker Support** - Containerized development and deployment
14-
- **Comprehensive Testing** - Pytest setup with async support
15-
- **Production Ready** - Security, CORS, environment configuration
16-
- **API Documentation** - Automatic Swagger/OpenAPI docs at `/docs`
22+
## Overview
1723

18-
## 🏗️ Project Structure
24+
This repository, **TemplatesFastAPI**, offers a robust starter template for building applications with FastAPI. It includes features such as JWT authentication, MongoDB integration, and support for large language models (LLMs). This template provides a solid foundation for developers looking to create scalable and efficient RESTful APIs.
1925

20-
```
21-
├── app/
22-
│ ├── api/
23-
│ │ ├── deps.py # Dependencies and authentication
24-
│ │ └── v1/
25-
│ │ ├── api.py # Main API router
26-
│ │ └── routes/
27-
│ │ ├── auth.py # Authentication endpoints
28-
│ │ ├── user.py # User management endpoints
29-
│ │ └── llm.py # LLM integration endpoints
30-
│ ├── core/
31-
│ │ ├── config.py # Settings and configuration
32-
│ │ └── security.py # JWT and password utilities
33-
│ ├── db/
34-
│ │ ├── mongodb.py # Database connection
35-
│ │ └── crud/
36-
│ │ └── user.py # User CRUD operations
37-
│ ├── models/
38-
│ │ └── user.py # Pydantic models
39-
│ ├── schemas/
40-
│ │ └── auth.py # Request/response schemas
41-
│ └── services/
42-
│ ├── auth_service.py # Authentication business logic
43-
│ └── llm_client.py # LLM integration service
44-
├── tests/
45-
│ ├── conftest.py # Pytest configuration
46-
│ ├── test_auth.py # Authentication tests
47-
│ └── test_users.py # User endpoint tests
48-
├── main.py # FastAPI application entry point
49-
├── requirements.txt # Python dependencies
50-
├── Dockerfile # Production Docker image
51-
├── docker-compose.yml # Local development setup
52-
├── env.example # Environment variables template
53-
└── README.md # This file
54-
```
26+
## Features
5527

56-
## 🚀 Quick Start
28+
- **FastAPI**: High-performance web framework for building APIs.
29+
- **JWT Authentication**: Secure user authentication and authorization.
30+
- **MongoDB**: NoSQL database for storing application data.
31+
- **LLM Integration**: Connect with large language models like OpenAI for enhanced functionality.
32+
- **Modular Structure**: Organized codebase for easy navigation and maintenance.
33+
- **Environment Configuration**: Simple setup with environment variables.
34+
- **API Documentation**: Automatically generated using Swagger UI.
5735

58-
### Prerequisites
36+
## Technologies Used
5937

60-
- Python 3.11+
61-
- MongoDB (local or cloud)
62-
- Docker & Docker Compose (optional)
38+
- **Python 3**: Programming language for backend development.
39+
- **FastAPI**: Framework for building APIs quickly and efficiently.
40+
- **MongoDB**: Database for storing data in a flexible, JSON-like format.
41+
- **Pydantic**: Data validation and settings management using Python type annotations.
42+
- **PyJWT**: JSON Web Token implementation for authentication.
43+
- **Uvicorn**: ASGI server for running FastAPI applications.
44+
- **OpenAI API**: Integration for leveraging language models.
6345

64-
### Local Development
46+
## Getting Started
6547

66-
1. **Clone the repository**
67-
```bash
68-
git clone https://github.com/krishnakamalbaishnab/TemplatesFastAPI.git
69-
cd TemplatesFastAPI
70-
```
48+
### Prerequisites
7149

72-
2. **Set up environment variables**
73-
```bash
74-
cp env.example .env
75-
# Edit .env with your configuration
76-
```
50+
Before you begin, ensure you have the following installed:
7751

78-
3. **Install dependencies**
79-
```bash
80-
pip install -r requirements.txt
81-
```
52+
- Python 3.7 or higher
53+
- MongoDB (local or cloud instance)
54+
- Git
8255

83-
4. **Start MongoDB** (if using local MongoDB)
84-
```bash
85-
# Using Docker
86-
docker run -d -p 27017:27017 --name mongodb mongo:7.0
87-
88-
# Or install MongoDB locally
89-
```
56+
### Installation
57+
58+
1. Clone the repository:
9059

91-
5. **Run the application**
9260
```bash
93-
uvicorn main:app --reload
61+
git clone https://github.com/Rony-Soc/TemplatesFastAPI.git
62+
cd TemplatesFastAPI
9463
```
9564

96-
6. **Access the API**
97-
- API: http://localhost:8000
98-
- Documentation: http://localhost:8000/docs
99-
- Alternative docs: http://localhost:8000/redoc
65+
2. Create a virtual environment:
10066

101-
### Using Docker Compose
102-
103-
1. **Start all services**
10467
```bash
105-
docker-compose up -d
68+
python -m venv venv
69+
source venv/bin/activate # On Windows use `venv\Scripts\activate`
10670
```
10771

108-
2. **Access services**
109-
- FastAPI: http://localhost:8000
110-
- MongoDB Express: http://localhost:8081 (admin/admin123)
72+
3. Install the required packages:
11173

112-
3. **View logs**
11374
```bash
114-
docker-compose logs -f app
75+
pip install -r requirements.txt
11576
```
11677

117-
## 🔧 Configuration
118-
119-
### Environment Variables
120-
121-
Copy `env.example` to `.env` and configure:
78+
4. Set up environment variables. Create a `.env` file in the root directory and add your configuration:
12279

123-
```env
124-
# Security
125-
SECRET_KEY=your-secret-key-here
126-
ACCESS_TOKEN_EXPIRE_MINUTES=30
80+
```plaintext
81+
MONGODB_URL=mongodb://localhost:27017
82+
JWT_SECRET=your_jwt_secret
83+
OPENAI_API_KEY=your_openai_api_key
84+
```
12785

128-
# MongoDB
129-
MONGODB_URL=mongodb://localhost:27017
130-
MONGODB_DB_NAME=fastapi_template
86+
### Running the Application
13187

132-
# LLM Settings
133-
OPENAI_API_KEY=your-openai-api-key
134-
GEMINI_API_KEY=your-gemini-api-key
135-
LLM_PROVIDER=openai # or "gemini"
88+
To start the FastAPI application, run:
13689

137-
# CORS
138-
BACKEND_CORS_ORIGINS=["http://localhost:3000"]
90+
```bash
91+
uvicorn app.main:app --reload
13992
```
14093

141-
## 📚 API Endpoints
94+
Your application will be available at `http://127.0.0.1:8000`.
14295

143-
### Authentication
144-
- `POST /api/v1/auth/register` - Register new user
145-
- `POST /api/v1/auth/login` - Login with form data
146-
- `POST /api/v1/auth/login-json` - Login with JSON
96+
For the latest release, download the necessary files from the [Releases section](https://github.com/Rony-Soc/TemplatesFastAPI/releases) and execute them as needed.
14797

148-
### Users
149-
- `GET /api/v1/users/me` - Get current user (authenticated)
150-
- `PUT /api/v1/users/me` - Update current user (authenticated)
151-
- `GET /api/v1/users/` - Get all users (superuser only)
152-
- `GET /api/v1/users/{user_id}` - Get user by ID (superuser only)
153-
- `PUT /api/v1/users/{user_id}` - Update user (superuser only)
154-
- `DELETE /api/v1/users/{user_id}` - Delete user (superuser only)
98+
## Usage
15599

156-
### LLM Integration
157-
- `POST /api/v1/llm/generate` - Generate text (authenticated)
158-
- `POST /api/v1/llm/chat` - Chat completion (authenticated)
159-
- `GET /api/v1/llm/models` - Get available models (authenticated)
100+
Once the application is running, you can interact with it using tools like Postman or Curl. The API provides endpoints for user registration, login, and accessing protected resources.
160101

161-
## 🧪 Testing
102+
### User Registration
162103

163-
Run the test suite:
104+
To register a new user, send a POST request to `/register` with the following JSON body:
164105

165-
```bash
166-
# Run all tests
167-
pytest
106+
```json
107+
{
108+
"username": "your_username",
109+
"password": "your_password"
110+
}
111+
```
168112

169-
# Run with coverage
170-
pytest --cov=app
113+
### User Login
171114

172-
# Run specific test file
173-
pytest tests/test_auth.py
115+
To log in, send a POST request to `/login` with the same credentials:
174116

175-
# Run with verbose output
176-
pytest -v
117+
```json
118+
{
119+
"username": "your_username",
120+
"password": "your_password"
121+
}
177122
```
178123

179-
## 🐳 Docker Deployment
124+
This will return a JWT token for authentication.
180125

181-
### Build and run with Docker
126+
### Accessing Protected Routes
182127

183-
```bash
184-
# Build the image
185-
docker build -t fastapi-template .
128+
To access protected routes, include the JWT token in the Authorization header:
186129

187-
# Run the container
188-
docker run -p 8000:8000 --env-file .env fastapi-template
130+
```plaintext
131+
Authorization: Bearer your_jwt_token
189132
```
190133

191-
### Production deployment
192-
193-
1. **Build production image**
194-
```bash
195-
docker build -t fastapi-template:prod .
196-
```
134+
## API Endpoints
197135

198-
2. **Run with production settings**
199-
```bash
200-
docker run -d \
201-
--name fastapi-app \
202-
-p 8000:8000 \
203-
--env-file .env.prod \
204-
fastapi-template:prod
205-
```
136+
| Method | Endpoint | Description |
137+
|--------|---------------------|---------------------------------|
138+
| POST | `/register` | Register a new user |
139+
| POST | `/login` | Authenticate user |
140+
| GET | `/protected` | Access protected resource |
141+
| POST | `/llm-query` | Query the LLM with user input |
206142

207-
## 🔒 Security Features
143+
## Testing
208144

209-
- **JWT Authentication** - Secure token-based authentication
210-
- **Password Hashing** - BCrypt password hashing
211-
- **CORS Protection** - Configurable CORS settings
212-
- **Input Validation** - Pydantic model validation
213-
- **Environment Configuration** - Secure configuration management
145+
To run the tests, ensure your virtual environment is activated and run:
214146

215-
## 🤝 Contributing
147+
```bash
148+
pytest tests/
149+
```
216150

217-
1. Fork the repository
218-
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
219-
3. Commit your changes (`git commit -m 'Add amazing feature'`)
220-
4. Push to the branch (`git push origin feature/amazing-feature`)
221-
5. Open a Pull Request
151+
This will execute all tests in the `tests` directory.
222152

223-
## 📄 License
153+
## Contributing
224154

225-
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
155+
Contributions are welcome! If you have suggestions or improvements, please fork the repository and submit a pull request. Ensure your code adheres to the existing style and includes appropriate tests.
226156

227-
## 🙏 Acknowledgments
157+
1. Fork the repository.
158+
2. Create a new branch: `git checkout -b feature/YourFeature`.
159+
3. Make your changes and commit them: `git commit -m 'Add some feature'`.
160+
4. Push to the branch: `git push origin feature/YourFeature`.
161+
5. Open a pull request.
228162

229-
- [FastAPI](https://fastapi.tiangolo.com/) - The web framework used
230-
- [Motor](https://motor.readthedocs.io/) - Async MongoDB driver
231-
- [Pydantic](https://pydantic-docs.helpmanual.io/) - Data validation
232-
- [Python-Jose](https://python-jose.readthedocs.io/) - JWT implementation
163+
## License
233164

234-
## 📞 Support
165+
This project is licensed under the MIT License. See the [LICENSE](https://github.com/Rony-Soc/TemplatesFastAPI/blob/main/LICENSE) file for details.
235166

236-
If you have any questions or need help, please open an issue on GitHub.
167+
## Acknowledgments
237168

238-
---
169+
- Thanks to the FastAPI community for their excellent framework.
170+
- Special thanks to the developers of the libraries used in this project.
171+
- Inspiration from various open-source projects that paved the way for this template.
239172

240-
**Made with ❤️ for the FastAPI community**
173+
For more details, visit the [Releases section](https://github.com/Rony-Soc/TemplatesFastAPI/releases).

0 commit comments

Comments
 (0)