From 0d62a7625b1ae01eb2cc1e95885ef675061661e5 Mon Sep 17 00:00:00 2001 From: pictuga Date: Sun, 23 Aug 2020 18:50:18 +0200 Subject: [PATCH] Define http port via env vars as well --- README.md | 2 +- morss/__main__.py | 21 +++++---------------- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index a325e87..089266e 100644 --- a/README.md +++ b/README.md @@ -214,7 +214,7 @@ without any argument, on port 8080. morss ``` -You can change the port like this `morss 9000`. +You can change the port using environment variables like this `PORT=9000 morss`. #### Passing arguments diff --git a/morss/__main__.py b/morss/__main__.py index 3c2349a..60fae7e 100644 --- a/morss/__main__.py +++ b/morss/__main__.py @@ -12,7 +12,7 @@ import wsgiref.simple_server import wsgiref.handlers -PORT = 8080 +PORT = int(os.getenv('PORT', 8080)) def isInt(string): @@ -26,7 +26,7 @@ def isInt(string): def main(): if 'REQUEST_URI' in os.environ: - # mod_cgi + # mod_cgi (w/o file handler) app = wsgi.cgi_app app = wsgi.cgi_dispatcher(app) @@ -35,19 +35,8 @@ def main(): wsgiref.handlers.CGIHandler().run(app) - elif len(sys.argv) <= 1 or isInt(sys.argv[1]): - # start internal (basic) http server - - if len(sys.argv) > 1 and isInt(sys.argv[1]): - argPort = int(sys.argv[1]) - if argPort > 0: - port = argPort - - else: - raise MorssException('Port must be positive integer') - - else: - port = PORT + elif len(sys.argv) <= 1: + # start internal (basic) http server (w/ file handler) app = wsgi.cgi_app app = wsgi.cgi_file_handler(app) @@ -56,7 +45,7 @@ def main(): app = wsgi.cgi_encode(app) print('Serving http://localhost:%s/' % port) - httpd = wsgiref.simple_server.make_server('', port, app) + httpd = wsgiref.simple_server.make_server('', PORT, app) httpd.serve_forever() else: