# ASGIMiddlewareStaticFile
![GitHub](https://img.shields.io/github/license/rexzhang/asgi-middleware-static-file)
[![](https://img.shields.io/pypi/v/ASGIMiddlewareStaticFile.svg)](https://pypi.org/project/ASGIMiddlewareStaticFile/)
[![](https://img.shields.io/pypi/pyversions/ASGIMiddlewareStaticFile.svg)](https://pypi.org/project/ASGIMiddlewareStaticFile/)
![Pytest Workflow Status](https://github.com/rexzhang/asgi-middleware-static-file/actions/workflows/check-pytest.yaml/badge.svg)
[![codecov](https://codecov.io/gh/rexzhang/asgi-middleware-static-file/branch/main/graph/badge.svg?token=083O4RHEZE)](https://codecov.io/gh/rexzhang/asgi-middleware-static-file)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
![PyPI - Downloads](https://img.shields.io/pypi/dm/ASGIMiddlewareStaticFile)
ASGI Middleware for serving static file.
## Why?
> ASGIMiddlewareStaticFile is a solution when we need to distribute the whole project with static files in Docker; or
> when the deployment environment has very limited resources; or Internal network(Unable to reach CDN).
## Features
- Standard ASGI middleware implement
- Async file IO
- Support ETag, base on md5(file_size + last_modified)
## Install
```shell
pip3 install -U ASGIMiddlewareStaticFile
```
## Usage
### Common
#### Prepare
```shell
pip3 install -U ASGIMiddlewareStaticFile
git clone https://github.com/rexzhang/asgi-middleware-static-file.git
cd asgi-middleware-static-file/example
```
#### Test with wget
```shell
(venv) ➜ example git:(main) ✗ wget http://127.0.0.1:8000/static/DEMO
--2022-02-10 16:02:07-- http://127.0.0.1:8000/static/DEMO
正在连接 127.0.0.1:8000... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:26 []
正在保存至: “DEMO”
DEMO 100%[===========================================================================>] 26 --.-KB/s 用时 0s
2022-02-10 16:02:08 (529 KB/s) - 已保存 “DEMO” [26/26])
```
### [Pure ASGI](https://asgi.readthedocs.io/en/latest/introduction.html)
#### Code
[`example_pure_asgi.py`](https://github.com/rexzhang/asgi-middleware-static-file/blob/main/example/example_pure_asgi.py)
#### Start Server
```shell
(venv) ➜ example git:(main) ✗ uvicorn example_pure_asgi:app
```
### [Django](https://docs.djangoproject.com/en/3.1/howto/deployment/asgi/) 3.0+
#### Code
[`/example_django/asgi.py`](https://github.com/rexzhang/asgi-middleware-static-file/blob/main/example/example_django/example_django/asgi.py)
#### Collect static file
```shell
(venv) ➜ example git:(main) cd example_django
(venv) ➜ example_django git:(main) ✗ python manage.py collectstatic
129 static files copied to '/Users/rex/p/asgi-middleware-static-file/example/example_django/staticfiles'.
```
#### Start Server
```shell
(venv) ➜ example_django git:(main) ✗ uvicorn example_django.asgi:application
```
### [Quart](https://pgjones.gitlab.io/quart/tutorials/quickstart.html) (Flask like)
#### Code
[`example_quart.py`](https://github.com/rexzhang/asgi-middleware-static-file/blob/main/example/example_quart.py)
#### Start Server
```shell
(venv) ➜ example git:(main) ✗ uvicorn example_quart:app
```
### [WSGI app](https://www.python.org/dev/peps/pep-3333/) eg: Flask, Django on WSGI mode
#### Code
[`example_wsgi_app.py`](https://github.com/rexzhang/asgi-middleware-static-file/blob/main/example/example_wsgi_app.py)
#### Start Server
```
(venv) ➜ example git:(main) ✗ uvicorn example_wsgi_app:app
```
## FAQ
### My static files are distributed in several different directories
You can send a list to `static_root_paths`; example:
```python
static_root_paths = [ "/path/a", "path/b" ]
application = ASGIMiddlewareStaticFile(
application,
static_url=settings.STATIC_URL,
static_root_paths=static_root_paths,
)
```
## History
### 0.6.1 - 20231219
- Maintenance update
- Change depend policy
### 0.6.0 - 20230210
- Update aiofiles to 23.1.0
- Use more async API
### 0.5.0 - 20220909
- Use more aiofiles api
- Dropped Python 3.6 support. If you require it, use version 0.4.0
- Update package for pep517/pep621
### v0.4.0 - 20220422
- Rewrite some code
- Fix bug #3(Cannot serve files from root (static_url="/" becomes "//"))
### v0.3.2
- Maintenance release
- Drop Py35
### v0.3.1
- Compatible Py37-
### v0.3.0
- Check cross border access
- Add more type hints
### v0.2.1
- Fix bug
### v0.2.0
- Update for aiofiles
- Fix bug
### v0.1.0
- First release
## Alternative
- ASGI Middleware
- django.contrib.staticfiles.handlers.ASGIStaticFilesHandler
- WSGI Middleware
- <https://github.com/kobinpy/wsgi-static-middleware>
- <https://pypi.org/project/whitenoise/>
- View
- starlette.staticfiles.StaticFiles
## TODO
- zero copy
- file extension filter,
- Cache Control
Raw data
{
"_id": null,
"home_page": "",
"name": "ASGIMiddlewareStaticFile",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "staticfile,middleware,asgi,asyncio",
"author": "Rex Zhang",
"author_email": "rex.zhang@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/3f/ec/e353dd5c59e517cb82f958698c777d7f6abceb93d550b0b3e63604affe48/ASGIMiddlewareStaticFile-0.6.1.tar.gz",
"platform": null,
"description": "# ASGIMiddlewareStaticFile\n\n![GitHub](https://img.shields.io/github/license/rexzhang/asgi-middleware-static-file)\n[![](https://img.shields.io/pypi/v/ASGIMiddlewareStaticFile.svg)](https://pypi.org/project/ASGIMiddlewareStaticFile/)\n[![](https://img.shields.io/pypi/pyversions/ASGIMiddlewareStaticFile.svg)](https://pypi.org/project/ASGIMiddlewareStaticFile/)\n![Pytest Workflow Status](https://github.com/rexzhang/asgi-middleware-static-file/actions/workflows/check-pytest.yaml/badge.svg)\n[![codecov](https://codecov.io/gh/rexzhang/asgi-middleware-static-file/branch/main/graph/badge.svg?token=083O4RHEZE)](https://codecov.io/gh/rexzhang/asgi-middleware-static-file)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n![PyPI - Downloads](https://img.shields.io/pypi/dm/ASGIMiddlewareStaticFile)\n\nASGI Middleware for serving static file.\n\n## Why?\n\n> ASGIMiddlewareStaticFile is a solution when we need to distribute the whole project with static files in Docker; or\n> when the deployment environment has very limited resources; or Internal network(Unable to reach CDN).\n\n## Features\n\n- Standard ASGI middleware implement\n- Async file IO\n- Support ETag, base on md5(file_size + last_modified)\n\n## Install\n\n```shell\npip3 install -U ASGIMiddlewareStaticFile\n```\n\n## Usage\n\n### Common\n\n#### Prepare\n\n```shell\npip3 install -U ASGIMiddlewareStaticFile\ngit clone https://github.com/rexzhang/asgi-middleware-static-file.git\ncd asgi-middleware-static-file/example\n```\n\n#### Test with wget\n\n```shell\n(venv) \u279c example git:(main) \u2717 wget http://127.0.0.1:8000/static/DEMO\n--2022-02-10 16:02:07-- http://127.0.0.1:8000/static/DEMO\n\u6b63\u5728\u8fde\u63a5 127.0.0.1:8000... \u5df2\u8fde\u63a5\u3002\n\u5df2\u53d1\u51fa HTTP \u8bf7\u6c42\uff0c\u6b63\u5728\u7b49\u5f85\u56de\u5e94... 200 OK\n\u957f\u5ea6\uff1a26 []\n\u6b63\u5728\u4fdd\u5b58\u81f3: \u201cDEMO\u201d\n\nDEMO 100%[===========================================================================>] 26 --.-KB/s \u7528\u65f6 0s \n\n2022-02-10 16:02:08 (529 KB/s) - \u5df2\u4fdd\u5b58 \u201cDEMO\u201d [26/26])\n```\n\n### [Pure ASGI](https://asgi.readthedocs.io/en/latest/introduction.html)\n\n#### Code\n\n[`example_pure_asgi.py`](https://github.com/rexzhang/asgi-middleware-static-file/blob/main/example/example_pure_asgi.py)\n\n#### Start Server\n\n```shell\n(venv) \u279c example git:(main) \u2717 uvicorn example_pure_asgi:app\n```\n\n### [Django](https://docs.djangoproject.com/en/3.1/howto/deployment/asgi/) 3.0+\n\n#### Code\n\n[`/example_django/asgi.py`](https://github.com/rexzhang/asgi-middleware-static-file/blob/main/example/example_django/example_django/asgi.py)\n\n#### Collect static file\n\n```shell\n(venv) \u279c example git:(main) cd example_django \n(venv) \u279c example_django git:(main) \u2717 python manage.py collectstatic\n\n129 static files copied to '/Users/rex/p/asgi-middleware-static-file/example/example_django/staticfiles'.\n```\n\n#### Start Server\n\n```shell\n(venv) \u279c example_django git:(main) \u2717 uvicorn example_django.asgi:application\n```\n\n### [Quart](https://pgjones.gitlab.io/quart/tutorials/quickstart.html) (Flask like)\n\n#### Code\n\n[`example_quart.py`](https://github.com/rexzhang/asgi-middleware-static-file/blob/main/example/example_quart.py)\n\n#### Start Server\n\n```shell\n(venv) \u279c example git:(main) \u2717 uvicorn example_quart:app \n```\n\n### [WSGI app](https://www.python.org/dev/peps/pep-3333/) eg: Flask, Django on WSGI mode\n\n#### Code\n\n[`example_wsgi_app.py`](https://github.com/rexzhang/asgi-middleware-static-file/blob/main/example/example_wsgi_app.py)\n\n#### Start Server\n\n```\n(venv) \u279c example git:(main) \u2717 uvicorn example_wsgi_app:app\n```\n\n## FAQ\n\n### My static files are distributed in several different directories\n\nYou can send a list to `static_root_paths`; example:\n\n```python\nstatic_root_paths = [ \"/path/a\", \"path/b\" ]\napplication = ASGIMiddlewareStaticFile(\n application,\n static_url=settings.STATIC_URL,\n static_root_paths=static_root_paths,\n)\n```\n\n## History\n\n### 0.6.1 - 20231219\n\n- Maintenance update\n- Change depend policy\n\n### 0.6.0 - 20230210\n\n- Update aiofiles to 23.1.0\n- Use more async API\n\n### 0.5.0 - 20220909\n\n- Use more aiofiles api\n- Dropped Python 3.6 support. If you require it, use version 0.4.0\n- Update package for pep517/pep621\n\n### v0.4.0 - 20220422\n\n- Rewrite some code\n- Fix bug #3(Cannot serve files from root (static_url=\"/\" becomes \"//\"))\n\n### v0.3.2\n\n- Maintenance release\n- Drop Py35\n\n### v0.3.1\n\n- Compatible Py37-\n\n### v0.3.0\n\n- Check cross border access\n- Add more type hints\n\n### v0.2.1\n\n- Fix bug\n\n### v0.2.0\n\n- Update for aiofiles\n- Fix bug\n\n### v0.1.0\n\n- First release\n\n## Alternative\n\n- ASGI Middleware\n - django.contrib.staticfiles.handlers.ASGIStaticFilesHandler\n\n- WSGI Middleware\n - <https://github.com/kobinpy/wsgi-static-middleware>\n - <https://pypi.org/project/whitenoise/>\n\n- View\n - starlette.staticfiles.StaticFiles\n\n## TODO\n\n- zero copy\n- file extension filter,\n- Cache Control\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "ASGI Middleware for serving Static File.",
"version": "0.6.1",
"project_urls": {
"changelog": "https://github.com/rexzhang/asgi-middleware-static-file/blob/main/README.md#history",
"documentation": "https://github.com/rexzhang/asgi-middleware-static-file/blob/main/README.md",
"homepage": "https://github.com/rexzhang/asgi-middleware-static-file",
"repository": "https://github.com/rexzhang/asgi-middleware-static-file"
},
"split_keywords": [
"staticfile",
"middleware",
"asgi",
"asyncio"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "cbcacb74a1fea57a8227cf4d03cad9a71b7b6a968e102b44a0942d3b802cc2f3",
"md5": "c528003997c9198bb8a7770657adcb34",
"sha256": "8a604766c7bc63cce4359a62af2701ba0721b446f9ca27c807829e1886f4eec5"
},
"downloads": -1,
"filename": "ASGIMiddlewareStaticFile-0.6.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "c528003997c9198bb8a7770657adcb34",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 6256,
"upload_time": "2023-12-19T13:46:22",
"upload_time_iso_8601": "2023-12-19T13:46:22.168570Z",
"url": "https://files.pythonhosted.org/packages/cb/ca/cb74a1fea57a8227cf4d03cad9a71b7b6a968e102b44a0942d3b802cc2f3/ASGIMiddlewareStaticFile-0.6.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "3fece353dd5c59e517cb82f958698c777d7f6abceb93d550b0b3e63604affe48",
"md5": "7c57941e9718cb5c1b6fa5a0f22c0254",
"sha256": "3bd2c8cfce4bdcb00ef570cd84d072e9f004d159568e6d1bbc72438d9936ca2a"
},
"downloads": -1,
"filename": "ASGIMiddlewareStaticFile-0.6.1.tar.gz",
"has_sig": false,
"md5_digest": "7c57941e9718cb5c1b6fa5a0f22c0254",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 7400,
"upload_time": "2023-12-19T13:46:24",
"upload_time_iso_8601": "2023-12-19T13:46:24.123344Z",
"url": "https://files.pythonhosted.org/packages/3f/ec/e353dd5c59e517cb82f958698c777d7f6abceb93d550b0b3e63604affe48/ASGIMiddlewareStaticFile-0.6.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-12-19 13:46:24",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "rexzhang",
"github_project": "asgi-middleware-static-file",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"requirements": [],
"lcname": "asgimiddlewarestaticfile"
}