From 164e22f4567deb538fd3fabfeff4c7d21437e420 Mon Sep 17 00:00:00 2001 From: Miguel BarĂ£o Date: Mon, 18 Feb 2019 23:35:03 +0000 Subject: [PATCH] add redirect command to redirect http to https --- aprendizations/initdb.py | 0 aprendizations/redirect.py | 61 ++++++++++++++++++++++++++++++++++--------------------------- aprendizations/serve.py | 4 ++-- setup.py | 1 + 4 files changed, 37 insertions(+), 29 deletions(-) mode change 100755 => 100644 aprendizations/initdb.py mode change 100755 => 100644 aprendizations/redirect.py diff --git a/aprendizations/initdb.py b/aprendizations/initdb.py old mode 100755 new mode 100644 index b03667f..b03667f --- a/aprendizations/initdb.py +++ b/aprendizations/initdb.py diff --git a/aprendizations/redirect.py b/aprendizations/redirect.py old mode 100755 new mode 100644 index 8caa46d..2922eeb --- a/aprendizations/redirect.py +++ b/aprendizations/redirect.py @@ -4,34 +4,41 @@ from tornado.web import RedirectHandler, Application from tornado.ioloop import IOLoop import argparse -default_url = 'https://bit.xdi.uevora.pt/' -default_port = 8080 - -# --- Commandline argument parsing -argparser = argparse.ArgumentParser( - description='Redirection server. ' - 'Listen on a given port for HTTP requests and responds with a ' - '301 - Moved Permanently - so that the browser is redirected to the ' - 'correct target address.' - ) -argparser.add_argument( - '-p', '--port', type=int, default=8080, - help='Port to listen to (default: 8080)' -) -argparser.add_argument( - '-t', '--target', type=str, default=default_url, - help='Target address (default: https://bit.xdi.uevora.pt)' +def main(): + default_url = 'https://bit.xdi.uevora.pt/' + default_port = 8080 + + # --- Commandline argument parsing + argparser = argparse.ArgumentParser( + description='Redirection server. ' + 'Listen on a given port for HTTP requests and responds with a ' + '301 - Moved Permanently - so that the browser is redirected to the ' + 'correct target address.' + ) + argparser.add_argument( + '-p', '--port', type=int, default=8080, + help='Port to listen to (default: 8080)' ) -arg = argparser.parse_args() -print(f'Redirecting from port {arg.port} to {arg.target}') + argparser.add_argument( + '-t', '--target', type=str, default=default_url, + help='Target address (default: https://bit.xdi.uevora.pt)' + ) + arg = argparser.parse_args() + print(f'Redirecting from port {arg.port} to {arg.target}') + + # --- run server + handlers = [(r'.*', RedirectHandler, {'url': arg.target})] + app = Application(handlers) + app.listen(arg.port) + + try: + IOLoop.current().start() # running... + except Exception: + IOLoop.current().stop() -handlers = [(r'.*', RedirectHandler, {'url': arg.target})] -app = Application(handlers) -app.listen(arg.port) + print('Redirection stopped!') -try: - IOLoop.current().start() # running... -except Exception: - IOLoop.current().stop() -print('Redirection stopped!') +# ---------------------------------------------------------------------------- +if __name__ == "__main__": + main() diff --git a/aprendizations/serve.py b/aprendizations/serve.py index 6126d85..0ce7c69 100644 --- a/aprendizations/serve.py +++ b/aprendizations/serve.py @@ -397,8 +397,8 @@ def get_logger_config(debug=False): 'version': 1, 'formatters': { 'standard': { - 'format': '%(asctime)s %(name)-22s %(levelname)-8s %(message)s', - 'datefmt': '%H:%M', + 'format': '%(asctime)s %(name)-24s %(levelname)-8s %(message)s', + 'datefmt': '%H:%M:%S', }, }, 'handlers': { diff --git a/setup.py b/setup.py index 03db108..d345ba1 100644 --- a/setup.py +++ b/setup.py @@ -27,6 +27,7 @@ setup( 'console_scripts': [ 'aprendizations = aprendizations.serve:main', 'initdb-aprendizations = aprendizations.initdb:main', + 'redirect = aprendizations.redirect:main', ] }, classifiers=[ -- libgit2 0.21.2