Skip to content

Настройки CORS

Эти настройки используются для управления опциями CORS.

Bases: BaseModel

CORS Middleware Settings.

See CORSMiddleware <https://www.starlette.io/middleware/#corsmiddleware>_ documentation.

.. note::

You can pass here any extra option supported by ``CORSMiddleware``,
even if it is not mentioned in documentation.

Examples

For development environment:

.. code-block:: bash

DATA_RENTGEN__SERVER__CORS__ENABLED=True
DATA_RENTGEN__SERVER__CORS__ALLOW_ORIGINS="*"
DATA_RENTGEN__SERVER__CORS__ALLOW_METHODS="*"
DATA_RENTGEN__SERVER__CORS__ALLOW_HEADERS="*"
DATA_RENTGEN__SERVER__CORS__EXPOSE_HEADERS="X-Request-ID,Location,Access-Control-Allow-Credentials"

For production environment:

.. code-block:: bash

DATA_RENTGEN__SERVER__CORS__ENABLED=True
DATA_RENTGEN__SERVER__CORS__ALLOW_ORIGINS="production.example.com"
DATA_RENTGEN__SERVER__CORS__ALLOW_METHODS="GET"
DATA_RENTGEN__SERVER__CORS__ALLOW_HEADERS="X-Request-ID,X-Request-With"
DATA_RENTGEN__SERVER__CORS__EXPOSE_HEADERS="X-Request-ID"
# custom option passed directly to middleware
DATA_RENTGEN__SERVER__CORS__MAX_AGE=600
Source code in data_rentgen/server/settings/cors.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class CORSSettings(BaseModel):
    """CORS Middleware Settings.

    See `CORSMiddleware <https://www.starlette.io/middleware/#corsmiddleware>`_ documentation.

    .. note::

        You can pass here any extra option supported by ``CORSMiddleware``,
        even if it is not mentioned in documentation.

    Examples
    --------

    For development environment:

    .. code-block:: bash

        DATA_RENTGEN__SERVER__CORS__ENABLED=True
        DATA_RENTGEN__SERVER__CORS__ALLOW_ORIGINS="*"
        DATA_RENTGEN__SERVER__CORS__ALLOW_METHODS="*"
        DATA_RENTGEN__SERVER__CORS__ALLOW_HEADERS="*"
        DATA_RENTGEN__SERVER__CORS__EXPOSE_HEADERS="X-Request-ID,Location,Access-Control-Allow-Credentials"

    For production environment:

    .. code-block:: bash

        DATA_RENTGEN__SERVER__CORS__ENABLED=True
        DATA_RENTGEN__SERVER__CORS__ALLOW_ORIGINS="production.example.com"
        DATA_RENTGEN__SERVER__CORS__ALLOW_METHODS="GET"
        DATA_RENTGEN__SERVER__CORS__ALLOW_HEADERS="X-Request-ID,X-Request-With"
        DATA_RENTGEN__SERVER__CORS__EXPOSE_HEADERS="X-Request-ID"
        # custom option passed directly to middleware
        DATA_RENTGEN__SERVER__CORS__MAX_AGE=600
    """

    enabled: bool = Field(default=True, description="Set to ``True`` to enable middleware")
    allow_origins: list[str] = Field(default_factory=list, description="Domains allowed for CORS")
    allow_credentials: bool = Field(
        default=False,
        description="If ``True``, cookies should be supported for cross-origin request",
    )
    allow_methods: list[str] = Field(default=["GET"], description="HTTP Methods allowed for CORS")
    # https://github.com/snok/asgi-correlation-id#cors
    allow_headers: list[str] = Field(
        default=["X-Request-ID", "X-Request-With"],
        description="HTTP headers allowed for CORS",
    )
    expose_headers: list[str] = Field(default=["X-Request-ID"], description="HTTP headers exposed from backend")

    @field_validator("allow_origins", "allow_methods", "allow_headers", "expose_headers", mode="before")
    @classmethod
    def _validate_bootstrap_servers(cls, value: Any):
        if not isinstance(value, str):
            return value
        if "[" in value:
            return json.loads(value)
        return [item.strip() for item in value.split(",")]

    model_config = ConfigDict(extra="allow")