|
| 1 | +from __future__ import annotations |
| 2 | + |
1 | 3 | import logging
|
2 | 4 | from functools import partial
|
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +from asgi_middleware_static_file import ASGIMiddlewareStaticFile # type: ignore |
| 8 | +from asgi_webdav.middleware.cors import ASGIMiddlewareCORS # type: ignore |
| 9 | +from asgi_webdav import __name__ as app_name # type: ignore |
| 10 | +from asgi_webdav import __version__ # type: ignore |
3 | 11 |
|
4 | 12 | try:
|
5 |
| - from asgi_webdav.config import init_config_from_obj # type: ignore |
| 13 | + from asgi_webdav.config import ( # type: ignore |
| 14 | + get_config, |
| 15 | + init_config_from_file, |
| 16 | + init_config_from_obj, |
| 17 | + ) |
6 | 18 | from asgi_webdav.constants import DAV_METHODS, AppEntryParameters # type: ignore
|
7 |
| - from asgi_webdav.server import get_asgi_app # type: ignore |
| 19 | + from asgi_webdav.server import Server # type: ignore |
8 | 20 |
|
9 | 21 | asgi_webdav_installed = True
|
10 | 22 | except BaseException:
|
@@ -40,3 +52,58 @@ def __init__(self, app: App, webdav_config: WebDAVConfig):
|
40 | 52 | webdav_aep = AppEntryParameters()
|
41 | 53 | webdav_app = get_asgi_app(aep=webdav_aep, config_obj=webdav_conf)
|
42 | 54 | app.add_middleware(partial(WebDAVApp, webdav_app=webdav_app))
|
| 55 | + |
| 56 | + |
| 57 | +# this is to get rid of asgi-webdav's logging configuration, see: |
| 58 | +# https://github.com/rexzhang/asgi-webdav/blob/53735fa67030e1db0d610deb58d2ebfedbdd7c3b/asgi_webdav/server.py#L99 |
| 59 | +def get_asgi_app(aep: AppEntryParameters, config_obj: dict | None = None): |
| 60 | + """create ASGI app""" |
| 61 | + # init config |
| 62 | + if aep.config_file is not None: |
| 63 | + init_config_from_file(aep.config_file) |
| 64 | + if config_obj is not None: |
| 65 | + init_config_from_obj(config_obj) |
| 66 | + |
| 67 | + config = get_config() |
| 68 | + config.update_from_app_args_and_env_and_default_value(aep=aep) |
| 69 | + |
| 70 | + # create ASGI app |
| 71 | + app = Server(config) |
| 72 | + |
| 73 | + # route /_/static |
| 74 | + app = ASGIMiddlewareStaticFile( |
| 75 | + app=app, |
| 76 | + static_url="_/static", |
| 77 | + static_root_paths=[Path(__file__).parent.joinpath("static")], |
| 78 | + ) |
| 79 | + |
| 80 | + # CORS |
| 81 | + if config.cors.enable: |
| 82 | + app = ASGIMiddlewareCORS( |
| 83 | + app=app, |
| 84 | + allow_url_regex=config.cors.allow_url_regex, |
| 85 | + allow_origins=config.cors.allow_origins, |
| 86 | + allow_origin_regex=config.cors.allow_origin_regex, |
| 87 | + allow_methods=config.cors.allow_methods, |
| 88 | + allow_headers=config.cors.allow_headers, |
| 89 | + allow_credentials=config.cors.allow_credentials, |
| 90 | + expose_headers=config.cors.expose_headers, |
| 91 | + preflight_max_age=config.cors.preflight_max_age, |
| 92 | + ) |
| 93 | + |
| 94 | + # config sentry |
| 95 | + if config.sentry_dsn: |
| 96 | + try: |
| 97 | + import sentry_sdk # type: ignore |
| 98 | + from sentry_sdk.integrations.asgi import SentryAsgiMiddleware # type: ignore |
| 99 | + |
| 100 | + sentry_sdk.init( |
| 101 | + dsn=config.sentry_dsn, |
| 102 | + release=f"{app_name}@{__version__}", |
| 103 | + ) |
| 104 | + app = SentryAsgiMiddleware(app) |
| 105 | + |
| 106 | + except ImportError as e: |
| 107 | + logger.warning(e) |
| 108 | + |
| 109 | + return app |
0 commit comments