1212
1313app = FastAPI ()
1414
15+ CHAINLIT_URI = os .getenv ("CHAINLIT_URI" )
16+
1517CLOUDFLARE_SECRET_KEY = os .getenv ("CLOUDFLARE_SECRET_KEY" )
1618CLOUDFLARE_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" )
6672async 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" )
96102async 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 )
0 commit comments