import argparse
import asyncio
import signal
import uvicorn
import sys
from api import app
import uvicorn
import configparser, json
import uvicorn.loops.auto
import uvicorn.protocols.http.auto
import uvicorn.protocols.websockets.auto
import uvicorn.lifespan.on
from multiprocessing import Process, freeze_support

SHUTDOWN_TIMEOUT = 10
# ===============默认数据配置=====================
config = configparser.ConfigParser()
config_name = "config.ini"
config.read(config_name)
# argparse
# parser = argparse.ArgumentParser(description="Process some integers.")
# parser.add_argument("--port", type=int, default=7074, help="The port number.")
# args = parser.parse_args()


def signal_handler(sig, frame):
    print("[python] [flask] Received signal to terminate the server:", sig)
    sys.exit(0)


# 注册信号处理函数
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
if __name__ == "__main__":
    freeze_support()
    # 以api方式启动服务会出现警告，请忽略
    # app.run(port=args.port,use_reloader=True)
    host = config.get("app", "host")
    port = config.get("app", "port")
    debug = config.get("app", "debug")
    isDebug = True if debug =='true' else False
    uvicorn.run(app="api:app", host=host, port=int(port), reload=isDebug, loop="auto")
    print("python server is running at port:", port)
