# -*- coding: utf-8 -*-

import os
import sys
import uvicorn

os.chdir(os.path.dirname(__file__))
sys.path.insert(0, os.path.abspath(r'../../'))

from hagworm.frame.fastapi.base import DEFAULT_HEADERS, create_fastapi
from hagworm.extend.base import Utils
from hagworm.extend.logging import LogFileRotator

from setting import ConfigDynamic
from service.base import DataSource
from controller import home


fastapi = create_fastapi(
    log_level=ConfigDynamic.LogLevel,
    log_file_path=ConfigDynamic.LogFilePath,
    log_file_rotation=LogFileRotator.make(ConfigDynamic.LogFileSplitSize, ConfigDynamic.LogFileSplitTime),
    log_file_retention=ConfigDynamic.LogFileBackups,
    debug=ConfigDynamic.Debug,
    on_startup=[DataSource.initialize],
    on_shutdown=[DataSource.release],
)

fastapi.include_router(home.router)


if __name__ == r'__main__':

    Utils.log.warning(r'The production environment is started using gunicorn')

    uvicorn.run(fastapi, port=8080, log_config=None, headers=DEFAULT_HEADERS)
