fastapi-cors


Namefastapi-cors JSON
Version 0.0.6 PyPI version JSON
download
home_page
SummarySimple env support of CORS settings for Fastapi applications
upload_time2023-07-12 17:17:03
maintainer
docs_urlNone
author
requires_python>=3.8
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # fastapi-cors

A simply scoped abstraction to provide CORS settings via environment variables to a Fastapi application.

## Usage

```python
from fastapi import FastAPI

# during this next line `fastapi_cors.env` will read environment variables from .env
from fastapi_cors import CORS 

app = FastAPI()

CORS(app)
```

## Opinions

A health check route is optionally added that displays these (but not other) environment variables.


*If you want to disable it, use the code below*

```python

from fastapi import FastAPI

# during this next line `fastapi_cors.env` will read environment variables from .env
from fastapi_cors import CORS 

app = FastAPI()

CORS(app, include_health_check=False)
```

## Config

Configure FastAPI as usual. Extra arguments (that can be accessed from `app.extra`):

| Name | Default | Description |
| --- | --- | --- |
| `HOST` | 0.0.0.0 | Displayed in the Swagger title, with `app.title`. |
| `PORT` | 8000 | Where to mount the static directory. Disabled if value is falsy. |
| `LOG_LEVEL` | info | log level. |
| `ALLOW_ORIGINS` | ["http://localhost","http://localhost:3000"] | A list of origins that should be permitted to make cross-origin requests. E.g. ['https://example.org', 'https://www.example.org']. You can use ['*'] to allow any origin.  *These are the URLs clients can make requests from* |
| `ALLOWED_CREDENTIALS` | True | Indicate that cookies should be supported for cross-origin requests. Also, allow_origins cannot be set to ['*'] for credentials to be allowed, origins must be specified. |
| `ALLOWED_METHODS` | ["*"] | A list of HTTP methods that should be allowed for cross-origin requests. Defaults to ['*'] to allow all standard methods. You can use ['GET'] to reduce the list. |
| `ALLOWED_HEADERS` | ["Access-Control-Allow-Origin"] | A list of HTTP request headers that should be supported for cross-origin requests. You can use ['*'] to allow all headers. The Accept, Accept-Language, Content-Language and Content-Type headers are always allowed for [simple CORS requests](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#simple_requests) |

> ⚠️ `allow_origins` default is `["http://localhost","http://localhost:3000"]`, not `[]` (80 -> docs, 3000 -> frontend)
>
> ⚠️ `allow_methods` default is `["*"]`, not `["GET"]`
>
> ⚠️ `allowed_credentials` default is `True`, not `False`
>
> See the [FastAPI documentation on CORS](https://fastapi.tiangolo.com/tutorial/cors/?h=cors) for more information

### Example Env

Values will be cast into a `list` of `str`, as appropriate.

```env
HOST=0.0.0.0
PORT=8000
LOG_LEVEL=info
ALLOW_ORIGINS=http://localhost,http://localhost:3000
ALLOWED_CREDENTIALS=True
ALLOWED_METHODS=*
ALLOWED_HEADERS=Access-Control-Allow-Origin

```

*Note, this is not required unless you are changing a default or want to declare them all*

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "fastapi-cors",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "",
    "author": "",
    "author_email": "Ian Cleary <github@iancleary.me>",
    "download_url": "https://files.pythonhosted.org/packages/22/4b/33e263c78cc50b6477319b1403a3ae8f9139b4272411a415267104e145a3/fastapi-cors-0.0.6.tar.gz",
    "platform": null,
    "description": "# fastapi-cors\n\nA simply scoped abstraction to provide CORS settings via environment variables to a Fastapi application.\n\n## Usage\n\n```python\nfrom fastapi import FastAPI\n\n# during this next line `fastapi_cors.env` will read environment variables from .env\nfrom fastapi_cors import CORS \n\napp = FastAPI()\n\nCORS(app)\n```\n\n## Opinions\n\nA health check route is optionally added that displays these (but not other) environment variables.\n\n\n*If you want to disable it, use the code below*\n\n```python\n\nfrom fastapi import FastAPI\n\n# during this next line `fastapi_cors.env` will read environment variables from .env\nfrom fastapi_cors import CORS \n\napp = FastAPI()\n\nCORS(app, include_health_check=False)\n```\n\n## Config\n\nConfigure FastAPI as usual. Extra arguments (that can be accessed from `app.extra`):\n\n| Name | Default | Description |\n| --- | --- | --- |\n| `HOST` | 0.0.0.0 | Displayed in the Swagger title, with `app.title`. |\n| `PORT` | 8000 | Where to mount the static directory. Disabled if value is falsy. |\n| `LOG_LEVEL` | info | log level. |\n| `ALLOW_ORIGINS` | [\"http://localhost\",\"http://localhost:3000\"] | A list of origins that should be permitted to make cross-origin requests. E.g. ['https://example.org', 'https://www.example.org']. You can use ['*'] to allow any origin.  *These are the URLs clients can make requests from* |\n| `ALLOWED_CREDENTIALS` | True | Indicate that cookies should be supported for cross-origin requests. Also, allow_origins cannot be set to ['*'] for credentials to be allowed, origins must be specified. |\n| `ALLOWED_METHODS` | [\"*\"] | A list of HTTP methods that should be allowed for cross-origin requests. Defaults to ['*'] to allow all standard methods. You can use ['GET'] to reduce the list. |\n| `ALLOWED_HEADERS` | [\"Access-Control-Allow-Origin\"] | A list of HTTP request headers that should be supported for cross-origin requests. You can use ['*'] to allow all headers. The Accept, Accept-Language, Content-Language and Content-Type headers are always allowed for [simple CORS requests](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#simple_requests) |\n\n> \u26a0\ufe0f `allow_origins` default is `[\"http://localhost\",\"http://localhost:3000\"]`, not `[]` (80 -> docs, 3000 -> frontend)\n>\n> \u26a0\ufe0f `allow_methods` default is `[\"*\"]`, not `[\"GET\"]`\n>\n> \u26a0\ufe0f `allowed_credentials` default is `True`, not `False`\n>\n> See the [FastAPI documentation on CORS](https://fastapi.tiangolo.com/tutorial/cors/?h=cors) for more information\n\n### Example Env\n\nValues will be cast into a `list` of `str`, as appropriate.\n\n```env\nHOST=0.0.0.0\nPORT=8000\nLOG_LEVEL=info\nALLOW_ORIGINS=http://localhost,http://localhost:3000\nALLOWED_CREDENTIALS=True\nALLOWED_METHODS=*\nALLOWED_HEADERS=Access-Control-Allow-Origin\n\n```\n\n*Note, this is not required unless you are changing a default or want to declare them all*\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Simple env support of CORS settings for Fastapi applications",
    "version": "0.0.6",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "54ad016bdc85603cca123527167cb3e3ca408f639f3ad71b8d51fd50b116b85e",
                "md5": "9ff8cdf3120d42de972e4f5538a91fbd",
                "sha256": "d116b482c682f9c5330f04b1c49a9d504f3a9df6373bc43dd6c31f3b9d0b8b15"
            },
            "downloads": -1,
            "filename": "fastapi_cors-0.0.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "9ff8cdf3120d42de972e4f5538a91fbd",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 4990,
            "upload_time": "2023-07-12T17:17:01",
            "upload_time_iso_8601": "2023-07-12T17:17:01.679464Z",
            "url": "https://files.pythonhosted.org/packages/54/ad/016bdc85603cca123527167cb3e3ca408f639f3ad71b8d51fd50b116b85e/fastapi_cors-0.0.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "224b33e263c78cc50b6477319b1403a3ae8f9139b4272411a415267104e145a3",
                "md5": "c5d26ae215c3eba85b4fdc9074cad3dc",
                "sha256": "17eeb92f2b3dd8f0deb8ca69f031760e35edd5a0c811aaf80f9743e5e6ae50a1"
            },
            "downloads": -1,
            "filename": "fastapi-cors-0.0.6.tar.gz",
            "has_sig": false,
            "md5_digest": "c5d26ae215c3eba85b4fdc9074cad3dc",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 4440,
            "upload_time": "2023-07-12T17:17:03",
            "upload_time_iso_8601": "2023-07-12T17:17:03.083854Z",
            "url": "https://files.pythonhosted.org/packages/22/4b/33e263c78cc50b6477319b1403a3ae8f9139b4272411a415267104e145a3/fastapi-cors-0.0.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-07-12 17:17:03",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "fastapi-cors"
}
        
Elapsed time: 0.20497s