|
| 1 | +# Welcome to the Python Backend |
| 2 | + |
| 3 | +TOC |
| 4 | +1. [Overview](#overview) |
| 5 | +2. [Getting Setup](#getting-setup) |
| 6 | +3. [Learning FastAPI](#learning-fastapi) |
| 7 | +4. [Exploring the Codebase](#exploring-the-codebase) |
| 8 | +5. [Feature Parity Status](#feature-parity-status) |
| 9 | +6. [Utilities and Nice to Know](#nice-to-know) |
| 10 | + |
| 11 | + |
| 12 | +## Overview |
| 13 | +We're porting our Express backend to Python/FastAPI to achieve **functional parity** - same endpoints, same responses, same MongoDB operations. This allows one frontend to work with multiple backends. |
| 14 | + |
| 15 | + |
| 16 | +**Important**: We're not rewriting - we're replicating behavior exactly. So this means we are figuring out the pythonic way of doing things. You will find that we can accomplish things with Fast in fewer lines of code than in Express. |
| 17 | + |
| 18 | +## Getting Setup |
| 19 | +0. Install Python if you don't already have it. (*Depending on your version of python, you might need to use python3 and pip3 vs python and pip*) |
| 20 | +1. Clone from my fork of the main repo. |
| 21 | + ```sh |
| 22 | + https://github.com/tmcneil-mdb/docs-sample-apps.git |
| 23 | + ``` |
| 24 | + |
| 25 | +2. Checkout my branch |
| 26 | + ```sh |
| 27 | + git checkout python-backend-setup |
| 28 | + ``` |
| 29 | + |
| 30 | +4. Verify you are on the right branch |
| 31 | + ```sh |
| 32 | + git branch |
| 33 | + ``` |
| 34 | +5. In the root directory. /python, make a virtual environment. |
| 35 | + ```sh |
| 36 | + python -m venv .venv |
| 37 | + ``` |
| 38 | +6. Activate the virtual environment |
| 39 | + ```sh |
| 40 | + source .venv/bin/activate |
| 41 | + ``` |
| 42 | +7. Navigate back to /python, install the required packages. |
| 43 | + ```sh |
| 44 | + pip install -r requirements.txt |
| 45 | + ``` |
| 46 | +8. Create an .env file at the /python root. |
| 47 | +It should have this format: |
| 48 | + ```python |
| 49 | + MONGO_URI="place your connection string here" |
| 50 | + MONGO_DB="sample_mflix" |
| 51 | + ``` |
| 52 | +9. Navigate back to the root and start the server. |
| 53 | + ```sh |
| 54 | + fastapi dev main.py or uvicorn main:app --reload |
| 55 | + ``` |
| 56 | + (NOTE: THESE WONT WORK IF THE VIRTUAL ENVIRONMENT IS NOT ACTIVATED) |
| 57 | +10. Click the link in the terminal or visit ```localhost:8000/docs``` |
| 58 | +11. Try visiting ```localhost:8000/api/movies```. |
| 59 | + |
| 60 | + If the setup ran correctly, you should see data 🎉 |
| 61 | + |
| 62 | +*I recommend having your atlas instance up to explore your data and query the db directly.* |
| 63 | + |
| 64 | +**Troubleshooting**: If commands fail, ensure your virtual environment is activated (you should see `(.venv)` in your terminal prompt). |
| 65 | + |
| 66 | + |
| 67 | +## Learning FastAPI |
| 68 | +Before diving into the repo, I suggest spending some time with the official [FastAPI tutorial](https://fastapi.tiangolo.com/tutorial/). |
| 69 | + |
| 70 | +You only need to read up to the 'Request Body' section to get comfortable. *The query parameters and path parameters section can be read later.* |
| 71 | + |
| 72 | +Key Takeaways: |
| 73 | +- Decorators (@app.get, @app.post,etc.) |
| 74 | +- Query, path, and body parameters |
| 75 | +- Pydantic validation/ serialization |
| 76 | +- Automatic JSON responses |
| 77 | +- Built-in docs and testing at '/docs' |
| 78 | + |
| 79 | + |
| 80 | +## Exploring the Codebase |
| 81 | +Once you have completed the tutorial, I would suggest exploring the code and just noticing the differences between Express and Python. The apps are setup similarly but there are some small differences. |
| 82 | + |
| 83 | +### Architecture |
| 84 | +|Layer|Purpose|Express Equivalent| Differences| |
| 85 | +|:---|:-------|:----------|:------| |
| 86 | +|Routes <br> `src/routers/movies.py`| Defines all /movies endpoints (GET, POST, PUT, etc.)| /controllers/movieController.ts |The movies.ts file inside the routes file in the Express backend it actually wiring up the endpoints. Fast handles this for us in the main.py in one line. ```app.include_router(movies.router, prefix="/api/movies", tags=["movies"])```| |
| 87 | +|Models <br> `src/models/models.py`| Pydantic schema for validating and serializing request/response data.| /types/index.ts|There are some differences in how the models are constructed. Take note on how nested classes are handled.| |
| 88 | +|Utils <br> `src/utils/errorHandler.py`| Centralized utilities for responses, error handling and MongoDB exception mapping.|utils/errorHandler.ts |Express requires devs to write exception handling and validation on their own. Pydanic handles the validation and exception handling is a bit cleaner in Fast. You will notice the most differences here.| |
| 89 | +|Database <br>`src/database/mongo_client.py`| Handles the connection to the db|/config/database.ts| *The current database file does not have feature parity with the Express version*| |
| 90 | + |
| 91 | + |
| 92 | +## Feature Parity Status |
| 93 | +|Feature|Status|Owner|Notes| |
| 94 | +|:------|:-----|:----|:----| |
| 95 | +|Global Exception Handling| DONE| - |Found in Utils | |
| 96 | +|JSON Response Matching| DONE | - |Found in Utils| |
| 97 | +|CRUD- ```insertOne()```| Not Started| Angela| - | |
| 98 | +|CRUD- ```insertMany()```| Not Started| Taylor| - | |
| 99 | +|CRUD- ```findOne()```| Not Started| Angela| - | |
| 100 | +|CRUD- ```find()```| DONE| - | Found in movies.py as ```get_all_movies()``` This is a good function to look at to understand query parameters, requests and responses. Your functions will be simpler than this. But that is a good base to start with.| |
| 101 | +|CRUD- ```updateOne()```| Not Started| Angela| - | |
| 102 | +|CRUD- ```updateMany()```| Not Started| Taylor| - | |
| 103 | +|CRUD- ```deleteOne()```| Not Started| Angela| - | |
| 104 | +|CRUD- ```deleteMany()```| Not Started| Taylor| -| |
| 105 | +|CRUD- ```findOne()```| Not Started| Angela| - | |
| 106 | +|CRUD- ```findOneAndDelete()```| Not Started| Angela| This one is a bit harder but I figure this could be a fun challenge.| |
| 107 | +
|
| 108 | +
|
| 109 | +## Nice to Know |
| 110 | +### Utilities (errorHandler.py) |
| 111 | +This module provides a parity replacement for the Express errorHandler.ts file. It provides the same response shapes and error handling and removes the middleware. |
| 112 | +
|
| 113 | +- create_success_response(data,message) - creates a success response with the same shape as the Express version. |
| 114 | +- create_error_response(message,code,details) - creates a error response with the same shape as the Express version. |
| 115 | +- parse_mongo_exception(exc) - ensures PyMongo exceptions return JSON identical to the Express Version |
| 116 | +- register_error_handlers - hooks our error system into the Fast app |
| 117 | +
|
| 118 | +Fast automatically handles async and validation errors, so there is no need for asyncHandler or validateRequiredFields from the TS version. |
| 119 | +
|
| 120 | +### Useful Links |
| 121 | +- [Main Repo]( https://github.com/mongodb/docs-sample-apps/tree/main ) |
| 122 | +- [Sample App Scoping Doc](https://docs.google.com/document/d/12dROckw_Cp0ku2IIGku-ch7MvEuBPo0V4Gs-5wQgeHQ/edit?tab=t.0) |
| 123 | +- [Sample App Project Description & Breakdown Doc](https://docs.google.com/document/d/1xv2dmcNrT-HYk5TBE-KtVDPmW0274rpBZ0_0QmC66ac/edit?tab=t.0#heading=h.ki9tatw08ilc) |
0 commit comments