github.com/nathants/docker-trace@v0.0.0-20220831131939-668bc05a257b/examples/python3-web/app.py (about)

     1  #!/usr/bin/env python3
     2  import logging
     3  import tornado.ioloop
     4  import web
     5  import ssl
     6  import subprocess
     7  
     8  check_call = lambda *a: subprocess.check_call(' '.join(map(str, a)), shell=True, executable='/bin/bash', stderr=subprocess.STDOUT)
     9  
    10  logging.basicConfig(level='INFO')
    11  
    12  async def handler(request: web.Request) -> web.Response:
    13      token = request['kwargs']['token']
    14      return {'code': 200, 'body': f'{token}'}
    15  
    16  async def fallback_handler(request: web.Request) -> web.Response:
    17      route = request['args'][0]
    18      return {'code': 200, 'body': f'no such route: /{route}, try: /hello/XYZ'}
    19  
    20  options = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
    21  options.load_cert_chain('ssl.crt', 'ssl.key')
    22  
    23  routes = [('/hello/:token', {'get': handler}),
    24            ('/(.*)',         {'get': fallback_handler})]
    25  
    26  app = web.app(routes)
    27  server = tornado.httpserver.HTTPServer(app, ssl_options=options)
    28  server.bind(8080)
    29  server.start(0)
    30  tornado.ioloop.IOLoop.current().start()