Skip to content

Commit fee7410

Browse files
committed
Refactor: Make resources do "init_app" that will do the router inclusion (so that middleware, sub-apps, etc. can be added)
1 parent f3244f9 commit fee7410

File tree

7 files changed

+21
-12
lines changed

7 files changed

+21
-12
lines changed

sample_fastapi/app/app_factory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ async def app_lifespan_handler(app: FastAPI):
2727

2828
def init_app():
2929
app = FastAPI(lifespan=app_lifespan_handler)
30-
resources.init_routes(app)
30+
resources.init_app(app)
3131
return app
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from .lifespan import lifespan
2-
from .routes import init_routes
2+
from .routes import init_app
33

4-
__all__ = ["lifespan", "init_routes"]
4+
__all__ = ["lifespan", "init_app"]
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
from .routes import init_routes
1+
from .routes import init_app, init_routes
22

3-
__all__ = ["init_routes"]
3+
__all__ = ["init_app", "init_routes"]

sample_fastapi/app/resources/calculator/routes.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from fastapi import APIRouter
1+
from fastapi import APIRouter, FastAPI
22

33
from .views import conv_dec2hex, conv_hex2dec
44

@@ -10,3 +10,7 @@ def init_routes():
1010
router.add_api_route("/convert/hex2dec", conv_hex2dec, methods={"POST"})
1111

1212
return router
13+
14+
15+
def init_app(app: FastAPI):
16+
app.include_router(init_routes())
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
from .routes import init_routes
1+
from .routes import init_app, init_routes
22

3-
__all__ = ["init_routes"]
3+
__all__ = ["init_app", "init_routes"]

sample_fastapi/app/resources/hello/routes.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from fastapi import APIRouter
1+
from fastapi import APIRouter, FastAPI
22

33
from .views import respond_hello, respond_name_greet
44

@@ -10,3 +10,7 @@ def init_routes():
1010
router.add_api_route("/greet", respond_name_greet, methods={"GET"})
1111

1212
return router
13+
14+
15+
def init_app(app: FastAPI):
16+
app.include_router(init_routes())
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from fastapi import FastAPI
22

33

4-
def init_routes(app: FastAPI):
4+
def init_app(app: FastAPI):
5+
"""Initialize routes, middleware, sub-apps, etc. to the given Application"""
56
from . import calculator, hello
6-
app.include_router(hello.init_routes())
7-
app.include_router(calculator.init_routes())
7+
hello.init_app(app)
8+
calculator.init_app(app)

0 commit comments

Comments
 (0)