question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Best practice to structure multiple module

See original GitHub issue

When I sperate apis into multiple module, I find it hard to structure the code, currently I approach like this:

# app.py
from fastapi import FastAPI
app = FastAPI()
# api1.py
from app import app

@app.get('/test1')
...
# api2.py
from app import app

@app.get('/test2')
...
# main.py
from app import app
import api1
import api2
$ uvicorn main:app --lifespan on
...

Any suggestions?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

24reactions
dmontagucommented, Jul 13, 2019

From the FastAPI project-generator codebase:

# app/api/api_v1/endpoints/items.py
...

from fastapi import APIRouter, Depends, HTTPException

...

router = APIRouter()


@router.get("/", response_model=List[Item])
...
# app/api/api_v1/api.py
from fastapi import APIRouter

from app.api.api_v1.endpoints import items, login, users, utils

api_router = APIRouter()
api_router.include_router(login.router, tags=["login"])
api_router.include_router(users.router, prefix="/users", tags=["users"])
api_router.include_router(utils.router, prefix="/utils", tags=["utils"])
api_router.include_router(items.router, prefix="/items", tags=["items"])
# app/main.py
from fastapi import FastAPI

...

from app.api.api_v1.api import api_router

...

app = FastAPI(title=config.PROJECT_NAME, openapi_url="/api/v1/openapi.json")

...

app.include_router(api_router, prefix=config.API_V1_STR)

...
4reactions
dmontagucommented, Jul 25, 2019

Right, you’d have to give it a name (e.g. @router.get("/items", response_model=List[Item])).

You can mount multiple routers with the same prefix if, for example, you want to have multiple distinct resources with the same prefix and different non-slash-ending endpoints:

from fastapi import FastAPI, APIRouter

app = FastAPI()

router1 = APIRouter()
router2 = APIRouter()


@router1.get("/items")
async def read_items():
    return []


@router2.get("/things")
async def read_things():
    return []


app.include_router(router1, prefix="")
app.include_router(router2, prefix="")

if __name__ == '__main__':
    import uvicorn

    uvicorn.run(app, host='0.0.0.0', port=8000)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Best practices for multi-module projects with Spring Boot
#1 Find a proper module structure · #2 Minimize dependencies · #3 Continuous improvement · #4 Gradle api vs implementation · #5 Use...
Read more >
Angular modules: Best practices for structuring your app
In this tutorial, we'll show you how to structure your application into a series of smaller blocks, or modules, of functionality. We'll walk ......
Read more >
Best Practices for Modular Application Design and Development
Building a modular app requires expertise and deep knowledge of coding principles. Here you'll find some best practices for modular app ...
Read more >
Type safe, multi-module best practices with Navigation ...
As your app grows in size and complexity, following these best practices for using Navigation Compose will set you up for expanding your ......
Read more >
Getting Started | Creating a Multi Module Project - Spring
This guide shows you how to create a multi-module project with Spring Boot. The project will have a library jar and a main...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found