zoukankan      html  css  js  c++  java
  • FastAPI配置系列(三) FastAPI应用常见配置项

    一、概述

    在路径操作配置中显然都是针对API的配置,比如API分类的tags参数等,那么针对FastAPI的应用的配置是如何的呢?常用的包含:

    • title & description & version
    • docs_url

    ...

    当然还有很多参数,具体查看源码:

    class FastAPI(Starlette):
        def __init__(
            self,
            *,
            debug: bool = False,
            routes: Optional[List[BaseRoute]] = None,
            title: str = "FastAPI",
            description: str = "",
            version: str = "0.1.0",
            openapi_url: Optional[str] = "/openapi.json",
            openapi_tags: Optional[List[Dict[str, Any]]] = None,
            servers: Optional[List[Dict[str, Union[str, Any]]]] = None,
            dependencies: Optional[Sequence[Depends]] = None,
            default_response_class: Type[Response] = Default(JSONResponse),
            docs_url: Optional[str] = "/docs",
            redoc_url: Optional[str] = "/redoc",
            swagger_ui_oauth2_redirect_url: Optional[str] = "/docs/oauth2-redirect",
            swagger_ui_init_oauth: Optional[Dict[str, Any]] = None,
            middleware: Optional[Sequence[Middleware]] = None,
            exception_handlers: Optional[
                Dict[
                    Union[int, Type[Exception]],
                    Callable[[Request, Any], Coroutine[Any, Any, Response]],
                ]
            ] = None,
            on_startup: Optional[Sequence[Callable[[], Any]]] = None,
            on_shutdown: Optional[Sequence[Callable[[], Any]]] = None,
            openapi_prefix: str = "",
            root_path: str = "",
            root_path_in_servers: bool = True,
            responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None,
            callbacks: Optional[List[BaseRoute]] = None,
            deprecated: Optional[bool] = None,
            include_in_schema: bool = True,
            **extra: Any,
        ) 
    ...

    二、常见参数说明

    在进行参数说明之前,先看一下未加任何参数的代码:

    from fastapi import FastAPI
    
    app = FastAPI()
    
    
    @app.get("items")
    async def read_items():
        return {"info": "read_items"}

    表现形式:

    1、title & description & version

    from fastapi import FastAPI
    
    app = FastAPI(
        title="FastAPI Docs Test",
        description="FastAPI Application Params Test",
        version="1.1.1"
    )
    ...

    表现形式:

     2、docs_url

    from fastapi import FastAPI
    
    app = FastAPI(
        title="FastAPI Docs Test",
        description="FastAPI Application Params Test",
        version="1.1.1",
        docs_url="/docs"
    )

    docs是交互文档的地址,可以自定义。还有很多其它的参数可使用,比如openapi_url、redoc_url等等。

    作者:iveBoy
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    《Apache Velocity用户指南》官方文档
    Mockito教程
    GitHub访问速度慢的解决方法
    使用log4jdbc记录SQL信息
    分环境部署SpringBoot日志logback-spring.xml
    SpringBoot入门之spring-boot-maven-plugin
    swagger报No operations defined in spec!
    delphi 时间
    DELPHI常用类型及定义单元
    delphi 多线程编程
  • 原文地址:https://www.cnblogs.com/shenjianping/p/14854215.html
Copyright © 2011-2022 走看看