# 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

# load swagger config 

def get_bundled_specs(main_file: Path) -> Dict[str, Any]:
    '''
    parses the given swagger.yml file and resolves dependencies
    from that file to enable the possibility to split the 
    configuration into several files
    '''
    parser = prance.ResolvingParser(str(main_file.absolute()),
                                    lazy = True, backend = 'openapi-spec-validator')
    parser.parse()
    return parser.specification

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

app.add_api(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
    
context = ('../../modules/certificate/articonf1.crt', '../../modules/certificate/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) # disable reloader so only subscribed once to rabbitmq
    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