Compare commits

..

1 Commits

Author SHA1 Message Date
57baf8d2fb Redirector asgi rewrite
All checks were successful
Test before pr merge / test-translation-lint (pull_request) Successful in 36s
Test before pr merge / test-lint (pull_request) Successful in 38s
Test before pr merge / test-before-merge (pull_request) Successful in 1m37s
2025-03-08 10:10:21 +01:00

View File

@ -29,7 +29,9 @@ async def handle_request(request: Request):
path = request.path_params["path"] path = request.path_params["path"]
short_url = await shortUrlDao.find_single_by({ShortUrl.short_url: path}) short_url = await shortUrlDao.find_single_by({ShortUrl.short_url: path})
if short_url is None: if short_url is None:
return templates.TemplateResponse("404.html", {"request": request}, status_code=404) return templates.TemplateResponse(
"404.html", {"request": request}, status_code=404
)
domains = Environment.get("DOMAINS", list[str], []) domains = Environment.get("DOMAINS", list[str], [])
domain = await short_url.domain domain = await short_url.domain
@ -43,10 +45,12 @@ async def handle_request(request: Request):
domain_strict_mode = Environment.get("DOMAIN_STRICT_MODE", bool, False) domain_strict_mode = Environment.get("DOMAIN_STRICT_MODE", bool, False)
if domain is not None and ( if domain is not None and (
domain.name not in domains domain.name not in domains
or (domain_strict_mode and not host.endswith(domain.name)) or (domain_strict_mode and not host.endswith(domain.name))
): ):
return templates.TemplateResponse("404.html", {"request": request}, status_code=404) return templates.TemplateResponse(
"404.html", {"request": request}, status_code=404
)
user_agent = request.headers.get("User-Agent", "").lower() user_agent = request.headers.get("User-Agent", "").lower()
@ -58,7 +62,11 @@ async def handle_request(request: Request):
return templates.TemplateResponse( return templates.TemplateResponse(
"redirect.html", "redirect.html",
{"request": request, "key": short_url.short_url, "target_url": _get_redirect_url(short_url.target_url)}, {
"request": request,
"key": short_url.short_url,
"target_url": _get_redirect_url(short_url.target_url),
},
) )
return await _handle_short_url(request, path, short_url) return await _handle_short_url(request, path, short_url)
@ -99,7 +107,7 @@ async def configure():
routes = [ routes = [
Route("/", endpoint=index), Route("/", endpoint=index),
Mount('/static', StaticFiles(directory='static'), name='static'), Mount("/static", StaticFiles(directory="static"), name="static"),
Route("/{path:path}", endpoint=handle_request), Route("/{path:path}", endpoint=handle_request),
] ]
@ -121,4 +129,4 @@ def main():
if __name__ == "__main__": if __name__ == "__main__":
main() main()