# FastAPI CDN host Selector for docs ui
![Python Versions](https://img.shields.io/pypi/pyversions/fastapi-cdn-host)
[![LatestVersionInPypi](https://img.shields.io/pypi/v/fastapi-cdn-host.svg?style=flat)](https://pypi.python.org/pypi/fastapi-cdn-host)
[![GithubActionResult](https://github.com/waketzheng/fastapi-cdn-host/workflows/ci/badge.svg)](https://github.com/waketzheng/fastapi-cdn-host/actions?query=workflow:ci)
[![Coverage Status](https://coveralls.io/repos/github/waketzheng/fastapi-cdn-host/badge.svg?branch=main)](https://coveralls.io/github/waketzheng/fastapi-cdn-host?branch=main)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
[![security: bandit](https://img.shields.io/badge/security-bandit-yellow.svg)](https://github.com/PyCQA/bandit)
Auto find swagger-ui in local files, if exist use them.
Otherwise make concurrent http requests by httpx to find out which third part cdn host(cdn.jsdelivr.net/unpkg.com/cdnjs.cloudflare.com/cdn.staticfile.org) is the fastest one.
**English** | [中文](./README.zh.md)
## Install
```bash
pip install fastapi-cdn-host
```
## Usage
```py
import fastapi_cdn_host
from fastapi import FastAPI
app = FastAPI()
# include_routes ...
fastapi_cdn_host.patch_docs(app)
```
See more at:
- [examples/](https://github.com/waketzheng/fastapi-cdn-host/tree/main/examples)
- [tests/](https://github.com/waketzheng/fastapi-cdn-host/tree/main/tests)
## Detail
1. Let's say that the default docs CDN host https://cdn.jsdelivr.net is too slow in your network, while unpkg.com is much faster.
```py
import fastapi_cdn_host
from fastapi import FastAPI
app = FastAPI()
fastapi_cdn_host.patch_docs(app) # Will use `unpkg.com`(or other faster host) to replace the `cdn.jsdelivr.net/npm`
```
2. To support offline docs/, put swagger-ui asset files into local directory named `static`
```py
from pathlib import Path
fastapi_cdn_host.patch_docs(app, Path(__file__).parent.joinpath('static'))
```
This get the same result of the example in official document:
https://fastapi.tiangolo.com/how-to/custom-docs-ui-assets/?h=static#self-hosting-javascript-and-css-for-docs
3. If asset files are ready in private cdn
```py
from fastapi_cdn_host import AssetUrl
fastapi_cdn_host.patch_docs(
app,
cdn_host=AssetUrl(
js='http://my-cdn.com/swagger-ui.js',
css='http://my-cdn.com/swagger-ui.css',
redoc='http://my-cdn.com/redoc.standalone.js',
favicon='http://my-cdn.com/favicon.ico',
)
)
```
## License
[MIT](./LICENSE)
Raw data
{
"_id": null,
"home_page": "https://github.com/waketzheng/fastapi-cdn-host",
"name": "fastapi-cdn-host",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.8",
"maintainer_email": null,
"keywords": null,
"author": "Waket Zheng",
"author_email": "waketzheng@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/86/62/a67a431e1495e495937f2a72c2cba968fe908902f30236a1b65241b8aefe/fastapi_cdn_host-0.8.3.tar.gz",
"platform": null,
"description": "# FastAPI CDN host Selector for docs ui\n![Python Versions](https://img.shields.io/pypi/pyversions/fastapi-cdn-host)\n[![LatestVersionInPypi](https://img.shields.io/pypi/v/fastapi-cdn-host.svg?style=flat)](https://pypi.python.org/pypi/fastapi-cdn-host)\n[![GithubActionResult](https://github.com/waketzheng/fastapi-cdn-host/workflows/ci/badge.svg)](https://github.com/waketzheng/fastapi-cdn-host/actions?query=workflow:ci)\n[![Coverage Status](https://coveralls.io/repos/github/waketzheng/fastapi-cdn-host/badge.svg?branch=main)](https://coveralls.io/github/waketzheng/fastapi-cdn-host?branch=main)\n[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)\n[![security: bandit](https://img.shields.io/badge/security-bandit-yellow.svg)](https://github.com/PyCQA/bandit)\n\nAuto find swagger-ui in local files, if exist use them.\nOtherwise make concurrent http requests by httpx to find out which third part cdn host(cdn.jsdelivr.net/unpkg.com/cdnjs.cloudflare.com/cdn.staticfile.org) is the fastest one.\n\n\n**English** | [\u4e2d\u6587](./README.zh.md)\n\n## Install\n\n```bash\npip install fastapi-cdn-host\n```\n\n## Usage\n```py\nimport fastapi_cdn_host\nfrom fastapi import FastAPI\n\napp = FastAPI()\n# include_routes ...\n\nfastapi_cdn_host.patch_docs(app)\n```\nSee more at:\n- [examples/](https://github.com/waketzheng/fastapi-cdn-host/tree/main/examples)\n- [tests/](https://github.com/waketzheng/fastapi-cdn-host/tree/main/tests)\n\n## Detail\n1. Let's say that the default docs CDN host https://cdn.jsdelivr.net is too slow in your network, while unpkg.com is much faster.\n```py\nimport fastapi_cdn_host\nfrom fastapi import FastAPI\n\napp = FastAPI()\nfastapi_cdn_host.patch_docs(app) # Will use `unpkg.com`(or other faster host) to replace the `cdn.jsdelivr.net/npm`\n```\n2. To support offline docs/, put swagger-ui asset files into local directory named `static`\n```py\nfrom pathlib import Path\n\nfastapi_cdn_host.patch_docs(app, Path(__file__).parent.joinpath('static'))\n```\nThis get the same result of the example in official document:\nhttps://fastapi.tiangolo.com/how-to/custom-docs-ui-assets/?h=static#self-hosting-javascript-and-css-for-docs\n\n3. If asset files are ready in private cdn\n```py\nfrom fastapi_cdn_host import AssetUrl\n\nfastapi_cdn_host.patch_docs(\n app,\n cdn_host=AssetUrl(\n js='http://my-cdn.com/swagger-ui.js',\n css='http://my-cdn.com/swagger-ui.css',\n redoc='http://my-cdn.com/redoc.standalone.js',\n favicon='http://my-cdn.com/favicon.ico',\n )\n)\n```\n\n## License\n\n[MIT](./LICENSE)\n",
"bugtrack_url": null,
"license": null,
"summary": "Auto choose the fastest cdn host for fastapi project docs.",
"version": "0.8.3",
"project_urls": {
"Homepage": "https://github.com/waketzheng/fastapi-cdn-host",
"Repository": "https://github.com/waketzheng/fastapi-cdn-host.git"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "feb9e685ef0a2ce04ba0becb0e4e4299ed0686c2a9e107fc710910836de39497",
"md5": "48bf43ae14ceddb1dd2819b0a5ab6a67",
"sha256": "5d20b40a7996a2e401ec9295176521c9882069265dc82b948023bf028713eedb"
},
"downloads": -1,
"filename": "fastapi_cdn_host-0.8.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "48bf43ae14ceddb1dd2819b0a5ab6a67",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.8",
"size": 13986,
"upload_time": "2024-11-03T07:49:52",
"upload_time_iso_8601": "2024-11-03T07:49:52.293921Z",
"url": "https://files.pythonhosted.org/packages/fe/b9/e685ef0a2ce04ba0becb0e4e4299ed0686c2a9e107fc710910836de39497/fastapi_cdn_host-0.8.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "8662a67a431e1495e495937f2a72c2cba968fe908902f30236a1b65241b8aefe",
"md5": "3e3ca6ee9c141fba6ed0b7092fc45bcb",
"sha256": "e6bbea3dd1b07ddf2604f0557a369db188f081058f5acfb3a26f3426b4a9b66e"
},
"downloads": -1,
"filename": "fastapi_cdn_host-0.8.3.tar.gz",
"has_sig": false,
"md5_digest": "3e3ca6ee9c141fba6ed0b7092fc45bcb",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.8",
"size": 13272,
"upload_time": "2024-11-03T07:49:54",
"upload_time_iso_8601": "2024-11-03T07:49:54.043040Z",
"url": "https://files.pythonhosted.org/packages/86/62/a67a431e1495e495937f2a72c2cba968fe908902f30236a1b65241b8aefe/fastapi_cdn_host-0.8.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-03 07:49:54",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "waketzheng",
"github_project": "fastapi-cdn-host",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "fastapi-cdn-host"
}