158 lines
4.6 KiB
Python
158 lines
4.6 KiB
Python
# -*- encoding: utf-8 -*-
|
|
|
|
# @author : vincent.benoit@benserv.fr
|
|
# @brief : Configurateur Backend Flask API
|
|
|
|
#########################################################
|
|
# Importation de modules externes #
|
|
|
|
import sys, re, os
|
|
import logging as log
|
|
from logging.config import dictConfig
|
|
|
|
from flask import Flask
|
|
from flask.logging import default_handler
|
|
from flask_cors import CORS, cross_origin
|
|
from flask_jwt_extended import JWTManager
|
|
|
|
import jwt
|
|
|
|
from ConfBack.config import DefaultConfig
|
|
from ConfBack.auth import auth
|
|
from ConfBack.account import account
|
|
from ConfBack.params import params
|
|
from ConfBack.schedule import schedule
|
|
from ConfBack.log import log as logs
|
|
from ConfBack.manager import sock
|
|
from ConfBack.infos import info
|
|
|
|
#########################################################
|
|
# Corps principal du programme #
|
|
|
|
def create_app(config=None, app_name=None):
|
|
''' Create and configure the app
|
|
|
|
:param config:
|
|
configuration object
|
|
|
|
:param app_name:
|
|
application name
|
|
'''
|
|
ret = True
|
|
if app_name is None:
|
|
app_name = DefaultConfig.PROJECT
|
|
|
|
# create the app
|
|
# tells the app that configuration files are relative to the instance folder.
|
|
app = Flask(app_name,
|
|
instance_path=os.path.join(os.getcwd(), app_name),
|
|
instance_relative_config=True)
|
|
|
|
if not configure_app(app, config):
|
|
return None
|
|
|
|
configure_log(app)
|
|
configure_blueprints(app)
|
|
ret = configure_connexion(app)
|
|
|
|
return ret, app
|
|
|
|
def configure_app(app=None, config=None):
|
|
''' configure the application with configuration file and/or object
|
|
|
|
:param app:
|
|
Application object
|
|
|
|
:param config:
|
|
Configuration object
|
|
|
|
:return bool:
|
|
True if OK, otherwise False
|
|
'''
|
|
# load default config
|
|
app.config.from_object(DefaultConfig)
|
|
|
|
try:
|
|
if config:
|
|
# load specific config
|
|
app.config.from_object(config)
|
|
except ModuleNotFoundError as e:
|
|
print("Module de configuration non trouvé: {}".format(e))
|
|
return False
|
|
|
|
# setup the Flask-JWT-Extended extension
|
|
jwt = JWTManager(app)
|
|
# setup the Flask-CORS extension for handling Cross Origin Resource Sharing
|
|
CORS(app, resources={r"/api/*": {
|
|
"origins": ["http://localhost:4200",
|
|
"http://localhost:6000",
|
|
"http://127.0.0.1:4200",
|
|
"http://127.0.0.1:6000"],
|
|
# "origins": "*",
|
|
"supports_credentials": True
|
|
}})
|
|
return True
|
|
|
|
def configure_log(app=None):
|
|
''' Configure log handler
|
|
|
|
:param app:
|
|
Application object
|
|
|
|
:return None:
|
|
'''
|
|
if not os.path.exists(app.config['LOG_FOLDER']):
|
|
try:
|
|
os.makedirs(app.config['LOG_FOLDER'])
|
|
except OSError:
|
|
pass
|
|
|
|
# On vire tous les handlers
|
|
for h in app.logger.handlers:
|
|
app.logger.removeHandler(h)
|
|
|
|
# Set info level on logger, which might be overwritten by handers.
|
|
# Suppress DEBUG messages.
|
|
app.logger.setLevel(log.DEBUG)
|
|
|
|
formatter = log.Formatter('%(asctime)s - %(name)s [%(module)s.%(funcName)s:%(lineno)d] - %(levelname)s - %(message)s')
|
|
info_log = os.path.join(app.config['LOG_FOLDER'], DefaultConfig.PROJECT + '.log')
|
|
info_file_handler = log.handlers.RotatingFileHandler(info_log, maxBytes=100000, backupCount=10)
|
|
info_file_handler.setLevel(log.DEBUG)
|
|
info_file_handler.setFormatter(formatter)
|
|
app.logger.addHandler(info_file_handler)
|
|
|
|
fl = log.StreamHandler()
|
|
fl.setLevel(log.DEBUG)
|
|
fl.setFormatter(formatter)
|
|
app.logger.addHandler(fl)
|
|
|
|
#logging.getLogger('apscheduler').setLevel(logging.DEBUG)
|
|
|
|
def configure_blueprints(app=None):
|
|
''' configure blueprints
|
|
|
|
:param app:
|
|
Application object
|
|
|
|
:return None:
|
|
'''
|
|
for bp in [auth, logs, account, params, schedule, info]:
|
|
app.register_blueprint(bp)
|
|
|
|
def configure_connexion(app=None):
|
|
''' Confgure AF_UNIX connexion with KineIntercom
|
|
process
|
|
'''
|
|
ret = False
|
|
try:
|
|
sock.connect(app.config['UNIX_ADDR'])
|
|
ret = True
|
|
except TimeoutError as e:
|
|
app.logger.error("Erreur de connexion avec le processus KineIntercom")
|
|
ret = False
|
|
except ConnectionRefusedError as e:
|
|
app.logger.error("Connexion refusée avec le processus KineIntercom")
|
|
ret = False
|
|
return ret
|