isididiidid commited on
Commit
e56afd7
·
verified ·
1 Parent(s): c2bf8cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -14
app.py CHANGED
@@ -1098,19 +1098,22 @@ async def index():
1098
  if __name__ == "__main__":
1099
  asyncio.run(initialize_tokens())
1100
  app.run(host="0.0.0.0", port=CONFIG["SERVER"]["PORT"])
1101
- if __name__ == "__main__":
1102
- import argparse
1103
- parser = argparse.ArgumentParser()
1104
- parser.add_argument("--port", type=int, default=CONFIG["SERVER"]["PORT"])
1105
- args = parser.parse_args()
1106
 
1107
- # 使用传入的端口覆盖配置中的端口
1108
- port = args.port
1109
 
1110
- # 这里使用修改后的端口启动服务
1111
- app.run(host="0.0.0.0", port=port)
1112
-
1113
-
1114
-
1115
-
1116
-
 
 
 
 
1098
  if __name__ == "__main__":
1099
  asyncio.run(initialize_tokens())
1100
  app.run(host="0.0.0.0", port=CONFIG["SERVER"]["PORT"])
1101
+ # 在现有app.py文件的最底部添加或修改这段代码
1102
+ if __name__ == "__main__":
1103
+ # 初始化令牌
1104
+ import asyncio
1105
+ asyncio.run(initialize_tokens())
1106
 
1107
+ # 获取端口,优先使用环境变量
1108
+ port = int(os.getenv("PORT", CONFIG["SERVER"]["PORT"]))
1109
 
1110
+ # 启动服务
1111
+ from hypercorn.asyncio import serve
1112
+ from hypercorn.config import Config as HyperConfig
1113
+
1114
+ config = HyperConfig()
1115
+ config.bind = [f"0.0.0.0:{port}"]
1116
+ config.use_reloader = True
1117
+
1118
+ logger.info(f"服务器正在启动,端口:{port}", "Server")
1119
+ asyncio.run(serve(app, config))