|
1 | | -# 🚀 FastAPI Template |
| 1 | +# FastAPI Starter Template with JWT Auth and MongoDB Integration 🚀 |
2 | 2 |
|
3 | | -[](https://github.com/krishnakamalbaishnab/TemplatesFastAPI) |
| 3 | +[](https://github.com/Rony-Soc/TemplatesFastAPI/releases) |
| 4 | +[](https://github.com/Rony-Soc/TemplatesFastAPI/blob/main/LICENSE) |
4 | 5 |
|
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 |
6 | 7 |
|
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) |
8 | 21 |
|
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 |
17 | 23 |
|
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. |
19 | 25 |
|
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 |
55 | 27 |
|
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. |
57 | 35 |
|
58 | | -### Prerequisites |
| 36 | +## Technologies Used |
59 | 37 |
|
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. |
63 | 45 |
|
64 | | -### Local Development |
| 46 | +## Getting Started |
65 | 47 |
|
66 | | -1. **Clone the repository** |
67 | | - ```bash |
68 | | - git clone https://github.com/krishnakamalbaishnab/TemplatesFastAPI.git |
69 | | - cd TemplatesFastAPI |
70 | | - ``` |
| 48 | +### Prerequisites |
71 | 49 |
|
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: |
77 | 51 |
|
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 |
82 | 55 |
|
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: |
90 | 59 |
|
91 | | -5. **Run the application** |
92 | 60 | ```bash |
93 | | - uvicorn main:app --reload |
| 61 | + git clone https://github.com/Rony-Soc/TemplatesFastAPI.git |
| 62 | + cd TemplatesFastAPI |
94 | 63 | ``` |
95 | 64 |
|
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: |
100 | 66 |
|
101 | | -### Using Docker Compose |
102 | | - |
103 | | -1. **Start all services** |
104 | 67 | ```bash |
105 | | - docker-compose up -d |
| 68 | + python -m venv venv |
| 69 | + source venv/bin/activate # On Windows use `venv\Scripts\activate` |
106 | 70 | ``` |
107 | 71 |
|
108 | | -2. **Access services** |
109 | | - - FastAPI: http://localhost:8000 |
110 | | - - MongoDB Express: http://localhost:8081 (admin/admin123) |
| 72 | +3. Install the required packages: |
111 | 73 |
|
112 | | -3. **View logs** |
113 | 74 | ```bash |
114 | | - docker-compose logs -f app |
| 75 | + pip install -r requirements.txt |
115 | 76 | ``` |
116 | 77 |
|
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: |
122 | 79 |
|
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 | + ``` |
127 | 85 |
|
128 | | -# MongoDB |
129 | | -MONGODB_URL=mongodb://localhost:27017 |
130 | | -MONGODB_DB_NAME=fastapi_template |
| 86 | +### Running the Application |
131 | 87 |
|
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: |
136 | 89 |
|
137 | | -# CORS |
138 | | -BACKEND_CORS_ORIGINS=["http://localhost:3000"] |
| 90 | +```bash |
| 91 | +uvicorn app.main:app --reload |
139 | 92 | ``` |
140 | 93 |
|
141 | | -## 📚 API Endpoints |
| 94 | +Your application will be available at `http://127.0.0.1:8000`. |
142 | 95 |
|
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. |
147 | 97 |
|
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 |
155 | 99 |
|
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. |
160 | 101 |
|
161 | | -## 🧪 Testing |
| 102 | +### User Registration |
162 | 103 |
|
163 | | -Run the test suite: |
| 104 | +To register a new user, send a POST request to `/register` with the following JSON body: |
164 | 105 |
|
165 | | -```bash |
166 | | -# Run all tests |
167 | | -pytest |
| 106 | +```json |
| 107 | +{ |
| 108 | + "username": "your_username", |
| 109 | + "password": "your_password" |
| 110 | +} |
| 111 | +``` |
168 | 112 |
|
169 | | -# Run with coverage |
170 | | -pytest --cov=app |
| 113 | +### User Login |
171 | 114 |
|
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: |
174 | 116 |
|
175 | | -# Run with verbose output |
176 | | -pytest -v |
| 117 | +```json |
| 118 | +{ |
| 119 | + "username": "your_username", |
| 120 | + "password": "your_password" |
| 121 | +} |
177 | 122 | ``` |
178 | 123 |
|
179 | | -## 🐳 Docker Deployment |
| 124 | +This will return a JWT token for authentication. |
180 | 125 |
|
181 | | -### Build and run with Docker |
| 126 | +### Accessing Protected Routes |
182 | 127 |
|
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: |
186 | 129 |
|
187 | | -# Run the container |
188 | | -docker run -p 8000:8000 --env-file .env fastapi-template |
| 130 | +```plaintext |
| 131 | +Authorization: Bearer your_jwt_token |
189 | 132 | ``` |
190 | 133 |
|
191 | | -### Production deployment |
192 | | - |
193 | | -1. **Build production image** |
194 | | - ```bash |
195 | | - docker build -t fastapi-template:prod . |
196 | | - ``` |
| 134 | +## API Endpoints |
197 | 135 |
|
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 | |
206 | 142 |
|
207 | | -## 🔒 Security Features |
| 143 | +## Testing |
208 | 144 |
|
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: |
214 | 146 |
|
215 | | -## 🤝 Contributing |
| 147 | +```bash |
| 148 | +pytest tests/ |
| 149 | +``` |
216 | 150 |
|
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. |
222 | 152 |
|
223 | | -## 📄 License |
| 153 | +## Contributing |
224 | 154 |
|
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. |
226 | 156 |
|
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. |
228 | 162 |
|
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 |
233 | 164 |
|
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. |
235 | 166 |
|
236 | | -If you have any questions or need help, please open an issue on GitHub. |
| 167 | +## Acknowledgments |
237 | 168 |
|
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. |
239 | 172 |
|
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