import logging.config
import os

import zmq
from helper import config_logger
from zeromqmessagehandler import ZeroMqMessageBus
from zeromqwsgiwrapper import FlaskApp

if "LOG_NAME" in os.environ:
    log_name = os.environ["LOG_NAME"]
else:
    log_name = "WsgiWrapper"


if "LOG_PATH" in os.environ:
    log_path = os.environ["LOG_PATH"]
else:
    log_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
config_logger(log_path, log_name, add_error_handler=True)
logger = logging.getLogger(log_name)


if "EVENT_SUBJECT" in os.environ:
    event_subject = os.environ["EVENT_SUBJECT"]
else:
    raise Exception("Specify EVENT_SUBJECT environment variable with the subject of the Event that will be pubslished")

num_threads = 1
if "ZMQ_NUM_THREADS" in os.environ:
    num_threads = int(os.environ["ZMQ_NUM_THREADS"])
context = zmq.Context(num_threads)


request_timeout = 30000
if "ROUTER_REQUEST_TIMEOUT" in os.environ:
    request_timeout = int(os.environ["ROUTER_REQUEST_TIMEOUT"])

message_bus = ZeroMqMessageBus("WsgiWrapper", zmq.Context(num_threads), logger)
flask_app = FlaskApp(message_bus, event_subject, request_timeout, logger)
app = flask_app.run("WsgiWrapper")
