From 01016aee3efcfdab0c921c1370ede8bf2d235cce Mon Sep 17 00:00:00 2001 From: Jakob Schlyter Date: Sun, 22 Dec 2024 12:18:09 +0100 Subject: [PATCH] Add Cache-Control header to get configuration --- nodeman/nodes.py | 5 +++++ nodeman/settings.py | 1 + tests/test_api.py | 1 + 3 files changed, 7 insertions(+) diff --git a/nodeman/nodes.py b/nodeman/nodes.py index bd3ae96..e43c896 100644 --- a/nodeman/nodes.py +++ b/nodeman/nodes.py @@ -356,6 +356,7 @@ async def renew_node( async def get_node_configuration( name: str, request: Request, + response: Response, ) -> NodeConfiguration: """Get node configuration""" @@ -369,4 +370,8 @@ async def get_node_configuration( node_configurations_requested.add(1) + # Cache response for 5 minutes + max_age = request.app.settings.nodes.configuration_ttl + response.headers["Cache-Control"] = f"public, max-age={max_age}" + return res diff --git a/nodeman/settings.py b/nodeman/settings.py index ffcfa52..270f51a 100644 --- a/nodeman/settings.py +++ b/nodeman/settings.py @@ -45,6 +45,7 @@ class NodesSettings(BaseModel): trusted_jwks: FilePath | None = Field(default=None) mqtt_broker: MqttUrl = Field(default="mqtt://localhost") mqtt_topics: dict[str, str] = Field(default={}) + configuration_ttl: int = Field(default=300) class User(BaseModel): diff --git a/tests/test_api.py b/tests/test_api.py index f625cd3..0293ef7 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -153,6 +153,7 @@ def _test_enroll(data_key: JWK, x509_key: PrivateKey, requested_name: str | None node_information = response.json() print(json.dumps(node_information, indent=4)) assert node_information["name"] == name + assert response.headers.get("Cache-Control") is not None ##################### # Get node public key