Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ddd0b50

Browse files
committed
Add a ping endpoint to webhook service
As noted in #25, the webhook was down for some period. The ping endpoint allows some external check to verify that it is running. If Caddy cannot connect to the webhook, it will return 502 instead of 200.
1 parent f8ae94a commit ddd0b50

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

templates/Caddyfile.j2

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ http://{{ caddy.addresses.webhook }} {
4646
}
4747
}
4848

49+
handle /ping {
50+
reverse_proxy * localhost:1234 {
51+
# Don't leak out internal problems.
52+
@error status 4xx 5xx
53+
handle_response @error {
54+
error 503
55+
}
56+
}
57+
}
58+
4959
handle {
5060
error 404
5161
}

webhook/test_webhook.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ async def test_update_repo(tmp_path_factory):
6565
assert dest_commit == src_commit
6666

6767

68+
async def test_ping(aiohttp_client, monkeypatch, tmp_path):
69+
"""Test ping always works."""
70+
monkeypatch.setenv('SITE_DIR', str(tmp_path))
71+
client = await aiohttp_client(create_app())
72+
73+
resp = await client.get('/ping')
74+
assert resp.status == 200
75+
76+
6877
async def test_github_webhook_errors(aiohttp_client, monkeypatch, tmp_path):
6978
"""Test invalid inputs to webhook."""
7079
monkeypatch.setenv('SITE_DIR', str(tmp_path))

webhook/webhook.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ async def github_webhook(request: web.Request):
173173
return web.Response(status=200)
174174

175175

176+
async def ping(request: web.Request):
177+
"""Respond to a ping, thus verifying the webhook service is alive."""
178+
return web.Response(status=200)
179+
180+
176181
def create_app():
177182
"""Create the aiohttp app and setup routes."""
178183
site_dir = Path(os.environ.get('SITE_DIR', 'sites')).resolve()
@@ -182,6 +187,7 @@ def create_app():
182187
app['site_dir'] = site_dir
183188
app.add_routes([
184189
web.post('/gh/{repo}', github_webhook),
190+
web.get('/ping', ping),
185191
])
186192
return app
187193

0 commit comments

Comments
 (0)