Skip to content

Commit

Permalink
chore(docs): Describe how to setup CORS Headers for aiohttp-tus
Browse files Browse the repository at this point in the history
And,

- Enable "blacken-docs" pre-commit hook
- Add `AUTHORS.rst` file
  • Loading branch information
playpauseandstop committed Mar 26, 2020
1 parent 3f39f89 commit 95b5a17
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 14 deletions.
10 changes: 10 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ repos:
language_version: "python3.8"
exclude: ^docs/.*$

- repo: "https://github.com/asottile/blacken-docs"
rev: "v1.6.0"
hooks:
- id: "blacken-docs"
name: "Format docs (blacken-docs)"
language_version: "python3.8"
args: ["-l", "64"]
additional_dependencies:
- "black==19.10b0"

- repo: "https://github.com/pre-commit/pre-commit-hooks"
rev: "v2.5.0"
hooks:
Expand Down
9 changes: 9 additions & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
======================
Authors & Contributors
======================

``aiohttp-tus`` is a `@pylotcode <https://github.com/pylotcode>`_ project, which never
happened without its authors & contributors, who listed below.

- `Igor Davydenko <https://github.com/playpauseandstop>`_
- `Alwin Wang <https://github.com/Dogfalo>`_
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ ChangeLog

- Add example to ensure that upload via `Uppy <https://uppy.io>`_ JavaScript library
works as expected
- Fix resuming uploads by passing missed ``Upload-Length`` header:
`#5 <https://github.com/pylotcode/aiohttp-tus/pull/5>`_
- Add documentation about `CORS Headers <https://aiohttp-tus.readthedocs.io/en/latest/usage.html#cors-headers>`_

1.0.0b2 (2020-03-18)
====================
Expand Down
8 changes: 4 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ aiohttp-tus
===========

.. image:: https://github.com/pylotcode/aiohttp-tus/workflows/ci/badge.svg
:target: https://github.com/pylotcode/aiohttp-tus/actions?query=workflow%3A%22ci%22
:alt: CI Workflow
:target: https://github.com/pylotcode/aiohttp-tus/actions?query=workflow%3A%22ci%22
:alt: CI Workflow

.. image:: https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white
:target: https://github.com/pre-commit/pre-commit
:alt: pre-commit
:target: https://github.com/pre-commit/pre-commit
:alt: pre-commit

.. image:: https://img.shields.io/pypi/v/aiohttp-tus.svg
:target: https://pypi.org/project/aiohttp-tus/
Expand Down
1 change: 1 addition & 0 deletions docs/authors.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. include:: ../AUTHORS.rst
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ Contents

usage
api
authors
changelog
53 changes: 43 additions & 10 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ To allow upload files to ``../uploads`` directory for all clients via ``/uploads
app = setup_tus(
web.Application(),
upload_path=Path(__file__).parent.parent / "uploads"
upload_path=Path(__file__).parent.parent / "uploads",
)
Understanding tus.io Chunk Size
Expand Down Expand Up @@ -75,6 +75,44 @@ uppy.io Configuration
chunk size **at least 1 byte smaller** than ``client_max_size``. If you'll provide
chunk size equals to client max size upload will not work properly.

CORS Headers
============

At a moment (`Mar 26 2020`), ``aiohttp-tus`` supports setting up CORS Headers for
``aiohttp.web`` application only via `cors_middleware <https://https://aiohttp-middlewares.readthedocs.io/en/latest/usage.html#cors-middleware>`_
from ``aiohttp-middlewares`` package.

As ``aiohttp-tus`` registers `OPTIONS` handlers it doesn't work with
`aiohttp-cors <https://github.com/aio-libs/aiohttp-cors>`_ library cause of known issue
`aio-libs/aiohttp-cors#241 <https://github.com/aio-libs/aiohttp-cors/issues/241>`_.
(`Full discussion <https://github.com/pylotcode/aiohttp-tus/issues/4>`_)

To enable CORS Headers for your ``aiohttp.web`` application, which is using
``aiohttp-tus``, you need to,

1. Install `aiohttp-middlewares <https://aiohttp-middlewares.readthedocs.io>`_
2. In your `app.py`,

.. code-block:: python
from pathlib import Path
from aiohttp import web
from aiohttp_middlewares import cors_middleware
from aiohttp_tus import setup_tus
# Allow CORS Headers for requests from http://localhost:3000
app = web.Application(
middlewares=[
cors_middleware(origins=["http://localhost:3000"])
]
)
setup_tus(
app,
upload_path=Pathlib(__file__).parent.parent / "uploads",
)
User Uploads
============

Expand Down Expand Up @@ -105,8 +143,8 @@ via ``/users/{username}/uploads`` URL,
decorator=upload_user_required,
)
Callback
========
On Upload Done Callback
=======================

There is a possibility to run any coroutine after upload is done. Example below,
illustrates how to achieve that,
Expand All @@ -117,9 +155,7 @@ illustrates how to achieve that,
async def notify_on_upload(
request: web.Request,
resource: Resource,
file_path: Path,
request: web.Request, resource: Resource, file_path: Path,
) -> None:
redis = request.config_dict["redis"]
await redis.rpush("uploaded_files", resource.file_name)
Expand All @@ -144,10 +180,7 @@ achieve anonymous & authenticated uploads in same time for one
base_upload_path = Path(__file__).parent.parent / "uploads"
# Anonymous users uploads
setup_tus(
app,
upload_path=base_upload_path / "anonymous"
)
setup_tus(app, upload_path=base_upload_path / "anonymous")
# Authenticated users uploads
setup_tus(
Expand Down

0 comments on commit 95b5a17

Please sign in to comment.