{"id":20014397,"url":"https://github.com/rexzhang/asgi-middleware-static-file","last_synced_at":"2025-05-04T22:31:13.575Z","repository":{"id":57411796,"uuid":"255537369","full_name":"rexzhang/asgi-middleware-static-file","owner":"rexzhang","description":"ASGI Middleware for serving static file.","archived":false,"fork":false,"pushed_at":"2023-12-19T14:22:00.000Z","size":86,"stargazers_count":12,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"main","last_synced_at":"2023-12-19T16:35:25.887Z","etag":null,"topics":["asgi","asycnio","middleware","staticfile"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rexzhang.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-04-14T07:15:25.000Z","updated_at":"2023-12-24T19:17:31.294Z","dependencies_parsed_at":"2023-12-24T19:27:56.526Z","dependency_job_id":null,"html_url":"https://github.com/rexzhang/asgi-middleware-static-file","commit_stats":null,"previous_names":[],"tags_count":10,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rexzhang%2Fasgi-middleware-static-file","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rexzhang%2Fasgi-middleware-static-file/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rexzhang%2Fasgi-middleware-static-file/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rexzhang%2Fasgi-middleware-static-file/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rexzhang","download_url":"https://codeload.github.com/rexzhang/asgi-middleware-static-file/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224413298,"owners_count":17306864,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asgi","asycnio","middleware","staticfile"],"created_at":"2024-11-13T07:42:10.498Z","updated_at":"2024-11-13T07:42:11.051Z","avatar_url":"https://github.com/rexzhang.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 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\u003e ASGIMiddlewareStaticFile is a solution when we need to distribute the whole project with static files in Docker; or\n\u003e 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) ➜  example git:(main) ✗ 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正在连接 127.0.0.1:8000... 已连接。\n已发出 HTTP 请求，正在等待回应... 200 OK\n长度：26 []\n正在保存至: “DEMO”\n\nDEMO                                   100%[===========================================================================\u003e]      26  --.-KB/s  用时 0s      \n\n2022-02-10 16:02:08 (529 KB/s) - 已保存 “DEMO” [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) ➜  example git:(main) ✗ 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) ➜  example git:(main) cd example_django \n(venv) ➜  example_django git:(main) ✗ 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) ➜  example_django git:(main) ✗ 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) ➜  example git:(main) ✗ 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) ➜  example git:(main) ✗ 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  - \u003chttps://github.com/kobinpy/wsgi-static-middleware\u003e\n  - \u003chttps://pypi.org/project/whitenoise/\u003e\n\n- View\n  - starlette.staticfiles.StaticFiles\n\n## TODO\n\n- zero copy\n- file extension filter,\n- Cache Control\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frexzhang%2Fasgi-middleware-static-file","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frexzhang%2Fasgi-middleware-static-file","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frexzhang%2Fasgi-middleware-static-file/lists"}