ASGI Middleware for serving Static File.
Project description
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.
Features
- Standard ASGI middleware implement
- Async file IO
- Support ETag(base on md5(file_size + last_modified) )
Install
pip3 install -U ASGIMiddlewareStaticFile
Usage
Common
Prepare
git clone https://github.com/rexzhang/asgi-middleware-static-file.git
cd asgi-middleware-static-file/example
Test with wget
(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
Code
Start Server
(venv) ➜ example git:(main) ✗ uvicorn example_pure_asgi:app
INFO: Started server process [7965]
INFO: Waiting for application startup.
INFO: ASGI 'lifespan' protocol appears unsupported.
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: 127.0.0.1:54529 - "GET /static/DEMO HTTP/1.1" 200 OK
Django 3.0+
Code
Collect static file
(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
(venv) ➜ example_django git:(main) ✗ uvicorn example_django.asgi:application
INFO: Started server process [9107]
INFO: Waiting for application startup.
INFO: ASGI 'lifespan' protocol appears unsupported.
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: 127.0.0.1:61925 - "GET /static/DEMO.txt HTTP/1.1" 200 OK
Quart (Flask like)
Code
Start Server
(venv) ➜ example git:(main) ✗ uvicorn example_quart:app
INFO: Started server process [7989]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: 127.0.0.1:56191 - "GET / HTTP/1.1" 200 OK
INFO: 127.0.0.1:56212 - "GET /static/DEMO HTTP/1.1" 200 OK
WSGI app eg: Flask, Django on WSGI mode
Code
Start Server
(venv) ➜ example git:(main) ✗ uvicorn example_wsgi_app:app
INFO: Started server process [8020]
INFO: Waiting for application startup.
INFO: ASGI 'lifespan' protocol appears unsupported.
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: 127.0.0.1:63924 - "GET /static/DEMO HTTP/1.1" 200 OK
History
0.5.0 -
- Use more aiofile 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
-
View
- starlette.staticfiles.StaticFiles
TODO
- zero copy
- file extension filter,
- Cache Control
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for ASGIMiddlewareStaticFile-0.5.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | a702b0010fc7381e358c8389a9fa43fd012e5700630bfff7e7fdf2324eb5563a |
|
MD5 | eac3db3521e3cb295cb9664b7e2c53f1 |
|
BLAKE2b-256 | cbdcd312b81e7632c429d9440e837f08fa2ad1eb457206312764533b91673e60 |
Close
Hashes for ASGIMiddlewareStaticFile-0.5.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed1553b11044c09df25ecb888abf11d7e0f340321766cd363a14eb14d13017a9 |
|
MD5 | 93cebcf9ba0556b8d5ef2cfc6c48c766 |
|
BLAKE2b-256 | f901dd90b01d642ed5900a38b35bcc77acab4e02b612923c93e1fc5025fd7c73 |