You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.7 KiB
83 lines
2.7 KiB
""" Initialize the webapp. """
|
|
|
|
import os
|
|
import threading
|
|
import signal
|
|
import configparser
|
|
import logging
|
|
import logging.config
|
|
|
|
from flask import Flask
|
|
import flask.cli
|
|
import yaml
|
|
|
|
from asl_rulebook2.webapp.config.constants import BASE_DIR, CONFIG_DIR
|
|
|
|
shutdown_event = threading.Event()
|
|
|
|
# ---------------------------------------------------------------------
|
|
|
|
def _load_config( fname, section ):
|
|
"""Load config settings from a file."""
|
|
fname = os.path.join( CONFIG_DIR, fname )
|
|
if not os.path.isfile( fname ):
|
|
return
|
|
config_parser = configparser.ConfigParser()
|
|
config_parser.optionxform = str # preserve case for the keys :-/
|
|
config_parser.read( fname )
|
|
app.config.update( dict( config_parser.items( section ) ) )
|
|
|
|
# ---------------------------------------------------------------------
|
|
|
|
def _on_sigint( signum, stack ): #pylint: disable=unused-argument
|
|
"""Clean up after a SIGINT."""
|
|
|
|
# notify everyone that we're shutting down
|
|
shutdown_event.set()
|
|
|
|
# call any registered cleanup handlers
|
|
for handler in globvars.cleanup_handlers:
|
|
handler()
|
|
|
|
# exit the application
|
|
raise SystemExit()
|
|
|
|
# ---------------------------------------------------------------------
|
|
|
|
# disable the Flask startup banner
|
|
flask.cli.show_server_banner = lambda *args: None
|
|
|
|
# initialize Flask
|
|
app = Flask( __name__ )
|
|
|
|
# load the application configuration
|
|
_load_config( "app.cfg", "System" )
|
|
_load_config( "site.cfg", "Site Config" )
|
|
_load_config( "debug.cfg", "Debug" )
|
|
for key, val in app.config.items():
|
|
if str( val ).isdigit():
|
|
app.config[ key ] = int( val )
|
|
|
|
# initialize logging
|
|
_fname = os.path.join( CONFIG_DIR, "logging.yaml" )
|
|
if os.path.isfile( _fname ):
|
|
with open( _fname, "r", encoding="utf-8" ) as fp:
|
|
try:
|
|
logging.config.dictConfig( yaml.safe_load( fp ) )
|
|
except Exception as ex: #pylint: disable=broad-except
|
|
logging.error( "Can't load the logging config: %s", ex )
|
|
else:
|
|
# stop Flask from logging every request :-/
|
|
logging.getLogger( "werkzeug" ).setLevel( logging.WARNING )
|
|
|
|
# load the application
|
|
import asl_rulebook2.webapp.main #pylint: disable=wrong-import-position,cyclic-import
|
|
import asl_rulebook2.webapp.startup #pylint: disable=wrong-import-position,cyclic-import
|
|
import asl_rulebook2.webapp.content #pylint: disable=wrong-import-position,cyclic-import
|
|
import asl_rulebook2.webapp.search #pylint: disable=wrong-import-position,cyclic-import
|
|
import asl_rulebook2.webapp.rule_info #pylint: disable=wrong-import-position,cyclic-import
|
|
from asl_rulebook2.webapp import globvars #pylint: disable=wrong-import-position,cyclic-import
|
|
app.before_request( globvars.on_request )
|
|
|
|
# install our signal handler
|
|
signal.signal( signal.SIGINT, _on_sigint )
|
|
|