ASGI Middleware for serving static file.
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).
- Standard ASGI middleware implement
- Async file IO
- Support ETag, base on md5(file_size + last_modified)
pip3 install -U ASGIMiddlewareStaticFile
pip3 install -U ASGIMiddlewareStaticFile
git clone https://github.com/rexzhang/asgi-middleware-static-file.git
cd asgi-middleware-static-file/example
(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])
(venv) ➜ example git:(main) ✗ uvicorn example_pure_asgi:app
Django 3.0+
(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'.
(venv) ➜ example_django git:(main) ✗ uvicorn example_django.asgi:application
Quart (Flask like)
(venv) ➜ example git:(main) ✗ uvicorn example_quart:app
WSGI app eg: Flask, Django on WSGI mode
(venv) ➜ example git:(main) ✗ uvicorn example_wsgi_app:app
You can send a list to static_root_paths
; example:
static_root_paths = [ "/path/a", "path/b" ]
application = ASGIMiddlewareStaticFile(
application,
static_url=settings.STATIC_URL,
static_root_paths=static_root_paths,
)
- Maintenance update
- Change depend policy
- Update aiofiles to 23.1.0
- Use more async API
- Use more aiofiles api
- Dropped Python 3.6 support. If you require it, use version 0.4.0
- Update package for pep517/pep621
- Rewrite some code
- Fix bug #3(Cannot serve files from root (static_url="/" becomes "//"))
- Maintenance release
- Drop Py35
- Compatible Py37-
- Check cross border access
- Add more type hints
- Fix bug
- Update for aiofiles
- Fix bug
- First release
-
ASGI Middleware
- django.contrib.staticfiles.handlers.ASGIStaticFilesHandler
-
WSGI Middleware
-
View
- starlette.staticfiles.StaticFiles
- zero copy
- file extension filter,
- Cache Control