simplified app configuration process

This commit is contained in:
Simon Pirkelmann 2022-02-04 22:13:15 +01:00
parent fa7c878cab
commit a77dcd1878
2 changed files with 19 additions and 48 deletions

View File

@ -5,7 +5,6 @@ from flask_security import Security, SQLAlchemyUserDatastore, hash_password
from email_validator import validate_email from email_validator import validate_email
from pathlib import Path from pathlib import Path
#from .webapp import door_app
from .door_handle import DoorHandle from .door_handle import DoorHandle
security = Security() security = Security()
@ -63,6 +62,7 @@ def create_super_admins(app, db, user_datastore, logger):
roles=roles) roles=roles)
db.session.commit() db.session.commit()
def setup_logging(app): def setup_logging(app):
# set up logging for the web app # set up logging for the web app
logger = logging.getLogger('webapp') logger = logging.getLogger('webapp')
@ -86,6 +86,7 @@ def setup_logging(app):
return logger return logger
def create_app(): def create_app():
app = Flask(__name__) app = Flask(__name__)
app.config.from_object('imaginaerraum_door_admin.default_app_config.DefaultConfig') app.config.from_object('imaginaerraum_door_admin.default_app_config.DefaultConfig')
@ -93,48 +94,20 @@ def create_app():
logger = setup_logging(app) logger = setup_logging(app)
# do some checks for file existence etc. token_file = Path(app.config.get('TOKEN_FILE'))
try: if not token_file.exists():
with open(app.config['KEY_FILE']) as f:
data = f.readlines()
if 'SECRET_KEY' in data[0]:
secret_key = data[0].split()[-1]
else:
raise Exception("Could not read SECURITY_PASSWORD_SALT")
if 'SECURITY_PASSWORD_SALT' in data[1]:
security_password_salt = data[1].split()[-1]
else:
raise Exception("Could not read SECURITY_PASSWORD_SALT")
except Exception as e:
logger.warning( logger.warning(
f"Flask keys could not be read from file at {Path(app.config['KEY_FILE']).absolute()}. Exception: {e}. Using default values instead.") f"Token file not found at {token_file.absolute()}. "
secret_key = 'Q7PJu2fg2jabYwP-Psop6c6f2G4' "An empty token file will be created."
security_password_salt = '10036796768252925167749545152988277953' )
token_file.touch()
if Path(app.config['TEMPLATE_FOLDER']).is_absolute(): # create door objects which provides access to the token file and current
if not Path(app.config['TEMPLATE_FOLDER']).exists(): # door state via MQTT
logger.error( app.door = DoorHandle(
f"Flask template folder not found at {Path(app.config['TEMPLATE_FOLDER']).absolute()}") token_file=token_file, mqtt_host=app.config['MQTT_HOST'],
else: nfc_socket=app.config['NFC_SOCKET'], logger=logger
if not (Path(__file__).parent / app.config['TEMPLATE_FOLDER']).exists(): )
logger.error(
f"Flask template folder not found at {(Path(__file__).parent / app.config['TEMPLATE_FOLDER']).absolute()}")
if Path(app.config['STATIC_FOLDER']).is_absolute():
if not Path(app.config['STATIC_FOLDER']).exists():
logger.error(
f"Flask static folder not found at {Path(app.config['STATIC_FOLDER']).absolute()}")
else:
if not (Path(__file__).parent / app.config['STATIC_FOLDER']).exists():
logger.error(
f"Flask static folder not found at {(Path(__file__).parent / app.config['STATIC_FOLDER']).absolute()}")
if not Path(app.config['TOKEN_FILE']).exists():
logger.warning(
f"Token file not found at {Path(app.config['TOKEN_FILE']).absolute()}")
# create door objects which provides access to the token file and current door state via MQTT
app.door = DoorHandle(token_file=app.config['TOKEN_FILE'], mqtt_host=app.config['MQTT_HOST'],
nfc_socket=app.config['NFC_SOCKET'],
logger=logger)
# Mail Config # Mail Config
#mail = Mail(app) #mail = Mail(app)
@ -153,4 +126,4 @@ def create_app():
create_super_admins(app, db, user_datastore, logger) create_super_admins(app, db, user_datastore, logger)
return app return app

View File

@ -11,9 +11,6 @@ class DefaultConfig(object):
SECRET_KEY = 'supersecret' SECRET_KEY = 'supersecret'
TEMPLATE_FOLDER = 'templates'
STATIC_FOLDER = 'static'
SECURITY_REGISTERABLE = False SECURITY_REGISTERABLE = False
SECURITY_CHANGEABLE = True SECURITY_CHANGEABLE = True
SECURITY_RECOVERABLE = True SECURITY_RECOVERABLE = True
@ -47,7 +44,8 @@ class DefaultConfig(object):
} }
KEY_FILE = '/root/flask_keys' KEY_FILE = '/root/flask_keys'
TOKEN_FILE = "/etc/door_tokens" TOKEN_FILE = "door_tokens"
ADMIN_FILE = 'admins'
LDAP_URL = "ldaps://ldap.imaginaerraum.de" LDAP_URL = "ldaps://ldap.imaginaerraum.de"
LDAP_USER_GROUP = 'Users' LDAP_USER_GROUP = 'Users'
@ -55,6 +53,6 @@ class DefaultConfig(object):
LDAP_DOMAIN_EXT = 'de' LDAP_DOMAIN_EXT = 'de'
NFC_SOCKET = "/tmp/nfc.sock" NFC_SOCKET = "/tmp/nfc.sock"
LOG_FILE = "/var/log/webinterface.log" LOG_FILE = "webinterface.log"
NFC_LOG = "/var/log/nfc.log" NFC_LOG = "nfc.log"
MQTT_HOST = '10.10.21.2' MQTT_HOST = '10.10.21.2'