etiquette/frontends/etiquette_flask/etiquette_flask_dev.py

128 lines
3.4 KiB
Python
Raw Normal View History

'''
This file is the gevent launcher for local / development use.
'''
2023-02-04 21:49:42 +00:00
from voussoirkit import vlogging
vlogging.earlybird_config()
import gevent.monkey; gevent.monkey.patch_all()
import werkzeug.middleware.proxy_fix
2016-09-18 08:33:46 +00:00
import argparse
import gevent.pywsgi
2021-01-08 21:38:54 +00:00
import os
2016-09-18 08:33:46 +00:00
import sys
2021-09-09 02:40:05 +00:00
from voussoirkit import betterhelp
2020-09-22 09:54:26 +00:00
from voussoirkit import pathclass
2023-02-04 21:49:42 +00:00
from voussoirkit import operatornotify
2020-11-09 04:20:04 +00:00
from voussoirkit import vlogging
2021-09-09 02:36:29 +00:00
log = vlogging.getLogger(__name__, 'etiquette_flask_dev')
import etiquette
import backend
site = backend.site
site.wsgi_app = werkzeug.middleware.proxy_fix.ProxyFix(site.wsgi_app)
site.debug = True
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
####################################################################################################
def etiquette_flask_launch(
*,
localhost_only,
port,
use_https,
):
if use_https is None:
use_https = port == 443
2016-09-18 08:33:46 +00:00
if use_https:
http = gevent.pywsgi.WSGIServer(
listener=('0.0.0.0', port),
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,
)
else:
http = gevent.pywsgi.WSGIServer(
listener=('0.0.0.0', port),
application=site,
)
2016-11-27 09:06:11 +00:00
2020-09-30 22:15:49 +00:00
if localhost_only:
2022-03-15 20:48:07 +00:00
log.info('Setting localhost_only=True')
2020-09-30 22:15:49 +00:00
site.localhost_only = True
try:
backend.common.init_photodb()
except etiquette.exceptions.NoClosestPhotoDB as exc:
2021-09-09 02:36:29 +00:00
log.error(exc.error_message)
log.error('Try `etiquette_cli.py init` to create the database.')
return 1
2020-09-22 09:54:26 +00:00
message = f'Starting server on port {port}, pid={os.getpid()}.'
if use_https:
message += ' (https)'
2021-09-09 02:36:29 +00:00
log.info(message)
2020-09-22 09:54:26 +00:00
try:
http.serve_forever()
except KeyboardInterrupt:
2021-09-09 02:36:29 +00:00
log.info('Goodbye')
2022-11-07 06:49:06 +00:00
backend.common.P.close()
return 0
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,
port=args.port,
2020-09-22 09:54:26 +00:00
use_https=args.use_https,
)
2023-02-04 21:49:42 +00:00
@operatornotify.main_decorator(subject='etiquette_flask_dev', notify_every_line=True)
def main(argv):
2022-02-13 03:56:00 +00:00
parser = argparse.ArgumentParser(
description='''
This file is the gevent launcher for local / development use.
''',
)
parser.add_argument(
'port',
nargs='?',
type=int,
default=5000,
help='''
Port number on which to run the server.
''',
)
parser.add_argument(
'--https',
dest='use_https',
action='store_true',
help='''
If this flag is not passed, HTTPS will automatically be enabled if the port
is 443. You can pass this flag to enable HTTPS on other ports.
We expect to find etiquette.key and etiquette.crt in
frontends/etiquette_flask/https.
''',
)
parser.add_argument(
'--localhost_only',
'--localhost-only',
action='store_true',
help='''
If this flag is passed, only localhost will be able to access the server.
Other users on the LAN will be blocked.
''',
)
2020-09-22 09:54:26 +00:00
parser.set_defaults(func=etiquette_flask_launch_argparse)
2022-02-13 03:56:00 +00:00
return betterhelp.go(parser, argv)
if __name__ == '__main__':
raise SystemExit(main(sys.argv[1:]))