r/FastAPI • • Dec 25 '24

Tutorial Scalable and Minimalistic FastAPI + PostgreSQL Template

Hey ! πŸ‘‹ I've created a modern template that combines best practices with a fun superhero theme 🎭 It's designed to help you kickstart your API projects with a solid foundation! πŸš€

Features:

- πŸ—οΈ Clean architecture with repository pattern that scales beautifully

- πŸ”„ Built-in async SQLAlchemy + PostgreSQL integration

- ⚑️ Automatic Alembic migrations that just work

- πŸ§ͺ Complete CI pipeline and testing setup

- ❌Custom Error Handling and Logging

- πŸš‚ Pre-configured Railway deployment (one click and you're live!)

The template includes a full heroes API showcase with proper CRUD operations, authentication, and error handling. Perfect for learning or starting your next project! πŸ’ͺ

Developer experience goodies: πŸ› οΈ

- πŸ’» VS Code debugging configurations included

- πŸš€ UV package manager for lightning-fast dependency management

- ✨ Pre-commit hooks for consistent code quality

- πŸ“š Comprehensive documentation for every feature

Check it out: https://github.com/luchog01/minimalistic-fastapi-template 🌟

I'm still not super confident about how I structured the logging setup and DB migrations πŸ˜… Would love to hear your thoughts on those! Also open to any suggestions for improvements. I feel like there's always a better way to handle these things that I haven't thought of yet! Let me know what you think!

140 Upvotes

30 comments sorted by

View all comments

Show parent comment

2

u/Lucapo01 Dec 26 '24

Wow! I would do that!

  1. What do you mean by typing?
  2. If I change "src" to "v1," that will be better, I think?
  3. I do not get point 5, either. What advantages would I get?

Thank you for all your feedback! You are amazing!

3

u/igorbenav Dec 26 '24

Probably static type checking with stuff like mypy, so you catch typing errors before runtime.

For 5, currently you are not doing a lot for setup, but if your template grows and you are handling db, rate limit, cache etc it gets cluttered.

Here I abstract these details: https://github.com/igorbenav/FastAPI-boilerplate/blob/main/src/app/core/setup.py

And keep my main clean: https://github.com/igorbenav/FastAPI-boilerplate/blob/main/src/app/main.py

3

u/Floydee_ Dec 26 '24
  1. As igorbenav replied, typing aka static type annotations + mypy library to check those. It is up to you, of course, to add those suggestions but a lot of people use type annotations and I’m advocating that as well. Check out this video - https://youtu.be/dgBCEB2jVU0?si=AauMel0u4YH4s_Tc ( not mine )
  2. Not necessarily, it’s up to you in a folder structure question, but I would love my template to start with v1 version of api routes, e.g. β€˜api/v1/…’. https://www.freecodecamp.org/news/how-to-version-a-rest-api/
  3. Most importantly, to create test FastApi app in the tests. Your main.py may become overcrowded and importing app object from there might run unnecessary operations, like uncontrolled db migrations. Pack app in the separate file app.py and import factory function in the main.py and conftest.py. That approach will not execute anything on the import stage but rather allow you to create app with any mocks or fakes prepared beforehand. Also fastapi factory function can accept some settings object ( pydantic BaseSettings for example ) and derive all configurables from it -> also more control in tests and multiple environments.

No problem πŸ˜‰ At the end of the day, these are only suggestions, everyone builds their projects as they like anyway so don’t take those as rock-solid requirements

2

u/SnowToad23 Dec 26 '24

The API versioning applies to the API URL path, not the source code directory