time-go/app.py
2019-08-12 22:26:31 +03:00

50 lines
1.5 KiB
Python

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