Skip to content

Cache static routes #457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions diracx-routers/src/diracx/routers/auth/well_known.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from cachetools import TTLCache
from fastapi import Request

from diracx.core.models import Metadata, OpenIDConfiguration
Expand All @@ -16,26 +17,36 @@
router = DiracxRouter(require_auth=False, path_root="")


_static_cache: TTLCache = TTLCache(maxsize=5, ttl=60)


@router.get("/openid-configuration")
async def get_openid_configuration(
request: Request,
config: Config,
settings: AuthSettings,
) -> OpenIDConfiguration:
"""OpenID Connect discovery endpoint."""
return await get_openid_configuration_bl(
str(request.url_for("get_oidc_token")),
str(request.url_for("userinfo")),
str(request.url_for("initiate_authorization_flow")),
str(request.url_for("initiate_device_flow")),
config,
settings,
)
cached_hash = f"openid-configuration:{config._hexsha}"
if cached_hash not in _static_cache:
_static_cache[cached_hash] = await get_openid_configuration_bl(
str(request.url_for("get_oidc_token")),
str(request.url_for("userinfo")),
str(request.url_for("initiate_authorization_flow")),
str(request.url_for("initiate_device_flow")),
config,
settings,
)
return _static_cache[cached_hash]


@router.get("/dirac-metadata")
async def get_installation_metadata(
config: Config,
) -> Metadata:
"""Get metadata about the dirac installation."""
return await get_installation_metadata_bl(config)
cached_hash = f"dirac-metadata:{config._hexsha}"
if cached_hash not in _static_cache:
_static_cache[cached_hash] = await get_installation_metadata_bl(config)

return _static_cache[cached_hash]
Loading