2017-11-04 12:17:12 +00:00
|
|
|
# ran on `python -m morss`
|
2020-08-21 20:17:55 +00:00
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2020-08-23 16:44:49 +00:00
|
|
|
from . import wsgi
|
2020-08-21 20:17:55 +00:00
|
|
|
from . import cli
|
|
|
|
|
|
|
|
from .morss import MorssException
|
|
|
|
|
|
|
|
import wsgiref.simple_server
|
|
|
|
import wsgiref.handlers
|
|
|
|
|
|
|
|
|
2020-08-23 16:50:18 +00:00
|
|
|
PORT = int(os.getenv('PORT', 8080))
|
2020-08-21 20:17:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
if 'REQUEST_URI' in os.environ:
|
2020-08-23 16:50:18 +00:00
|
|
|
# mod_cgi (w/o file handler)
|
2020-08-21 20:17:55 +00:00
|
|
|
|
2020-08-23 16:44:49 +00:00
|
|
|
app = wsgi.cgi_app
|
|
|
|
app = wsgi.cgi_dispatcher(app)
|
|
|
|
app = wsgi.cgi_error_handler(app)
|
|
|
|
app = wsgi.cgi_encode(app)
|
2020-08-21 20:17:55 +00:00
|
|
|
|
|
|
|
wsgiref.handlers.CGIHandler().run(app)
|
|
|
|
|
2020-08-23 16:50:18 +00:00
|
|
|
elif len(sys.argv) <= 1:
|
|
|
|
# start internal (basic) http server (w/ file handler)
|
2020-08-21 20:17:55 +00:00
|
|
|
|
2020-08-23 16:44:49 +00:00
|
|
|
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)
|
2020-08-21 20:17:55 +00:00
|
|
|
|
|
|
|
print('Serving http://localhost:%s/' % port)
|
2020-08-23 16:50:18 +00:00
|
|
|
httpd = wsgiref.simple_server.make_server('', PORT, app)
|
2020-08-21 20:17:55 +00:00
|
|
|
httpd.serve_forever()
|
|
|
|
|
|
|
|
else:
|
|
|
|
# as a CLI app
|
|
|
|
try:
|
|
|
|
cli.cli_app()
|
|
|
|
|
|
|
|
except (KeyboardInterrupt, SystemExit):
|
|
|
|
raise
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
print('ERROR: %s' % e.message)
|
2015-03-11 06:23:14 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|