Skip to content

Commit

Permalink
Fix fps-webdav logging config (#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart authored Jun 6, 2023
1 parent 3dafbe4 commit ab09a2e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
71 changes: 69 additions & 2 deletions plugins/webdav/fps_webdav/routes.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
from __future__ import annotations

import logging
from functools import partial
from pathlib import Path

from asgi_middleware_static_file import ASGIMiddlewareStaticFile # type: ignore
from asgi_webdav.middleware.cors import ASGIMiddlewareCORS # type: ignore
from asgi_webdav import __name__ as app_name # type: ignore
from asgi_webdav import __version__ # type: ignore

try:
from asgi_webdav.config import init_config_from_obj # type: ignore
from asgi_webdav.config import ( # type: ignore
get_config,
init_config_from_file,
init_config_from_obj,
)
from asgi_webdav.constants import DAV_METHODS, AppEntryParameters # type: ignore
from asgi_webdav.server import get_asgi_app # type: ignore
from asgi_webdav.server import Server # type: ignore

asgi_webdav_installed = True
except BaseException:
Expand Down Expand Up @@ -40,3 +52,58 @@ def __init__(self, app: App, webdav_config: WebDAVConfig):
webdav_aep = AppEntryParameters()
webdav_app = get_asgi_app(aep=webdav_aep, config_obj=webdav_conf)
app.add_middleware(partial(WebDAVApp, webdav_app=webdav_app))


# this is to get rid of asgi-webdav's logging configuration, see:
# https://github.com/rexzhang/asgi-webdav/blob/53735fa67030e1db0d610deb58d2ebfedbdd7c3b/asgi_webdav/server.py#L99
def get_asgi_app(aep: AppEntryParameters, config_obj: dict | None = None):
"""create ASGI app"""
# init config
if aep.config_file is not None:
init_config_from_file(aep.config_file)
if config_obj is not None:
init_config_from_obj(config_obj)

config = get_config()
config.update_from_app_args_and_env_and_default_value(aep=aep)

# create ASGI app
app = Server(config)

# route /_/static
app = ASGIMiddlewareStaticFile(
app=app,
static_url="_/static",
static_root_paths=[Path(__file__).parent.joinpath("static")],
)

# CORS
if config.cors.enable:
app = ASGIMiddlewareCORS(
app=app,
allow_url_regex=config.cors.allow_url_regex,
allow_origins=config.cors.allow_origins,
allow_origin_regex=config.cors.allow_origin_regex,
allow_methods=config.cors.allow_methods,
allow_headers=config.cors.allow_headers,
allow_credentials=config.cors.allow_credentials,
expose_headers=config.cors.expose_headers,
preflight_max_age=config.cors.preflight_max_age,
)

# config sentry
if config.sentry_dsn:
try:
import sentry_sdk # type: ignore
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware # type: ignore

sentry_sdk.init(
dsn=config.sentry_dsn,
release=f"{app_name}@{__version__}",
)
app = SentryAsgiMiddleware(app)

except ImportError as e:
logger.warning(e)

return app
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pre-install-commands = [
"pip install -e ./plugins/terminals",
"pip install -e ./plugins/yjs",
"pip install -e ./plugins/resource_usage",
"pip install -e ./plugins/webdav[test]",
]
features = ["test"]

Expand Down Expand Up @@ -97,7 +98,7 @@ frontend = ["jupyterlab", "retrolab"]
auth = ["noauth", "auth", "auth_fief"]

[tool.hatch.envs.dev.scripts]
test = "pytest ./tests -v"
test = "pytest ./tests plugins/webdav/tests -v"
lint = [
"black --line-length 100 jupyverse ./plugins",
"isort --profile=black jupyverse ./plugins",
Expand All @@ -112,6 +113,7 @@ typecheck0 = """mypy --no-incremental \
./plugins/terminals \
./plugins/yjs \
./plugins/resource_usage \
./plugins/webdav \
"""

[tool.hatch.envs.docs]
Expand Down

0 comments on commit ab09a2e

Please sign in to comment.