import asyncio
import logging
import sys

from gateway_runtime.RuntimeConfig import loadRuntimeConfig
from gateway_runtime.RuntimeComposer import RuntimeComposer
from gateway_core.utils.setupLogging import setupLogging
from gateway_runtime.RuntimeErrors import RuntimeStartupError

logger = logging.getLogger(__name__)


def start() -> None:
    setupLogging(appName="gateway-runtime-")

    try:
        config = loadRuntimeConfig()
        composer = RuntimeComposer(config=config)
        asyncio.run(composer.run())
    except RuntimeStartupError as e:
        logger.error("gateway-runtime startup failed: %s", e)
        sys.exit(1)
    except Exception as e:
        logger.exception("gateway-runtime crashed:", e)
        sys.exit(1)


if __name__ == "__main__":
    start()
