2021-03-24 16:09:21 +00:00
|
|
|
import enum
|
|
|
|
|
2020-09-07 14:07:35 +00:00
|
|
|
import pkg_resources
|
2020-10-15 12:44:58 +00:00
|
|
|
from flask import Flask, current_app
|
2020-09-07 14:07:35 +00:00
|
|
|
from flask_cors import CORS
|
2020-11-15 14:49:18 +00:00
|
|
|
from datetime import datetime, date
|
2020-09-07 16:11:38 +00:00
|
|
|
from flask.json import JSONEncoder, jsonify
|
2020-11-15 17:53:46 +00:00
|
|
|
from sqlalchemy.exc import OperationalError
|
2020-09-07 14:07:35 +00:00
|
|
|
from werkzeug.exceptions import HTTPException
|
|
|
|
|
|
|
|
from . import logger
|
2020-10-30 02:30:46 +00:00
|
|
|
from .plugins import AuthPlugin
|
|
|
|
from flaschengeist.config import config, configure_app
|
|
|
|
from flaschengeist.controller import roleController
|
2021-12-17 13:27:27 +00:00
|
|
|
from flaschengeist.utils.hook import Hook
|
2020-09-07 14:07:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CustomJSONEncoder(JSONEncoder):
|
|
|
|
def default(self, o):
|
|
|
|
try:
|
2021-03-24 17:37:53 +00:00
|
|
|
# Check if custom model
|
2020-09-07 14:07:35 +00:00
|
|
|
return o.serialize()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2020-11-15 14:49:18 +00:00
|
|
|
if isinstance(o, datetime) or isinstance(o, date):
|
2020-09-07 14:07:35 +00:00
|
|
|
return o.isoformat()
|
2021-03-24 16:09:21 +00:00
|
|
|
if isinstance(o, enum.Enum):
|
|
|
|
return o.value
|
2020-09-07 14:07:35 +00:00
|
|
|
try:
|
2021-03-24 17:37:53 +00:00
|
|
|
# Check if iterable
|
2020-09-07 14:07:35 +00:00
|
|
|
iterable = iter(o)
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
return list(iterable)
|
|
|
|
return JSONEncoder.default(self, o)
|
|
|
|
|
|
|
|
|
2021-12-17 13:27:27 +00:00
|
|
|
@Hook("plugins.loaded")
|
2020-09-07 14:07:35 +00:00
|
|
|
def __load_plugins(app):
|
2021-12-06 22:44:07 +00:00
|
|
|
logger.debug("Search for plugins")
|
|
|
|
|
2020-09-07 14:07:35 +00:00
|
|
|
app.config["FG_PLUGINS"] = {}
|
2021-11-28 21:24:49 +00:00
|
|
|
for entry_point in pkg_resources.iter_entry_points("flaschengeist.plugins"):
|
2021-12-06 22:44:07 +00:00
|
|
|
logger.debug(f"Found plugin: >{entry_point.name}<")
|
|
|
|
|
|
|
|
if entry_point.name == config["FLASCHENGEIST"]["auth"] or (
|
|
|
|
entry_point.name in config and config[entry_point.name].get("enabled", False)
|
|
|
|
):
|
|
|
|
logger.debug(f"Load plugin {entry_point.name}")
|
2021-03-24 16:09:21 +00:00
|
|
|
try:
|
|
|
|
plugin = entry_point.load()
|
2021-03-29 05:30:56 +00:00
|
|
|
if not hasattr(plugin, "name"):
|
|
|
|
setattr(plugin, "name", entry_point.name)
|
2021-12-06 22:44:07 +00:00
|
|
|
plugin = plugin(config.get(entry_point.name, {}))
|
2021-03-29 05:30:56 +00:00
|
|
|
if hasattr(plugin, "blueprint") and plugin.blueprint is not None:
|
2021-03-24 16:09:21 +00:00
|
|
|
app.register_blueprint(plugin.blueprint)
|
|
|
|
except:
|
|
|
|
logger.error(
|
2021-11-18 11:54:37 +00:00
|
|
|
f"Plugin {entry_point.name} was enabled, but could not be loaded due to an error.",
|
|
|
|
exc_info=True,
|
2021-03-24 16:09:21 +00:00
|
|
|
)
|
2021-08-30 12:39:54 +00:00
|
|
|
continue
|
2021-12-06 22:44:07 +00:00
|
|
|
if isinstance(plugin, AuthPlugin):
|
|
|
|
if entry_point.name != config["FLASCHENGEIST"]["auth"]:
|
|
|
|
logger.debug(f"Unload not configured AuthPlugin {entry_point.name}")
|
|
|
|
del plugin
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
logger.info(f"Using authentication plugin: {entry_point.name}")
|
|
|
|
app.config["FG_AUTH_BACKEND"] = plugin
|
2020-10-15 19:58:56 +00:00
|
|
|
else:
|
2021-12-06 22:44:07 +00:00
|
|
|
logger.info(f"Using plugin: {entry_point.name}")
|
2021-12-18 01:15:53 +00:00
|
|
|
app.config["FG_PLUGINS"][entry_point.name] = plugin
|
2021-12-17 11:21:53 +00:00
|
|
|
else:
|
|
|
|
logger.debug(f"Skip disabled plugin {entry_point.name}")
|
2020-10-15 19:58:56 +00:00
|
|
|
if "FG_AUTH_BACKEND" not in app.config:
|
|
|
|
logger.error("No authentication plugin configured or authentication plugin not found")
|
2021-08-30 12:39:54 +00:00
|
|
|
raise RuntimeError("No authentication plugin configured or authentication plugin not found")
|
2020-10-15 12:44:58 +00:00
|
|
|
|
|
|
|
|
2021-12-17 13:27:27 +00:00
|
|
|
@Hook("plugins.installed")
|
2020-10-15 12:44:58 +00:00
|
|
|
def install_all():
|
2020-10-30 02:30:46 +00:00
|
|
|
from flaschengeist.database import db
|
2020-10-15 20:10:50 +00:00
|
|
|
|
2020-10-15 12:44:58 +00:00
|
|
|
db.create_all()
|
|
|
|
db.session.commit()
|
|
|
|
for name, plugin in current_app.config["FG_PLUGINS"].items():
|
2020-10-18 23:41:54 +00:00
|
|
|
if not plugin:
|
2021-03-24 17:37:53 +00:00
|
|
|
logger.debug(f"Skip disabled plugin: {name}")
|
2020-10-18 23:41:54 +00:00
|
|
|
continue
|
2021-03-24 17:37:53 +00:00
|
|
|
logger.info(f"Install plugin {name}")
|
2020-10-15 12:44:58 +00:00
|
|
|
plugin.install()
|
|
|
|
if plugin.permissions:
|
2020-10-20 17:34:14 +00:00
|
|
|
roleController.create_permissions(plugin.permissions)
|
2020-09-07 14:07:35 +00:00
|
|
|
|
|
|
|
|
2021-12-18 00:39:04 +00:00
|
|
|
def create_app(test_config=None, cli=False):
|
2020-09-07 14:07:35 +00:00
|
|
|
app = Flask(__name__)
|
|
|
|
app.json_encoder = CustomJSONEncoder
|
|
|
|
CORS(app)
|
|
|
|
|
|
|
|
with app.app_context():
|
2020-10-30 02:30:46 +00:00
|
|
|
from flaschengeist.database import db
|
2020-10-15 20:10:50 +00:00
|
|
|
|
2021-12-18 00:39:04 +00:00
|
|
|
configure_app(app, test_config, cli)
|
2020-09-07 14:07:35 +00:00
|
|
|
db.init_app(app)
|
|
|
|
__load_plugins(app)
|
|
|
|
|
|
|
|
@app.route("/", methods=["GET"])
|
|
|
|
def __get_state():
|
|
|
|
from . import __version__ as version
|
2020-10-15 20:10:50 +00:00
|
|
|
|
2020-09-07 14:07:35 +00:00
|
|
|
return jsonify({"plugins": app.config["FG_PLUGINS"], "version": version})
|
|
|
|
|
|
|
|
@app.errorhandler(Exception)
|
|
|
|
def handle_exception(e):
|
|
|
|
if isinstance(e, HTTPException):
|
|
|
|
logger.debug(e.description, exc_info=True)
|
|
|
|
return jsonify({"error": e.description}), e.code
|
2020-11-15 17:53:46 +00:00
|
|
|
if isinstance(e, OperationalError):
|
|
|
|
logger.error(e, exc_info=True)
|
|
|
|
return {"error": "Database unavailable"}, 504
|
2020-09-07 14:07:35 +00:00
|
|
|
logger.error(str(e), exc_info=True)
|
|
|
|
return jsonify({"error": "Internal server error occurred"}), 500
|
|
|
|
|
|
|
|
return app
|