Shift __main__'s wsgi code where it belongs

master
pictuga 2020-09-30 23:24:51 +02:00
parent d9f46b23a6
commit 2fc7cd391c
2 changed files with 22 additions and 23 deletions

View File

@ -25,36 +25,15 @@ from . import cli
from .morss import MorssException from .morss import MorssException
import wsgiref.simple_server
import wsgiref.handlers
PORT = int(os.getenv('PORT', 8080))
def main(): def main():
if 'REQUEST_URI' in os.environ: if 'REQUEST_URI' in os.environ:
# mod_cgi (w/o file handler) # mod_cgi (w/o file handler)
wsgi.cgi_handle_request()
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)
elif len(sys.argv) <= 1: elif len(sys.argv) <= 1:
# start internal (basic) http server (w/ file handler) # start internal (basic) http server (w/ file handler)
wsgi.cgi_start_server()
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()
else: else:
# as a CLI app # as a CLI app

View File

@ -22,6 +22,8 @@ import lxml.etree
import cgitb import cgitb
import wsgiref.util import wsgiref.util
import wsgiref.simple_server
import wsgiref.handlers
import mimetypes import mimetypes
try: try:
@ -37,6 +39,9 @@ from .morss import FeedFetch, FeedGather, FeedFormat
from .morss import Options, log, TIMEOUT, DELAY, MorssException from .morss import Options, log, TIMEOUT, DELAY, MorssException
PORT = int(os.getenv('PORT', 8080))
def parse_options(options): def parse_options(options):
""" Turns ['md=True'] into {'md':True} """ """ Turns ['md=True'] into {'md':True} """
out = {} out = {}
@ -267,3 +272,18 @@ application = cgi_file_handler(application)
application = cgi_dispatcher(application) application = cgi_dispatcher(application)
application = cgi_error_handler(application) application = cgi_error_handler(application)
application = cgi_encode(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()