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

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

# init logging to file
import logging
LOG_FORMAT = ('%(levelname) -5s %(asctime)s %(name)s:%(funcName) -35s %(lineno) -5d:  %(message)s')
logging.basicConfig(level=logging.INFO, format=LOG_FORMAT)
LOGGER = logging.getLogger(__name__)

#################################
import connexion
from security import swagger_util

# load swagger config 

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

app.add_api(swagger_util.get_bundled_specs(Path("configs/swagger.yml")),
            resolver = connexion.RestyResolver("cms_rest_api"))

@app.route('/', methods=['GET'])
def api_root():
    return 'Endpoint of SMART RESTful API Gateway!'

# 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

# start app
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True, use_reloader=False, ssl_context=context) # disable reloader so only subscribed once to rabbitmq