FastAPI Boilerplate using MongoDB, Motor, Docker

This page summarizes the projects mentioned and recommended in the original post on /r/FastAPI

Our great sponsors
  • WorkOS - The modern identity platform for B2B SaaS
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • SaaSHub - Software Alternatives and Reviews
  • fastapi-motor-mongo-template

    High-performance FastAPI backend boilerplate for real-world production, with mongo and pytest. Suitable for microservices.

  • dispatch

    All of the ad-hoc things you're doing to manage incidents today, done for you, and much more!

  • Hey, I have a lot of opinions about this template, but these are just my opinions based on my own experiences being burned by these things so take from them what you will: 1. Your version of poetry is outdated, dependency groups don't work that way anymore and this will fail to install on modern poetry 2. You list pyyaml as a dependency but don't use it anywhere 3. The healthcheck endpoint is interesting, but expensive and a security risk. I like the value this provides, but I don't know if exposing it this way or using it as a healthcheck is a good idea 1. You typically don't want to touch external systems (mongo) as part of a healthcheck as this can cause cascading failure chains that get out of hand quickly 2. You typically don't want to touch the underlying system itself 1. which means you can / should get rid of psutil as a dependency 4. You don't need and shouldn't use pytest-asyncio for a FastAPI project. It comes built-in with its own async test handlers that you should be using 5. Having python-dotenv installed in production has burned me many times. I recommend removing this complete, otherwise just moving it to a dev dep 6. Using the src layout prevents a lot of weird import time problems from cropping up in production, I recommend checking it out 7. The entrypoint for the Docker container should be using 1 worker, as containers really prefer if you have only a single root PID chain and nothing else. Deploying this into k8s would cause a lot of issues 8. Native python logging really isn't great for modern production applications. Structlog or Loguru are great alternatives and much easier to use (which should remove your only dependency on pyyaml) 9. The configuration management may not work the way you want since it is weakly typed. Since FastAPI uses Pydantic, you have access to BaseSettings which is a far superior product for configuration management, especially with environment variables 10. The app and API folder structure is an anti-pattern that doesn't scale past projects the size of a tutorial on how to laern FastAPI. I strongly recommend changing this to move of a vertical slice or folder per feature layout such as is used in https://github.com/Netflix/dispatch/tree/master/src/dispatch 11. FastAPI routes don't need `response_model=` anymore in favor of adding the return type to your function signature such as `async def create_thing() -> Thing:` 12. The uuid_masker function is interesting, but exposing UUIDs in logs usually doesn't pose a security risk and only makes debugging more difficult 13. You have some type lies in your code that could burn you such as https://github.com/alexk1919/fastapi-motor-mongo-template/blob/main/app/db/db.py#L10 . This pattern for the global DB handle has also burned me in the past and I had to go back and refactor out all of them to instead to purely use the FastAPI dependency injection chaining 14. datetime.datetime isn't safe to use as it is in sample_resource_common.py, you need a timezone aware implementation 15. Your test suite is stateful, require a running database, leak a lot of implementation details of the underlying models. This is every anti-pattern in the book for unit testing. And if you are going to do integration tests, then you would be better off with tooling designed for it such as playwright. Again, these are all just my opinions and may alone not be enough to warrant changing anything you have here.

  • WorkOS

    The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.

    WorkOS logo
NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts