Skip to content
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

Add /healthz endpoint and don't log successful responses #51

Merged
merged 2 commits into from Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion webtranslate/main.py
Expand Up @@ -3,6 +3,8 @@
"""
import logging

from wsgiref.simple_server import WSGIRequestHandler

from . import (
bottle,
config,
Expand Down Expand Up @@ -38,6 +40,14 @@
bottle.TEMPLATE_PATH = ["./views/"]


class NoLog200HandlerHandler(WSGIRequestHandler):
def log_message(self, format, *args):
# Only log if the status was not successful.
if not (200 <= int(args[1]) < 400):
# Use log.info() instead of parent call, as parent call uses stderr.write().
log.info("%s - - [%s] %s", self.address_string(), self.log_date_time_string(), format % args)
TrueBrain marked this conversation as resolved.
Show resolved Hide resolved


def run():
# Load basic settings from the configuration (in particular, language meta-data directories).
config.cfg = config.Config("config.xml")
Expand Down Expand Up @@ -78,7 +88,13 @@ def run():

# With 'mod_wsgi', application does not run from here.
if config.cfg.server_mode != "mod_wsgi":
bottle.run(reloader=False, debug=debug, host=config.cfg.server_host, port=config.cfg.server_port)
bottle.run(
reloader=False,
debug=debug,
host=config.cfg.server_host,
port=config.cfg.server_port,
handler_class=NoLog200HandlerHandler,
)


if __name__ == "__main__":
Expand Down
5 changes: 5 additions & 0 deletions webtranslate/pages/root.py
Expand Up @@ -18,3 +18,8 @@ def root(userauth):
@route("/robots.txt", method="GET")
def robots():
return HTTPResponse("User-agent: *\nDisallow: /", status=200, headers={"content-type": "text/plain"})


@route("/healthz", method="GET")
def healthz():
return HTTPResponse("200: OK", status=200, headers={"content-type": "text/plain"})