Skip to content

Commit

Permalink
feat: when the proxy cache dir cannot be written to, disable the proxy (
Browse files Browse the repository at this point in the history
#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.
  • Loading branch information
maartenbreddels authored Jul 20, 2023
1 parent b632d4f commit 453e664
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
18 changes: 10 additions & 8 deletions solara/server/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,16 @@ def serve_static(path):
return send_from_directory(server.solara_static, path)


@blueprint.route(f"/{cdn_helper.cdn_url_path}/<path: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}/<path: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": ""})
Expand Down
14 changes: 13 additions & 1 deletion solara/server/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import site
import sys
import uuid
import warnings
from enum import Enum
from pathlib import Path
from typing import Optional
Expand Down Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion solara/server/starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down

0 comments on commit 453e664

Please sign in to comment.