diff --git a/routers/root.py b/routers/root.py index 01e7289e4..45f840e20 100644 --- a/routers/root.py +++ b/routers/root.py @@ -589,20 +589,13 @@ def chat_lib_route(request: Request, integration_id: str, integration_name: str ) -# catch home page - TODO uncomment when home page is ready -# @gui.route(app, "/") -# def home_page(request: Request): -# import static_pages - -# static_content = static_pages.serve("index", None) - -# if static_content.get("redirectUrl"): -# return RedirectResponse(static_content.get("redirectUrl")) - -# if static_content.get("content"): -# return Response( -# content=static_content["content"], -# ) +@gui.route(app, "/") +def home_page(request: Request): + static_content = render_static_content(request, "index", None) + if static_content: + return static_content + else: + return HTTPException(status_code=404) @gui.route( @@ -622,23 +615,31 @@ def recipe_page_or_handle( try: handle = Handle.objects.get_by_name(page_slug) except Handle.DoesNotExist: - import static_pages - - static_content = static_pages.serve(page_slug, path) - if not static_content: + static_content = render_static_content(request, page_slug, path) + if static_content: + return static_content + else: # render recipe page return render_page(request, page_slug, RecipeTabs.run, example_id) + else: + return render_page_for_handle(request, handle) - if static_content.get("redirectUrl"): - return RedirectResponse(static_content.get("redirectUrl")) - if static_content.get("content"): - return Response( - content=static_content["content"], - ) +def render_static_content(request: Request, page_slug: str, path: str | None): + import static_pages - else: - return render_page_for_handle(request, handle) + static_data = static_pages.serve(page_slug, path) + if not static_data: + # render recipe page + return + + if static_data.get("redirectUrl"): + return RedirectResponse(static_data.get("redirectUrl")) + + if static_data.get("content"): + return Response( + content=static_data["content"], + ) def render_page_for_handle(request: Request, handle: Handle): diff --git a/static_pages.py b/static_pages.py index 2ba9a4e4b..ed7a5a3c4 100644 --- a/static_pages.py +++ b/static_pages.py @@ -1,4 +1,3 @@ -import django.urls import gooey_gui as gui from daras_ai_v2 import settings import io @@ -29,16 +28,17 @@ def serve(page_slug: str, file_path: str): blob_path = "" if page_slug and not file_path: - # If page_slug is provided, then it's a page + # If only page_slug is provided, then it's a page blob_path = f"{WEBSITE_FOLDER_PATH}/{page_slug}.html" elif file_path: # fetch every file from root blob_path = f"{WEBSITE_FOLDER_PATH}/{extract_file_name(file_path)}" - if not (blob_path.endswith(".html")): + # check .css cause fonts don't work in redirect + if not (blob_path.endswith(".html") or blob_path.endswith(".css")): blob = bucket.get_blob(blob_path) if not blob or not blob.exists(): - return None # 404 + return None # don't send 404 here, let the next handler handle it # redirect every path that doesn't end with .html return dict( redirectUrl=f"https://storage.googleapis.com/{settings.GS_BUCKET_NAME}/{blob_path}" @@ -80,7 +80,7 @@ def extract_zip_to_gcloud(self): file_data = z.read(file_info) file_name = extract_file_name( file_info.filename - ) # tp put everything at root + ) # put everything at root blob_path = f"{WEBSITE_FOLDER_PATH}/{file_name}" blob = bucket.blob(blob_path) content_type = (