From 2fc7cd391cb3d012a1e268861fb3dcaa832edd40 Mon Sep 17 00:00:00 2001 From: pictuga Date: Wed, 30 Sep 2020 23:24:51 +0200 Subject: [PATCH] Shift __main__'s wsgi code where it belongs --- morss/__main__.py | 25 ++----------------------- morss/wsgi.py | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/morss/__main__.py b/morss/__main__.py index 594fd14..d074337 100644 --- a/morss/__main__.py +++ b/morss/__main__.py @@ -25,36 +25,15 @@ from . import cli from .morss import MorssException -import wsgiref.simple_server -import wsgiref.handlers - - -PORT = int(os.getenv('PORT', 8080)) - def main(): if 'REQUEST_URI' in os.environ: # mod_cgi (w/o file handler) - - app = wsgi.cgi_app - app = wsgi.cgi_dispatcher(app) - app = wsgi.cgi_error_handler(app) - app = wsgi.cgi_encode(app) - - wsgiref.handlers.CGIHandler().run(app) + wsgi.cgi_handle_request() elif len(sys.argv) <= 1: # start internal (basic) http server (w/ file handler) - - app = wsgi.cgi_app - app = wsgi.cgi_file_handler(app) - app = wsgi.cgi_dispatcher(app) - app = wsgi.cgi_error_handler(app) - app = wsgi.cgi_encode(app) - - print('Serving http://localhost:%s/' % PORT) - httpd = wsgiref.simple_server.make_server('', PORT, app) - httpd.serve_forever() + wsgi.cgi_start_server() else: # as a CLI app diff --git a/morss/wsgi.py b/morss/wsgi.py index 3ddb1c3..0a98c39 100644 --- a/morss/wsgi.py +++ b/morss/wsgi.py @@ -22,6 +22,8 @@ import lxml.etree import cgitb import wsgiref.util +import wsgiref.simple_server +import wsgiref.handlers import mimetypes try: @@ -37,6 +39,9 @@ from .morss import FeedFetch, FeedGather, FeedFormat from .morss import Options, log, TIMEOUT, DELAY, MorssException +PORT = int(os.getenv('PORT', 8080)) + + def parse_options(options): """ Turns ['md=True'] into {'md':True} """ out = {} @@ -267,3 +272,18 @@ application = cgi_file_handler(application) application = cgi_dispatcher(application) application = cgi_error_handler(application) application = cgi_encode(application) + + +def cgi_handle_request(): + app = cgi_app + app = cgi_dispatcher(app) + app = cgi_error_handler(app) + app = cgi_encode(app) + + wsgiref.handlers.CGIHandler().run(app) + + +def cgi_start_server(): + print('Serving http://localhost:%s/' % PORT) + httpd = wsgiref.simple_server.make_server('', PORT, application) + httpd.serve_forever()