|
1 | 1 | # FAST-API-APP
|
| 2 | + |
| 3 | +A modern, scalable, and production-ready FastAPI application designed for managing users, posts, and votes. This project demonstrates best practices in API development, testing, and deployment, making it an excellent showcase for recruiters and collaborators. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## 🚀 Features |
| 8 | + |
| 9 | +- **User Management**: Create, retrieve, and manage users with secure authentication. |
| 10 | +- **Post Management**: Full CRUD operations for posts with ownership validation. |
| 11 | +- **Voting System**: Users can upvote or remove votes on posts. |
| 12 | +- **Authentication**: Secure user authentication using OAuth2 and JWT tokens. |
| 13 | +- **Database Integration**: SQLModel and Alembic for database modeling and migrations. |
| 14 | +- **Testing**: Comprehensive unit and integration tests using `pytest`. |
| 15 | +- **Containerization**: Docker support for seamless development and production environments. |
| 16 | +- **CI/CD**: Automated testing and deployment pipelines using GitHub Actions and Render. |
| 17 | +- **API Documentation**: Interactive API documentation with Swagger UI and ReDoc. |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +## 🛠️ Technologies Used |
| 22 | + |
| 23 | +### Backend Framework |
| 24 | + |
| 25 | +- **FastAPI**: High-performance, modern web framework for building APIs with Python 3.12+. |
| 26 | + |
| 27 | +### Database |
| 28 | + |
| 29 | +- **PostgreSQL**: Relational database for storing application data. |
| 30 | +- **SQLModel**: Combines SQLAlchemy and Pydantic for database modeling. |
| 31 | +- **Alembic**: Database migrations. |
| 32 | + |
| 33 | +### Authentication |
| 34 | + |
| 35 | +- **OAuth2**: Secure authentication with JWT tokens. |
| 36 | + |
| 37 | +### Testing |
| 38 | + |
| 39 | +- **Pytest**: Unit and integration testing framework. |
| 40 | +- **HTTPX**: Asynchronous HTTP client for testing API endpoints. |
| 41 | + |
| 42 | +### Containerization |
| 43 | + |
| 44 | +- **Docker**: Containerization for development and production environments. |
| 45 | +- **Docker Compose**: Simplified multi-container orchestration. |
| 46 | + |
| 47 | +### CI/CD |
| 48 | + |
| 49 | +- **GitHub Actions**: Automated testing and deployment pipelines. |
| 50 | +- **Render**: Cloud platform for deploying the application. |
| 51 | + |
| 52 | +### API Documentation |
| 53 | + |
| 54 | +- **Swagger UI**: Interactive API documentation [[Swagger UI](https://fast-api-app-f69e.onrender.com/docs)]. |
| 55 | +- **ReDoc**: Alternative API documentation interface [[ReDoc](https://fast-api-app-f69e.onrender.com/redoc)]. |
| 56 | + |
| 57 | +--- |
| 58 | + |
| 59 | +## 🧪 Testing |
| 60 | + |
| 61 | +## This project includes a robust test suite to ensure the reliability of the application. Both **unit tests** and **integration tests** are implemented to validate individual components and their interactions. |
| 62 | + |
| 63 | +### Running Tests |
| 64 | + |
| 65 | +1. Install the required dependencies: |
| 66 | + |
| 67 | + ```bash |
| 68 | + pip install -r requirements.txt |
| 69 | + ``` |
| 70 | + |
| 71 | +2. Run the test suite: |
| 72 | + ```bash |
| 73 | + pytest -v -s |
| 74 | + ``` |
| 75 | + |
| 76 | +### Test Coverage |
| 77 | + |
| 78 | +#### Unit Tests |
| 79 | + |
| 80 | +- **Users**: |
| 81 | + - User creation and validation. |
| 82 | + - Token generation and authentication. |
| 83 | +- **Posts**: |
| 84 | + - CRUD operations for posts. |
| 85 | + - Ownership validation. |
| 86 | +- **Votes**: |
| 87 | + - Adding and removing votes. |
| 88 | + - Duplicate vote handling. |
| 89 | + |
| 90 | +#### Integration Tests |
| 91 | + |
| 92 | +Integration tests validate the interaction between multiple components, such as database operations, authentication, and API endpoints. |
| 93 | + |
| 94 | +Example: Integration test for creating a post and voting on it: |
| 95 | + |
| 96 | +```python |
| 97 | +# filepath: tests/test_integration.py |
| 98 | +import pytest |
| 99 | + |
| 100 | +def test_create_post_and_vote(client, test_user, token_headers): |
| 101 | + # Create a post |
| 102 | + response = client.post( |
| 103 | + "/posts/", |
| 104 | + json={"title": "Integration Test Post", "content": "This is a test."}, |
| 105 | + headers=token_headers, |
| 106 | + ) |
| 107 | + assert response.status_code == 201 |
| 108 | + post_id = response.json()["id"] |
| 109 | + |
| 110 | + # Vote on the post |
| 111 | + vote_response = client.post( |
| 112 | + "/vote/", |
| 113 | + json={"post_id": post_id, "dir": 1}, |
| 114 | + headers=token_headers, |
| 115 | + ) |
| 116 | + assert vote_response.status_code == 201 |
| 117 | + assert vote_response.json()["message"] == "Vote added successfully" |
| 118 | +``` |
| 119 | + |
| 120 | +--- |
| 121 | + |
| 122 | +## 🌐 API Endpoints |
| 123 | + |
| 124 | +### Authentication |
| 125 | + |
| 126 | +- `POST /login`: User login and token generation. |
| 127 | + |
| 128 | +### Users |
| 129 | + |
| 130 | +- `GET /users/`: Retrieve all users. |
| 131 | +- `POST /users/`: Create a new user. |
| 132 | +- `GET /users/{id}`: Retrieve a user by ID. |
| 133 | + |
| 134 | +### Posts |
| 135 | + |
| 136 | +- `GET /posts/`: Retrieve all posts. |
| 137 | +- `POST /posts/`: Create a new post. |
| 138 | +- `GET /posts/{id}`: Retrieve a post by ID. |
| 139 | +- `PUT /posts/{id}`: Update a post. |
| 140 | +- `DELETE /posts/{id}`: Delete a post. |
| 141 | + |
| 142 | +### Votes |
| 143 | + |
| 144 | +- `POST /vote/`: Add or remove a vote on a post. |
| 145 | + |
| 146 | +--- |
| 147 | + |
| 148 | +## 🚀 Deployment |
| 149 | + |
| 150 | +### Local Deployment |
| 151 | + |
| 152 | +1. Clone the repository: |
| 153 | + |
| 154 | + ```bash |
| 155 | + git clone https://github.com/your-repo/fast-api-app.git |
| 156 | + cd fast-api-app |
| 157 | + ``` |
| 158 | + |
| 159 | +2. Install dependencies: |
| 160 | + |
| 161 | + ```bash |
| 162 | + pip install -r requirements.txt |
| 163 | + ``` |
| 164 | + |
| 165 | +3. Run database migrations: |
| 166 | + |
| 167 | + ```bash |
| 168 | + alembic upgrade head |
| 169 | + ``` |
| 170 | + |
| 171 | +4. Start the application: |
| 172 | + |
| 173 | + ```bash |
| 174 | + uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload |
| 175 | + ``` |
| 176 | + |
| 177 | +5. Access the API at [http://localhost:8000/docs](http://localhost:8000/docs). |
| 178 | + |
| 179 | +### Docker Deployment |
| 180 | + |
| 181 | +1. Build and run the Docker container: |
| 182 | + |
| 183 | + ```bash |
| 184 | + docker-compose -f docker-compose-prod.yml up --build |
| 185 | + ``` |
| 186 | + |
| 187 | +2. Access the API at [http://localhost](http://localhost). |
| 188 | + |
| 189 | +--- |
| 190 | + |
| 191 | +## 🔄 CI/CD Pipeline |
| 192 | + |
| 193 | +This project uses GitHub Actions for automated testing and deployment: |
| 194 | + |
| 195 | +1. **Continuous Integration (CI)**: |
| 196 | + |
| 197 | + - Runs tests on every push or pull request. |
| 198 | + - Ensures code quality and reliability. |
| 199 | + |
| 200 | +2. **Continuous Deployment (CD)**: |
| 201 | + - Deploys the application to Render after successful tests. |
| 202 | + |
| 203 | +--- |
| 204 | + |
| 205 | +## 📜 Environment Variables |
| 206 | + |
| 207 | +| Variable | Description | |
| 208 | +| ----------------------------- | -------------------------------- | |
| 209 | +| `DATABASE_URL` | Database connection URL | |
| 210 | +| `SECRET_KEY` | Secret key for JWT | |
| 211 | +| `ALGORITHM` | Algorithm for JWT | |
| 212 | +| `ACCESS_TOKEN_EXPIRE_MINUTES` | Token expiration time in minutes | |
| 213 | + |
| 214 | +## 📄 License |
| 215 | + |
| 216 | +This project is licensed under the [MIT License](LICENSE). |
| 217 | + |
| 218 | +``` |
| 219 | +
|
| 220 | +``` |
0 commit comments