Initial commit

This commit is contained in:
Yehuda Deutsch 2019-08-12 22:26:31 +03:00
commit e6ec6de63f
3 changed files with 64 additions and 0 deletions

0
.gitignore vendored Normal file
View file

14
Dockerfile Normal file
View file

@ -0,0 +1,14 @@
FROM python:3.7-alpine
MAINTAINER Yehuda Deutsch <yeh@uda.co.il>
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"]

50
app.py Normal file
View file

@ -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()