A WSGI middleware which provides static content for development environments or simple server setups. Its usage is quite simple:: import os from werkzeug.middleware.shared_data import SharedDataMiddleware app = SharedDataMiddleware(app, { '/shared': os.path.
| 40 | |
| 41 | |
| 42 | class SharedDataMiddleware: |
| 43 | """A WSGI middleware which provides static content for development |
| 44 | environments or simple server setups. Its usage is quite simple:: |
| 45 | |
| 46 | import os |
| 47 | from werkzeug.middleware.shared_data import SharedDataMiddleware |
| 48 | |
| 49 | app = SharedDataMiddleware(app, { |
| 50 | '/shared': os.path.join(os.path.dirname(__file__), 'shared') |
| 51 | }) |
| 52 | |
| 53 | The contents of the folder ``./shared`` will now be available on |
| 54 | ``http://example.com/shared/``. This is pretty useful during development |
| 55 | because a standalone media server is not required. Files can also be |
| 56 | mounted on the root folder and still continue to use the application because |
| 57 | the shared data middleware forwards all unhandled requests to the |
| 58 | application, even if the requests are below one of the shared folders. |
| 59 | |
| 60 | If `pkg_resources` is available you can also tell the middleware to serve |
| 61 | files from package data:: |
| 62 | |
| 63 | app = SharedDataMiddleware(app, { |
| 64 | '/static': ('myapplication', 'static') |
| 65 | }) |
| 66 | |
| 67 | This will then serve the ``static`` folder in the `myapplication` |
| 68 | Python package. |
| 69 | |
| 70 | The optional `disallow` parameter can be a list of :func:`~fnmatch.fnmatch` |
| 71 | rules for files that are not accessible from the web. If `cache` is set to |
| 72 | `False` no caching headers are sent. |
| 73 | |
| 74 | Currently the middleware does not support non-ASCII filenames. If the |
| 75 | encoding on the file system happens to match the encoding of the URI it may |
| 76 | work but this could also be by accident. We strongly suggest using ASCII |
| 77 | only file names for static files. |
| 78 | |
| 79 | The middleware will guess the mimetype using the Python `mimetype` |
| 80 | module. If it's unable to figure out the charset it will fall back |
| 81 | to `fallback_mimetype`. |
| 82 | |
| 83 | :param app: the application to wrap. If you don't want to wrap an |
| 84 | application you can pass it :exc:`NotFound`. |
| 85 | :param exports: a list or dict of exported files and folders. |
| 86 | :param disallow: a list of :func:`~fnmatch.fnmatch` rules. |
| 87 | :param cache: enable or disable caching headers. |
| 88 | :param cache_timeout: the cache timeout in seconds for the headers. |
| 89 | :param fallback_mimetype: The fallback mimetype for unknown files. |
| 90 | |
| 91 | .. versionchanged:: 1.0 |
| 92 | The default ``fallback_mimetype`` is |
| 93 | ``application/octet-stream``. If a filename looks like a text |
| 94 | mimetype, the ``utf-8`` charset is added to it. |
| 95 | |
| 96 | .. versionadded:: 0.6 |
| 97 | Added ``fallback_mimetype``. |
| 98 | |
| 99 | .. versionchanged:: 0.5 |
no outgoing calls