# add modules folder to interpreter path
import sys
import os
from pathlib import Path

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
from pathlib import Path
import env_info

from flask import request
from flask import redirect
from flask_cors import CORS

# load swagger config 
app = connexion.App(__name__, specification_dir='configs/')
CORS(app.app)

@app.route('/', methods=['GET'])
def api_root():
    return redirect('/api/ui')

if not env_info.is_running_locally():
    swagger_path = "configs/swagger.yml"

    # SSL configuration
    certificate_path = env_info.get_resources_path()
    context = (os.path.normpath(f'{certificate_path}/articonf1.crt'), os.path.normpath(f'{certificate_path}/articonf1.key')) # certificate and key files
                    
else:
    print("Running locally...")
    swagger_path = "configs/swagger_local.yml"
    context = None
  
    
app.add_api(swagger_util.get_bundled_specs(Path(swagger_path)),
                resolver = connexion.RestyResolver("cms_rest_api"))


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