commit e6ec6de63f8fe744b811a13ab06c7999216d8fbd Author: Yehuda Deutsch Date: Mon Aug 12 22:26:31 2019 +0300 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..15cf9e6 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM python:3.7-alpine + +MAINTAINER Yehuda Deutsch + +RUN echo 'manylinux1_compatible = True' > /usr/local/lib/python3.7/_manylinux.py +RUN apk add --virtual .build-deps gcc musl-dev \ + && pip install sanic \ + && rm -Rf ~/.cache \ + && apk --purge del .build-deps + +WORKDIR /code +COPY app.py /code/ + +CMD ["python", "app.py"] diff --git a/app.py b/app.py new file mode 100644 index 0000000..b67d48e --- /dev/null +++ b/app.py @@ -0,0 +1,50 @@ +from datetime import datetime + +from sanic import Sanic +from sanic.response import text, json +from sanic.views import HTTPMethodView + + +class IndexView(HTTPMethodView): + def get(self, request): + now = datetime.utcnow() + return text(now.strftime('%Y-%m-%d %H:%M:%S')) + + +class JsonView(HTTPMethodView): + def get(self, request): + now = datetime.utcnow() + return json({ + 'now': now.strftime('%Y-%m-%d %H:%M:%S'), + 'timestamp': int(datetime.timestamp(now)), + }) + + +class Application(object): + app = Sanic() + + def __init__(self, args): + self.args = args + self.app.add_route(IndexView.as_view(), '/') + self.app.add_route(JsonView.as_view(), '/json') + + def run(self): + self.app.run(host=self.args.interface, port=self.args.port) + + +if __name__ == '__main__': + import os + from argparse import ArgumentParser + + default_host = os.environ.get('APP_HOST', '0.0.0.0') + default_port = int(os.environ.get('APP_PORT', 8080)) + default_trusted = os.environ.get('TRUSTED_PROXIES') or None + + parser = ArgumentParser('app.py') + parser.add_argument('--interface', '-i', type=str, help='Interface to listen on', default=default_host) + parser.add_argument('--port', '-p', type=int, help='Port to listen on', default=default_port) + parser.add_argument('--proxies', type=str, help='A list of trusted proxies, comma separated. you can use CIDRs.', default=default_trusted) + args = parser.parse_args() + + zero_if = Application(args) + zero_if.run()