Skip to content

Commit

Permalink
make static file route from request.url
Browse files Browse the repository at this point in the history
  • Loading branch information
anish-work committed Jul 30, 2024
1 parent 3f42a08 commit 4026136
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
10 changes: 8 additions & 2 deletions daras_ai_v2/static_pages.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import io
from starlette.responses import Response
from static_pages.models import StaticPage
from google.cloud import storage

Expand Down Expand Up @@ -51,12 +52,17 @@ def get_all_styles(links: list, uid: str):
return f"<style>{styles}</style>"


def serve(page_slug: str, file_path: str = None):
def serve(request: Response, page_slug: str):
static_page = StaticPage.objects.get(path=page_slug)

if not static_page:
return None

file_path = request.url.path
if not file_path == f"/{page_slug}/":
file_path = file_path[len(f"/{page_slug}/") :]
else:
file_path = None

uid = static_page.uid
bucket = gcs_bucket()

Expand Down
9 changes: 6 additions & 3 deletions routers/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,23 +578,26 @@ def chat_lib_route(request: Request, integration_id: str, integration_name: str
app,
"/{page_slug}/",
"/{page_slug}/{run_slug}/",
"/{page_slug}/{run_slug}/{path}",
"/{page_slug}/{run_slug}-{example_id}/",
"/{page_slug}/{path:path}",
)
def recipe_page_or_handle(
request: Request,
page_slug: str,
run_slug: str = None,
example_id: str = None,
path: str = None,
example_id: str = None,
):
try:
handle = Handle.objects.get_by_name(page_slug)
except Handle.DoesNotExist:
try:
import daras_ai_v2.static_pages as static_pages

html, file_path = static_pages.serve(page_slug=page_slug, file_path=path)
html, file_path = static_pages.serve(
request=request,
page_slug=page_slug,
)

if file_path:
return RedirectResponse(file_path)
Expand Down

0 comments on commit 4026136

Please sign in to comment.