rexzhang / asgi-webdav

An asynchronous WebDAV server implementation, Support multi-provider, multi-account and permission control.
https://rexzhang.github.io/asgi-webdav/
MIT License
75 stars 14 forks source link

FastAPI integration #21

Closed davidbrochart closed 1 year ago

davidbrochart commented 2 years ago

I'm new to WebDAV, but seeing that asgi-webdav is built on top of ASGI, does it mean that it could work with FastAPI? Or is it an entirely separate server?

rexzhang commented 2 years ago

Yes, both! How can I help you?

davidbrochart commented 2 years ago

Great, thanks for the feedback. How would it look like to serve the WebDAV routes inside an existing FastAPI application?

rexzhang commented 2 years ago

Sorry for the delay, I have some projects that must be done. I have added a example, maybe it helps you.

davidbrochart commented 2 years ago

Sorry for the delay, I have some projects that must be done.

No worries!

I have added a example, maybe it helps you.

Thanks a lot!

davidbrochart commented 1 year ago

I was wondering if it is possible to add asgi-webdav as a FastAPI middleware, instead of using the method described in the examples?

rexzhang commented 1 year ago

It's a good idea, I will try. But I'm not sure when to deal with it

davidbrochart commented 1 year ago

Something like that should work:

from functools import partial
from asgi_webdav.constants import DAV_METHODS, AppEntryParameters
from asgi_webdav.server import get_asgi_app as get_webdav_asgi_app
from fastapi import FastAPI

webdav_config = {
    "provider_mapping": [
        {
            "prefix": "/webdav",
            "uri": "file://.",
        },
    ]
}
webdav_aep = AppEntryParameters()
webdav_app = get_webdav_asgi_app(aep=webdav_aep, config_obj=webdav_config)

class WebDAVApp:
    def __init__(self, app, webdav_app):
        self._app = app
        self._webdav_app = webdav_app

    async def __call__(self, scope, receive, send):
        if scope.get("method") in DAV_METHODS and scope.get("path").startswith("/webdav"):
            return await self._webdav_app(scope, receive, send)
        return await self._app(scope, receive, send)

app = FastAPI()
app.add_middleware(partial(WebDAVApp, webdav_app=webdav_app))
rexzhang commented 1 year ago

Nice! Welcome PR

davidbrochart commented 1 year ago

Done in #30.