22。FastAPI开发大型应用 在前面的代码示例中,我们都是在一个文件创建路由;在实际开发中,一般会根据需求进行模块划分,代码项目中也会根据模块进行开发,不同的模块采用不同的文件来编写程序。在FastAPI中提供了APIRouter来组织路由,相当于Flask中的Blueprints。22。1使用APIRouter创建路由 1。导入APIRouterfromfastapiimportAPIRouter 2。使用APIRouter定义路由操作routerAPIRouter() 使用router。get等对路由方法进行修饰即可,使用方式与FastAPI类相同。22。2FastAPI主体程序 在FastAPI主体程序中,将具有APIRouter的子模块导入,然后通过FastAPI类的includerouter将路由添加到应用程序。代码示例如下: 目录结构:main。pytestinit。pytest。pydemoinit。pydemo。py 上面的目录结构中,test与demo是Python包。 test。pycoding:utf8fromfastapiimportAPIRouterrouterAPIRouter()router。get(pathhello1)asyncdefhello1():return{hello1:HelloAPIRouter1} demo。pycoding:utf8fromfastapiimportAPIRouterrouterAPIRouter()router。get(pathhello2)asyncdefhello1():return{hello2:HelloAPIRouter2} main。pycoding:utf8fromfastapiimportFastAPIfromtestimporttestfromdemoimportdemoappFastAPI()app。includerouter(test。router)app。includerouter(demo。router)app。get(path)asyncdefroot():returnHelloworld 启动应用,然后进行请求测试:curlhttp:127。0。0。1:8000Helloworldcurlhttp:127。0。0。1:8000hello1{hello1:HelloAPIRouter1}curlhttp:127。0。0。1:8000hello2{hello2:HelloAPIRouter2}22。3APIRouter的参数 在实际开发中,一个文件中的路由是有相同的部分的,比如:testhello1、testhello2中,test是相同的路由路径,在FastAPI中,可以通过APIRouter的参数来添加路由的前缀,而不需要在每个路由定义的时候分别去添加。APIRouter的参数:prefix路由前缀tags标签dependencies依赖项responses额外的响应 除此之外,我们有时候在不同的文件中还需要相同的路由前缀,所以,APIRouter最好在包的初始化文件init。py中声明和定义。下面将上面的代码修改如下: demoinit。pycoding:utf8fromfastapiimportAPIRouterrouterAPIRouter(prefixdemo)importdemo。demo demo。pycoding:utf8fromdemoimportrouterrouter。get(pathhello2)asyncdefhello1():return{hello2:HelloAPIRouter2} testinit。pycoding:utf8fromfastapiimportAPIRouterrouterAPIRouter(prefixtest)importtest。test test。pycoding:utf8fromtestimportrouterrouter。get(pathhello1)asyncdefhello1():return{hello1:HelloAPIRouter1} main。pycoding:utf8fromfastapiimportFastAPIimporttestimportdemoappFastAPI()app。includerouter(test。router)app。includerouter(demo。router)app。get(path)asyncdefroot():returnHelloworld 从以上代码中可以看出,对应定义的路由:curlhttp:127。0。0。1:8000demohello2curlhttp:127。0。0。1:8000testhello1 运行测试:curlhttp:127。0。0。1:8000testhello1{hello1:HelloAPIRouter1}curlhttp:127。0。0。1:8000demohello2{hello2:HelloAPIRouter2} 需要注意:在包的初始化文件init。py文件中,一定要对使用了router的python文件进行import,并且import指令要放到router声明的后面,以便将使用了router的python文件编译进来。22。4包含路由 在FastAPI中,也可以使用FastAPI类的includerouter函数的相应参数进行路由前缀的设置,当然也可以设置其他参数,这样就不必在APIRouter声明中设置参数。如将上面的代码中修改如下: init。py中的代码修改为:fromfastapiimportAPIRouterrouterAPIRouter() main。pycoding:utf8fromfastapiimportFastAPIimporttestimportdemoappFastAPI()app。includerouter(test。router,prefixtest)app。includerouter(demo。router,prefixdemo)app。get(path)asyncdefroot():returnHelloworld 运行结果与上面的代码是相同的。22。5多次使用不同的prefix包含同一个路由器 在FastAPI中,也可以在同一路由上使用不同的前缀来多次使用。includerouter();这样做在有些场景下可能有用,例如以不同的前缀公开同一个的API,比方说apiv1和apilatest;这是一个可能并不真正需要的高级用法,但万一你有需要了就能够用上。 如:在上面的main。py中增加一行:app。includerouter(demo。router,prefixdemo2) 这样,我们在请求curlhttp:127。0。0。1:8000demo2hello2也会返回与curlhttp:127。0。0。1:8000demohello2相同的结果。22。6在另一个APIRouter中包含一个APIRouter 与在FastAPI应用程序中包含APIRouter的方式相同,也可以在另一个APIRouter中包含APIRouter。 如:上面的代码中,我们在testinit。py中增加一行:router。includerouter(demo。router) 这样,就可以请求:curlhttp:127。0。0。1:8000testhello2,返回结果:{hello2:HelloAPIRouter2} 本篇主要讲述了FastAPI的APIRouter的使用方法,主要以其prefix参数以及includerouter函数的prefix参数的使用来说明如何组织路由,通过组织路由来完成大型项目的开发,以便我们可以在多个文件中组织和管理软件的代码。