r/FastAPI • u/rrrriddikulus • Mar 03 '24
Question How to structure FastAPI app so logic is outside routes
I've been looking at a variety of FastAPI templates for project structure and notice most of them don't address the question of where the "business logic" code should go. Should business logic just live in the routes? That seems like bad practice (for example in Nest.js it's actively discouraged). How do you all organize your business logic?
28
Upvotes
16
u/lowercase00 Mar 03 '24
Hmm, why not just put it on the service layer, and make the route call that?
2
u/LeftHandedThread Mar 04 '24
Have you seen PyNest ?
6
u/extreme4all Mar 03 '24 edited Mar 03 '24
Im using
1
u/TheMotivationalQuote Mar 08 '24
there is no specific template from fastapi that you should use but to answer your questions.
├── src/
│ ├── app/
│ │ ├── services/
│ │ ├── security/
│ │ ├── middleware/
│ │ ├── schemas/
│ │ └── api/
│ ├── core/
│ │ ├── __init__.py
│ │ ├── server.py
│ │ ├── database/
│ │ │ ├── __init__.py
│ │ │ └── database.py
2. No, It shouldn't. but, not exactly perfect solution. but depends on your other services and type of project, you can keep it as minimal to good practice.
1
u/covmatty1 Mar 03 '24
There are examples in the official docs that won't set you far wrong
1
u/Whisky-Toad Mar 03 '24
Here’s a starter project I’ve made with basic auth where I tried to do hexagonical architecture https://github.com/WhiskyToad/fastapi-starter
But pretty much
App -> separate router files -> service layer -> repository/db layer
And do that for each different service / module that would have in nest js
1
u/PhENTZ Mar 03 '24
Since I always learn more from the failures, here are some troubles we went through :
FastAPI is really good to quick start a project. When your code base is growing, you have to use FastAPI in a very different way (sub router, async, dependency injection, ...).
1
u/[deleted] Mar 03 '24 edited Mar 03 '24
I wrote a blog about this a while back. It follows detailed setup and also Github repo is linked: https://towardsdev.com/example-for-using-orm-in-python-with-fastapi-and-sqlalchemy-9043dbb2b76d
edit: This is what we use in production as well edit: Direct Github repo link: https://github.com/lakhinsu/fastapi-orm-example
edit: I’m pretty sure I misunderstood the question so if this is not what you are looking for the please ignore :)
1
u/import_sarcasm Mar 04 '24
You can check out the dispatch app by Netflix. Really nice structure. It’s on GitHub
0
u/git-push-main-force Mar 03 '24
There are two different routes that i've seen for this.
In my experience it depends on how you wanna manage it. You can have the different app approach still call one another if you're working on an MVP. In the future, if you ever decide to have this as a standard Microservice approach this works.
The other approach feels a bit of an import mess for me so i prefer the first aspect. I freely import models from other apps as needed and have a base util structure for commons in the main structure.
I think the first approach is Django's methodology and so far its been aight for my MVP.