from pathlib import Path
from utils import logger
import config
import logging.config


def startDev():
    import uvicorn
    from pathlib import Path

    uvicorn.run(
        "api:app",
        host="0.0.0.0",
        port=8081,
        reload=True,
        reload_dirs=[config.RELOAD_DIR],
        log_config=None,
    )


async def main():
    from db import DATABASE_URL
    from db.initialize import DatabaseInitializer

    try:
        force = True
        reNewDB = True
        await DatabaseInitializer(DATABASE_URL).initialize(force, reNewDB)
    except Exception as e:
        logger.error(f"Инициализация базы завершилась ошибкой: {str(e)}", exc_info=True)

    startDev()


if __name__ == "__main__":
    import asyncio

    log_config_path = Path("config/log.ini")
    logging.config.fileConfig(log_config_path, disable_existing_loggers=False)
    asyncio.run(main())
