# 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 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 messaging.ReconnectingMessageManager import ReconnectingMessageManager
from messaging.MessageHandler import MessageHandler

# init message handler
message_handler = MessageHandler()
def message_received_callback(channel, method, properties, body):
    message_handler.handle_generic(body)

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

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

# start app
if __name__ == '__main__':
    message_manager = ReconnectingMessageManager.getInstance()
    message_manager.start_consuming('datahub', 'direct', 'semantic-linking', True, message_received_callback)

    app.run(host='0.0.0.0', port=5000, debug=True, use_reloader=False)
