From 453e6647fde2c601d28787e09ccb0a7d76699e0a Mon Sep 17 00:00:00 2001 From: Maarten Breddels Date: Thu, 20 Jul 2023 13:34:59 +0200 Subject: [PATCH] feat: when the proxy cache dir cannot be written to, disable the proxy (#211) This will let solara work out of the box when the proxy cache dir is not writable, and will not require the user to manually disable the proxy. If the proxy is disabled, the cache dir will not be created and the no endpoint will be registered. --- solara/server/flask.py | 18 ++++++++++-------- solara/server/settings.py | 14 +++++++++++++- solara/server/starlette.py | 3 ++- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/solara/server/flask.py b/solara/server/flask.py index 3722ff0ab..0ea1baced 100644 --- a/solara/server/flask.py +++ b/solara/server/flask.py @@ -187,14 +187,16 @@ def serve_static(path): return send_from_directory(server.solara_static, path) -@blueprint.route(f"/{cdn_helper.cdn_url_path}/") -def cdn(path): - if not allowed(): - abort(401) - cache_directory = settings.assets.proxy_cache_dir - content = cdn_helper.get_data(Path(cache_directory), path) - mime = mimetypes.guess_type(path) - return flask.Response(content, mimetype=mime[0]) +if settings.assets: + + @blueprint.route(f"/{cdn_helper.cdn_url_path}/") + def cdn(path): + if not allowed(): + abort(401) + cache_directory = settings.assets.proxy_cache_dir + content = cdn_helper.get_data(Path(cache_directory), path) + mime = mimetypes.guess_type(path) + return flask.Response(content, mimetype=mime[0]) @blueprint.route("/", defaults={"path": ""}) diff --git a/solara/server/settings.py b/solara/server/settings.py index 8e6bab206..aa59d399d 100644 --- a/solara/server/settings.py +++ b/solara/server/settings.py @@ -3,6 +3,7 @@ import site import sys import uuid +import warnings from enum import Enum from pathlib import Path from typing import Optional @@ -156,7 +157,18 @@ class Config: oauth = OAuth() session = Session() -assets.proxy_cache_dir.mkdir(exist_ok=True, parents=True) +if assets.proxy: + try: + assets.proxy_cache_dir.mkdir(exist_ok=True, parents=True) + except Exception as e: + assets.proxy = False + warnings.warn( + f"Could not create {assets.proxy_cache_dir} due to {e}. We will automatically disable the assets proxy for you. " + "If you want to disable this warning, set SOLARA_ASSETS_PROXY to False (e.g. export SOLARA_ASSETS_PROXY=false)." + ) + # that's ok, the user probably doesn't have permission to create the directory + # in this case, we would need to install solara-assets? + pass if telemetry.server_user_id == "not_set": home = get_solara_home() diff --git a/solara/server/starlette.py b/solara/server/starlette.py index e12fa69ea..1b0b6dc4a 100644 --- a/solara/server/starlette.py +++ b/solara/server/starlette.py @@ -381,7 +381,8 @@ def readyz(request: Request): Route("/", endpoint=root), Route("/{fullpath}", endpoint=root), Route("/_solara/api/close/{connection_id}", endpoint=close, methods=["POST"]), - Mount(f"/{cdn_url_path}", app=StaticCdn(directory=settings.assets.proxy_cache_dir)), + # only enable when the proxy is turned on, otherwise if the directory does not exists we will get an exception + *([Mount(f"/{cdn_url_path}", app=StaticCdn(directory=settings.assets.proxy_cache_dir))] if settings.assets.proxy else []), Mount(f"{prefix}/static/public", app=StaticPublic()), Mount(f"{prefix}/static/assets", app=StaticAssets()), Mount(f"{prefix}/static/nbextensions", app=StaticNbFiles()),