Skip to content

๐ŸŽง ๐Ÿˆธ - ๐Ÿ—ป

๐Ÿšฅ ๐Ÿ‘† ๐Ÿ’ช โœ”๏ธ 2๏ธโƒฃ ๐Ÿ”ฌ FastAPI ๐Ÿˆธ, โฎ๏ธ ๐Ÿ‘ซ ๐Ÿ‘ ๐Ÿ”ฌ ๐Ÿ—„ & ๐Ÿ‘ซ ๐Ÿ‘ ๐Ÿฉบ โšœ, ๐Ÿ‘† ๐Ÿ’ช โœ”๏ธ ๐Ÿ‘‘ ๐Ÿ“ฑ & "๐Ÿ—ป" 1๏ธโƒฃ (โš–๏ธ ๐ŸŒ…) ๐ŸŽง-๐Ÿˆธ(โ“‚).

๐Ÿ—œ FastAPI ๐Ÿˆธ

"๐Ÿ—œ" โ›“ โŽ ๐Ÿ• "๐Ÿ”ฌ" ๐Ÿˆธ ๐ŸŽฏ โžก, ๐Ÿ‘ˆ โคด๏ธ โœŠ ๐Ÿ’… ๐Ÿšš ๐ŸŒ ๐Ÿ”ฝ ๐Ÿ‘ˆ โžก, โฎ๏ธ โžก ๐Ÿ› ๏ธ ๐Ÿ“ฃ ๐Ÿ‘ˆ ๐ŸŽง-๐Ÿˆธ.

๐Ÿ”-๐ŸŽš ๐Ÿˆธ

๐Ÿฅ‡, โœ ๐Ÿ‘‘, ๐Ÿ”-๐ŸŽš, FastAPI ๐Ÿˆธ, & ๐Ÿšฎ โžก ๐Ÿ› ๏ธ:

from fastapi import FastAPI

app = FastAPI()


@app.get("/app")
def read_main():
    return {"message": "Hello World from main app"}


subapi = FastAPI()


@subapi.get("/sub")
def read_sub():
    return {"message": "Hello World from sub API"}


app.mount("/subapi", subapi)

๐ŸŽง-๐Ÿˆธ

โคด๏ธ, โœ ๐Ÿ‘† ๐ŸŽง-๐Ÿˆธ, & ๐Ÿšฎ โžก ๐Ÿ› ๏ธ.

๐Ÿ‘‰ ๐ŸŽง-๐Ÿˆธ โž•1๏ธโƒฃ ๐Ÿฉ FastAPI ๐Ÿˆธ, โœ‹๏ธ ๐Ÿ‘‰ 1๏ธโƒฃ ๐Ÿ‘ˆ ๐Ÿ”œ "๐Ÿ—ป":

from fastapi import FastAPI

app = FastAPI()


@app.get("/app")
def read_main():
    return {"message": "Hello World from main app"}


subapi = FastAPI()


@subapi.get("/sub")
def read_sub():
    return {"message": "Hello World from sub API"}


app.mount("/subapi", subapi)

๐Ÿ—ป ๐ŸŽง-๐Ÿˆธ

๐Ÿ‘† ๐Ÿ”-๐ŸŽš ๐Ÿˆธ, app, ๐Ÿ—ป ๐ŸŽง-๐Ÿˆธ, subapi.

๐Ÿ‘‰ ๐Ÿ’ผ, โšซ๏ธ ๐Ÿ”œ ๐Ÿ“Œ โžก /subapi:

from fastapi import FastAPI

app = FastAPI()


@app.get("/app")
def read_main():
    return {"message": "Hello World from main app"}


subapi = FastAPI()


@subapi.get("/sub")
def read_sub():
    return {"message": "Hello World from sub API"}


app.mount("/subapi", subapi)

โœ… ๐Ÿง ๐Ÿ› ๏ธ ๐Ÿฉบ

๐Ÿ”œ, ๐Ÿƒ uvicorn โฎ๏ธ ๐Ÿ‘‘ ๐Ÿ“ฑ, ๐Ÿšฅ ๐Ÿ‘† ๐Ÿ“ main.py, โšซ๏ธ ๐Ÿ”œ:

$ uvicorn main:app --reload

<span style="color: green;">INFO</span>:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

& ๐Ÿ“‚ ๐Ÿฉบ http://127.0.0.1:8000/docs.

๐Ÿ‘† ๐Ÿ”œ ๐Ÿ‘€ ๐Ÿง ๐Ÿ› ๏ธ ๐Ÿฉบ ๐Ÿ‘‘ ๐Ÿ“ฑ, ๐Ÿ”Œ ๐Ÿ•ด ๐Ÿšฎ ๐Ÿ‘ โžก ๐Ÿ› ๏ธ:

& โคด๏ธ, ๐Ÿ“‚ ๐Ÿฉบ ๐ŸŽง-๐Ÿˆธ, http://127.0.0.1:8000/subapi/docs.

๐Ÿ‘† ๐Ÿ”œ ๐Ÿ‘€ ๐Ÿง ๐Ÿ› ๏ธ ๐Ÿฉบ ๐ŸŽง-๐Ÿˆธ, โœ… ๐Ÿ•ด ๐Ÿšฎ ๐Ÿ‘ โžก ๐Ÿ› ๏ธ, ๐ŸŒ ๐Ÿ”ฝ โ˜‘ ๐ŸŽง-โžก ๐Ÿ”ก /subapi:

๐Ÿšฅ ๐Ÿ‘† ๐Ÿ”„ ๐Ÿ”— โฎ๏ธ ๐Ÿ™† 2๏ธโƒฃ ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ”ข, ๐Ÿ‘ซ ๐Ÿ”œ ๐Ÿ‘ท โ˜‘, โ†ฉ๏ธ ๐Ÿ–ฅ ๐Ÿ”œ ๐Ÿ’ช ๐Ÿ’ฌ ๐Ÿ”  ๐ŸŽฏ ๐Ÿ“ฑ โš–๏ธ ๐ŸŽง-๐Ÿ“ฑ.

๐Ÿ“ก โ„น: root_path

๐Ÿ•โ” ๐Ÿ‘† ๐Ÿ—ป ๐ŸŽง-๐Ÿˆธ ๐Ÿ”ฌ ๐Ÿ”›, FastAPI ๐Ÿ”œ โœŠ ๐Ÿ’… ๐Ÿ”— ๐Ÿ—ป โžก ๐ŸŽง-๐Ÿˆธ โš™๏ธ ๐Ÿ› ๏ธ โšช๏ธโžก๏ธ ๐Ÿ”ซ ๐Ÿ”ง ๐Ÿค™ root_path.

๐Ÿ‘ˆ ๐ŸŒŒ, ๐ŸŽง-๐Ÿˆธ ๐Ÿ”œ ๐Ÿ’ญ โš™๏ธ ๐Ÿ‘ˆ โžก ๐Ÿ”ก ๐Ÿฉบ ๐ŸŽš.

& ๐ŸŽง-๐Ÿˆธ ๐Ÿ’ช โœ”๏ธ ๐Ÿšฎ ๐Ÿ‘ ๐Ÿ“Œ ๐ŸŽง-๐Ÿˆธ & ๐ŸŒ ๐Ÿ”œ ๐Ÿ‘ท โ˜‘, โ†ฉ๏ธ FastAPI ๐Ÿต ๐ŸŒ ๐Ÿ‘‰ root_pathโ“‚ ๐Ÿ”.

๐Ÿ‘† ๐Ÿ”œ ๐Ÿ’ก ๐ŸŒ… ๐Ÿ”ƒ root_path & โ” โš™๏ธ โšซ๏ธ ๐ŸŽฏ ๐Ÿ“„ ๐Ÿ”ƒ โ›… ๐Ÿ—ณ.