From bde0f0b73d227636adf40d849fd281451136c476 Mon Sep 17 00:00:00 2001 From: Gustavo Carneiro Date: Sat, 23 Mar 2024 19:05:21 +0000 Subject: [PATCH] add web.headers option, to allow control of response headers --- example/adhoc.yacron.d/test-utc.yaml | 2 ++ yacron/config.py | 7 ++++++- yacron/cron.py | 19 +++++++++++++++---- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/example/adhoc.yacron.d/test-utc.yaml b/example/adhoc.yacron.d/test-utc.yaml index a01b478..a381676 100644 --- a/example/adhoc.yacron.d/test-utc.yaml +++ b/example/adhoc.yacron.d/test-utc.yaml @@ -2,6 +2,8 @@ web: listen: - http://127.0.0.1:8080 - unix:///tmp/yacron.sock + headers: + x-foobar: zbr defaults: shell: /bin/bash diff --git a/yacron/config.py b/yacron/config.py index 0e31b44..a38f86f 100644 --- a/yacron/config.py +++ b/yacron/config.py @@ -248,7 +248,12 @@ class ConfigError(Exception): { Opt("defaults"): Map(_job_defaults_common), Opt("jobs"): Seq(Map(_job_schema_dict)), - Opt("web"): Map({"listen": Seq(Str())}), + Opt("web"): Map( + { + "listen": Seq(Str()), + Opt("headers"): MapPattern(Str(), Str()), + } + ), Opt("include"): Seq(Str()), Opt("logging"): Map( { diff --git a/yacron/cron.py b/yacron/cron.py index 5a8e62c..c35937b 100644 --- a/yacron/cron.py +++ b/yacron/cron.py @@ -167,9 +167,14 @@ def update_config(self) -> YacronConfig: return config async def _web_get_version(self, request: web.Request) -> web.Response: - return web.Response(text=yacron.version.version) + assert self.web_config is not None + return web.Response( + text=yacron.version.version, + headers=self.web_config.get("headers", None), + ) async def _web_get_status(self, request: web.Request) -> web.Response: + assert self.web_config is not None out = [] for name, job in self.cron_jobs.items(): running = self.running_jobs.get(name, None) @@ -200,7 +205,9 @@ async def _web_get_status(self, request: web.Request) -> web.Response: } ) if request.headers.get("Accept") == "application/json": - return web.json_response(out) + return web.json_response( + out, headers=self.web_config.get("headers", None) + ) else: lines = [] for jobstat in out: # type: Dict[str, Any] @@ -223,16 +230,20 @@ async def _web_get_status(self, request: web.Request) -> web.Response: name=jobstat["job"], status=status ) ) - return web.Response(text="\n".join(lines)) + return web.Response( + text="\n".join(lines), + headers=self.web_config.get("headers", None), + ) async def _web_start_job(self, request: web.Request) -> web.Response: + assert self.web_config is not None name = request.match_info["name"] try: job = self.cron_jobs[name] except KeyError as ex: raise web.HTTPNotFound() from ex await self.maybe_launch_job(job) - return web.Response() + return web.Response(headers=self.web_config.get("headers", None)) async def start_stop_web_app(self, web_config: Optional[WebConfig]): if self.web_runner is not None and (