# add modules folder to interpreter path
import sys
import os
modules_path = '../../../modules/'
if os.path.exists(modules_path):
    sys.path.insert(1, modules_path)

### init logging ###
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
from pathlib import Path

# 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 role-stage-discovery-microservice!'

# SSL configuration
try:
    # should be ../../../modules/certificate local
    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, ssl_context=context) 
