# add modules folder to interpreter path
import sys
import os
from pathlib import Path
from typing import Dict, Any

modules_path = '../../../modules/'
if os.path.exists(modules_path):
    sys.path.insert(1, modules_path)


# load swagger config
import connexion
from security import swagger_util

app = connexion.App(__name__, specification_dir='configs/')

from db.entities.layer_adapter import LayerAdapter

@app.route('/', methods=['GET'])
def api_root():
    return 'Endpoint of business-logic-microservice!'

# SSL configuration
try:
    certificate_path = os.environ['ARTICONF_CERTIFICATE_PATH']
except KeyError:
    certificate_path = '/srv/articonf/'
context = (os.path.normpath(f'{certificate_path}/articonf1.crt'), os.path.normpath(f'{certificate_path}/articonf1.key')) # certificate and key files

# Local Mode
try:
    print("Running with local settings...")
    local = os.environ['ARTICONF_LOCAL']
    app.add_api(swagger_util.get_bundled_specs(Path("configs/swagger_local.yml")),
                resolver = connexion.RestyResolver("cms_rest_api"))
except KeyError:
    app.add_api(swagger_util.get_bundled_specs(Path("configs/swagger.yml")),
                resolver = connexion.RestyResolver("cms_rest_api"))

# start app
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=False, ssl_context=context)
