Skip to content

Commit

Permalink
add web.headers option, to allow control of response headers
Browse files Browse the repository at this point in the history
  • Loading branch information
gjcarneiro committed Mar 23, 2024
1 parent 4f7936a commit bde0f0b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
2 changes: 2 additions & 0 deletions example/adhoc.yacron.d/test-utc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ web:
listen:
- http://127.0.0.1:8080
- unix:///tmp/yacron.sock
headers:
x-foobar: zbr

defaults:
shell: /bin/bash
Expand Down
7 changes: 6 additions & 1 deletion yacron/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down
19 changes: 15 additions & 4 deletions yacron/cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand All @@ -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 (
Expand Down

0 comments on commit bde0f0b

Please sign in to comment.