76 lines
2.0 KiB
Python
76 lines
2.0 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""
|
||
中央服务器入口(Django + Channels + uvicorn)。
|
||
启动方式: python -m server.main
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import asyncio
|
||
import logging
|
||
import os
|
||
|
||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "server.settings")
|
||
|
||
import django # noqa: E402
|
||
django.setup() # noqa: E402
|
||
|
||
import uvicorn # noqa: E402
|
||
|
||
from server import config # noqa: E402
|
||
from server.core.task_health_monitor import task_health_monitor # noqa: E402
|
||
from server.core.worker_manager import worker_manager # noqa: E402
|
||
from tunnel.server import TunnelServer # noqa: E402
|
||
|
||
logging.basicConfig(
|
||
level=logging.INFO,
|
||
format="%(asctime)s %(name)-28s %(levelname)-5s %(message)s",
|
||
datefmt="%Y-%m-%d %H:%M:%S",
|
||
)
|
||
logger = logging.getLogger("server.main")
|
||
|
||
|
||
async def run_server():
|
||
"""启动服务器:Django ASGI + 心跳巡检 + 隧道。"""
|
||
# 启动心跳巡检
|
||
asyncio.create_task(worker_manager.check_heartbeats_loop())
|
||
# 启动超时任务巡检
|
||
asyncio.create_task(task_health_monitor.run_loop())
|
||
|
||
# 启动隧道服务
|
||
tunnel_server = TunnelServer(
|
||
control_port=config.TUNNEL_CONTROL_PORT,
|
||
stream_port=config.TUNNEL_STREAM_PORT,
|
||
proxy_base_port=config.TUNNEL_PROXY_BASE_PORT,
|
||
host=config.HOST,
|
||
)
|
||
await tunnel_server.start()
|
||
|
||
logger.info(
|
||
"服务器启动: http://%s:%s | 隧道: 控制 %s, 流 %s, 代理起始 %s",
|
||
config.HOST, config.PORT,
|
||
config.TUNNEL_CONTROL_PORT, config.TUNNEL_STREAM_PORT, config.TUNNEL_PROXY_BASE_PORT,
|
||
)
|
||
|
||
# 启动 uvicorn(使用 Django Channels ASGI 应用)
|
||
uvi_config = uvicorn.Config(
|
||
"server.asgi:application",
|
||
host=config.HOST,
|
||
port=config.PORT,
|
||
log_level="info",
|
||
)
|
||
server = uvicorn.Server(uvi_config)
|
||
|
||
try:
|
||
await server.serve()
|
||
finally:
|
||
await tunnel_server.stop()
|
||
logger.info("服务器已关闭")
|
||
|
||
|
||
def main():
|
||
asyncio.run(run_server())
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|