Skip to content

Commit 5a2ec76

Browse files
author
Adam Wright
authored
Merge pull request #55 from reactome/chainlit-uri-envvar
use CHAINLIT_URI environment variable
2 parents 0b2eb87 + ee15cab commit 5a2ec76

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

bin/chat-fastapi.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
app = FastAPI()
1414

15+
CHAINLIT_URI = os.getenv("CHAINLIT_URI")
16+
1517
CLOUDFLARE_SECRET_KEY = os.getenv("CLOUDFLARE_SECRET_KEY")
1618
CLOUDFLARE_SITE_KEY = os.getenv("CLOUDFLARE_SITE_KEY")
1719

@@ -43,7 +45,11 @@ async def verify_captcha_middleware(request: Request, call_next):
4345
# Allow access to CAPTCHA pages and static files
4446
if (
4547
request.url.path
46-
in ["/chat/verify_captcha", "/chat/verify_captcha_page", "/chat/static"]
48+
in [
49+
f"{CHAINLIT_URI}/verify_captcha",
50+
f"{CHAINLIT_URI}/verify_captcha_page",
51+
f"{CHAINLIT_URI}/static",
52+
]
4753
or request.url.path.startswith("/static")
4854
or not os.getenv("CLOUDFLARE_SECRET_KEY")
4955
):
@@ -55,22 +61,22 @@ async def verify_captcha_middleware(request: Request, call_next):
5561

5662
# If CAPTCHA is not verified, block access
5763
if not captcha_verified or not verify_secure_cookie(captcha_verified):
58-
return RedirectResponse(url="/chat/verify_captcha_page")
64+
return RedirectResponse(url=f"{CHAINLIT_URI}/verify_captcha_page")
5965

6066
response = await call_next(request)
6167
return response
6268

6369

6470
# Serve the CAPTCHA verification page (basic HTML form)
65-
@app.get("/chat/verify_captcha_page")
71+
@app.get(f"{CHAINLIT_URI}/verify_captcha_page")
6672
async def captcha_page():
6773
html_content = f"""
6874
<html>
6975
<head>
7076
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>
7177
</head>
7278
<body>
73-
<form id="captcha-form" action="/chat/verify_captcha" method="post">
79+
<form id="captcha-form" action="{CHAINLIT_URI}/verify_captcha" method="post">
7480
<div class="cf-turnstile" data-sitekey="{os.getenv('CLOUDFLARE_SITE_KEY')}" data-callback="onSubmit"></div>
7581
</form>
7682
<script>
@@ -92,7 +98,7 @@ async def captcha_page():
9298
return Response(content=html_content, media_type="text/html")
9399

94100

95-
@app.post("/chat/verify_captcha")
101+
@app.post(f"{CHAINLIT_URI}/verify_captcha")
96102
async def verify_captcha(request: Request):
97103
form_data = await request.form()
98104
cf_turnstile_response = form_data.get("cf-turnstile-response")
@@ -117,7 +123,7 @@ async def verify_captcha(request: Request):
117123

118124
# Set a signed cookie to mark CAPTCHA as verified
119125
cookie_value = create_secure_cookie(cf_turnstile_response)
120-
redirect_response = RedirectResponse(url="/chat", status_code=303)
126+
redirect_response = RedirectResponse(url=f"{CHAINLIT_URI}", status_code=303)
121127
redirect_response.set_cookie(
122128
key="captcha_verified",
123129
value=cookie_value,
@@ -129,4 +135,4 @@ async def verify_captcha(request: Request):
129135
return redirect_response
130136

131137

132-
mount_chainlit(app=app, target="bin/chat-chainlit.py", path=os.getenv("CHAINLIT_URI"))
138+
mount_chainlit(app=app, target="bin/chat-chainlit.py", path=CHAINLIT_URI)

env_template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ CLOUDFLARE_SECRET_KEY=
1212
CLOUDFLARE_SITE_KEY=0x4AAAAAAAkzFQ5GRs2toYuv
1313
CHAINLIT_IMAGE=public.ecr.aws/reactome/reactome-chatbot:latest
1414
#CHAINLIT_IMAGE=reactome-chatbot
15+
CHAINLIT_URI=/chat

0 commit comments

Comments
 (0)