Skip to content

Using ServeStatic with ASGI apps

Tip

ServeStaticASGI inherits its interface and features from the WSGI variant.

To enable ServeStatic you need to wrap your existing ASGI application in a ServeStaticASGI instance and tell it where to find your static files. For example:

1
2
3
4
5
6
7
from servestatic import ServeStaticASGI

from my_project import MyASGIApp

application = MyASGIApp()
application = ServeStaticASGI(application, root="/path/to/static/files")
application.add_files("/path/to/more/static/files", prefix="more-files/")

If you would rather use ServeStatic as a standalone file server, you can simply not provide an ASGI app, such as via ServeStaticASGI(None, root="/path/to/static/files").

On initialization, ServeStatic walks over all the files in the directories that have been added (descending into sub-directories) and builds a list of available static files. Any requests which match a static file get served by ServeStatic, all others are passed through to the original application.

After configuring ServeStatic, you can use your favourite ASGI server (such as uvicorn, hypercorn, or nginx-unit) to run your application.

See the API reference documentation for detailed usage and features.