2018-06-30 19:59:10 +00:00
|
|
|
'''
|
|
|
|
This file is the gevent launcher for local / development use.
|
|
|
|
|
|
|
|
Simply run it on the command line:
|
2020-09-30 22:05:44 +00:00
|
|
|
python etiquette_flask_dev.py [port]
|
2018-06-30 19:59:10 +00:00
|
|
|
'''
|
2018-04-16 03:34:03 +00:00
|
|
|
import gevent.monkey; gevent.monkey.patch_all()
|
2016-09-18 08:33:46 +00:00
|
|
|
|
2017-11-26 10:37:11 +00:00
|
|
|
import logging
|
|
|
|
handler = logging.StreamHandler()
|
|
|
|
log_format = '{levelname}:etiquette.{module}.{funcName}: {message}'
|
|
|
|
handler.setFormatter(logging.Formatter(log_format, style='{'))
|
|
|
|
logging.getLogger().addHandler(handler)
|
|
|
|
|
2018-04-16 03:34:03 +00:00
|
|
|
import argparse
|
2018-11-05 03:27:20 +00:00
|
|
|
import gevent.pywsgi
|
2021-01-08 21:38:54 +00:00
|
|
|
import os
|
2016-09-18 08:33:46 +00:00
|
|
|
import sys
|
|
|
|
|
2020-09-22 09:54:26 +00:00
|
|
|
from voussoirkit import pathclass
|
2021-01-09 23:41:52 +00:00
|
|
|
from voussoirkit import pipeable
|
2020-11-09 04:20:04 +00:00
|
|
|
from voussoirkit import vlogging
|
2018-01-18 04:53:25 +00:00
|
|
|
|
2021-01-09 23:41:52 +00:00
|
|
|
import etiquette
|
2020-09-30 22:05:44 +00:00
|
|
|
import backend
|
|
|
|
|
|
|
|
site = backend.site
|
2018-01-18 04:53:25 +00:00
|
|
|
|
2020-09-22 09:54:26 +00:00
|
|
|
HTTPS_DIR = pathclass.Path(__file__).parent.with_child('https')
|
2020-11-09 04:20:04 +00:00
|
|
|
LOG_LEVEL = vlogging.NOTSET
|
|
|
|
|
|
|
|
####################################################################################################
|
2018-01-18 04:53:25 +00:00
|
|
|
|
2020-09-30 22:17:20 +00:00
|
|
|
def etiquette_flask_launch(
|
|
|
|
*,
|
|
|
|
localhost_only,
|
|
|
|
port,
|
|
|
|
use_https,
|
|
|
|
):
|
2018-04-16 03:34:03 +00:00
|
|
|
if use_https is None:
|
|
|
|
use_https = port == 443
|
2016-09-18 08:33:46 +00:00
|
|
|
|
2018-04-16 03:34:03 +00:00
|
|
|
if use_https:
|
|
|
|
http = gevent.pywsgi.WSGIServer(
|
|
|
|
listener=('0.0.0.0', port),
|
2020-09-30 22:05:44 +00:00
|
|
|
application=site,
|
2020-09-22 09:54:26 +00:00
|
|
|
keyfile=HTTPS_DIR.with_child('etiquette.key').absolute_path,
|
|
|
|
certfile=HTTPS_DIR.with_child('etiquette.crt').absolute_path,
|
2018-04-16 03:34:03 +00:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
http = gevent.pywsgi.WSGIServer(
|
|
|
|
listener=('0.0.0.0', port),
|
2020-09-30 22:05:44 +00:00
|
|
|
application=site,
|
2018-04-16 03:34:03 +00:00
|
|
|
)
|
2016-11-27 09:06:11 +00:00
|
|
|
|
2020-09-30 22:15:49 +00:00
|
|
|
if localhost_only:
|
|
|
|
site.localhost_only = True
|
|
|
|
|
2021-01-09 23:41:52 +00:00
|
|
|
try:
|
|
|
|
backend.common.init_photodb(log_level=LOG_LEVEL)
|
|
|
|
except etiquette.exceptions.NoClosestPhotoDB as exc:
|
|
|
|
pipeable.stderr(exc.error_message)
|
|
|
|
pipeable.stderr('Try etiquette_cli init')
|
|
|
|
return 1
|
2020-09-22 09:54:26 +00:00
|
|
|
|
2021-01-08 21:38:54 +00:00
|
|
|
message = f'Starting server on port {port}, pid={os.getpid()}'
|
2018-04-16 03:34:03 +00:00
|
|
|
if use_https:
|
|
|
|
message += ' (https)'
|
|
|
|
print(message)
|
2020-09-22 09:54:26 +00:00
|
|
|
|
2018-04-16 03:34:03 +00:00
|
|
|
try:
|
|
|
|
http.serve_forever()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
2016-09-18 08:33:46 +00:00
|
|
|
|
2020-09-22 09:54:26 +00:00
|
|
|
def etiquette_flask_launch_argparse(args):
|
|
|
|
return etiquette_flask_launch(
|
2020-09-30 22:15:49 +00:00
|
|
|
localhost_only=args.localhost_only,
|
2020-09-30 22:17:20 +00:00
|
|
|
port=args.port,
|
2020-09-22 09:54:26 +00:00
|
|
|
use_https=args.use_https,
|
|
|
|
)
|
2018-04-16 03:34:03 +00:00
|
|
|
|
|
|
|
def main(argv):
|
2020-11-09 04:20:04 +00:00
|
|
|
global LOG_LEVEL
|
|
|
|
(LOG_LEVEL, argv) = vlogging.get_level_by_argv(argv)
|
|
|
|
|
2018-04-16 03:34:03 +00:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
|
2020-09-22 09:54:26 +00:00
|
|
|
parser.add_argument('port', nargs='?', type=int, default=5000)
|
2018-04-16 03:34:03 +00:00
|
|
|
parser.add_argument('--https', dest='use_https', action='store_true', default=None)
|
2020-09-30 22:17:20 +00:00
|
|
|
parser.add_argument('--localhost_only', '--localhost-only', dest='localhost_only', action='store_true')
|
2020-09-22 09:54:26 +00:00
|
|
|
parser.set_defaults(func=etiquette_flask_launch_argparse)
|
2018-04-16 03:34:03 +00:00
|
|
|
|
|
|
|
args = parser.parse_args(argv)
|
2020-02-09 01:24:57 +00:00
|
|
|
return args.func(args)
|
2018-04-16 03:34:03 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
raise SystemExit(main(sys.argv[1:]))
|