Skip to content

Commit ab09a2e

Browse files
Fix fps-webdav logging config (#317)
1 parent 3dafbe4 commit ab09a2e

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

plugins/webdav/fps_webdav/routes.py

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1+
from __future__ import annotations
2+
13
import logging
24
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
311

412
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+
)
618
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
820

921
asgi_webdav_installed = True
1022
except BaseException:
@@ -40,3 +52,58 @@ def __init__(self, app: App, webdav_config: WebDAVConfig):
4052
webdav_aep = AppEntryParameters()
4153
webdav_app = get_asgi_app(aep=webdav_aep, config_obj=webdav_conf)
4254
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

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ pre-install-commands = [
6767
"pip install -e ./plugins/terminals",
6868
"pip install -e ./plugins/yjs",
6969
"pip install -e ./plugins/resource_usage",
70+
"pip install -e ./plugins/webdav[test]",
7071
]
7172
features = ["test"]
7273

@@ -97,7 +98,7 @@ frontend = ["jupyterlab", "retrolab"]
9798
auth = ["noauth", "auth", "auth_fief"]
9899

99100
[tool.hatch.envs.dev.scripts]
100-
test = "pytest ./tests -v"
101+
test = "pytest ./tests plugins/webdav/tests -v"
101102
lint = [
102103
"black --line-length 100 jupyverse ./plugins",
103104
"isort --profile=black jupyverse ./plugins",
@@ -112,6 +113,7 @@ typecheck0 = """mypy --no-incremental \
112113
./plugins/terminals \
113114
./plugins/yjs \
114115
./plugins/resource_usage \
116+
./plugins/webdav \
115117
"""
116118

117119
[tool.hatch.envs.docs]

0 commit comments

Comments
 (0)