Skip to main content

ASGI Middleware for serving Static File.

Project description

ASGIMiddlewareStaticFile

GitHub Pytest Workflow Status codecov Code style: black PyPI - Downloads

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

pip3 install -U ASGIMiddlewareStaticFile

Usage

Common

Prepare

pip3 install -U ASGIMiddlewareStaticFile
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

example_pure_asgi.py

Start Server

(venv)   example git:(main)  uvicorn example_pure_asgi:app

Django 3.0+

Code

/example_django/asgi.py

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

Quart (Flask like)

Code

example_quart.py

Start Server

(venv)   example git:(main)  uvicorn example_quart:app    

WSGI app eg: Flask, Django on WSGI mode

Code

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:

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

TODO

  • zero copy
  • file extension filter,
  • Cache Control

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ASGIMiddlewareStaticFile-0.6.1.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

ASGIMiddlewareStaticFile-0.6.1-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file ASGIMiddlewareStaticFile-0.6.1.tar.gz.

File metadata

File hashes

Hashes for ASGIMiddlewareStaticFile-0.6.1.tar.gz
Algorithm Hash digest
SHA256 3bd2c8cfce4bdcb00ef570cd84d072e9f004d159568e6d1bbc72438d9936ca2a
MD5 7c57941e9718cb5c1b6fa5a0f22c0254
BLAKE2b-256 3fece353dd5c59e517cb82f958698c777d7f6abceb93d550b0b3e63604affe48

See more details on using hashes here.

File details

Details for the file ASGIMiddlewareStaticFile-0.6.1-py3-none-any.whl.

File metadata

File hashes

Hashes for ASGIMiddlewareStaticFile-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8a604766c7bc63cce4359a62af2701ba0721b446f9ca27c807829e1886f4eec5
MD5 c528003997c9198bb8a7770657adcb34
BLAKE2b-256 cbcacb74a1fea57a8227cf4d03cad9a71b7b6a968e102b44a0942d3b802cc2f3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page