Browse Source

对话的token从url传

zhangqian 1 year ago
parent
commit
f41ca9e5dc
3 changed files with 46 additions and 5 deletions
  1. 3 4
      app/api/__init__.py
  2. 0 1
      app/config/config.py
  3. 43 0
      restart.sh

+ 3 - 4
app/api/__init__.py

@@ -48,11 +48,10 @@ def get_current_user(token: str = Depends(oauth2_scheme)):
 
 
 async def get_current_user_websocket(websocket: WebSocket):
-    auth_header = websocket.headers.get('Authorization')
-    if auth_header is None or not auth_header.startswith('Bearer '):
+    token = websocket.query_params.get('token')
+    if token is None:
         await websocket.close(code=1008)
         raise WebSocketDisconnect(code=status.WS_1008_POLICY_VIOLATION)
-    token = auth_header[len('Bearer '):]
     try:
         payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
         username: str = payload.get("sub")
@@ -67,4 +66,4 @@ async def get_current_user_websocket(websocket: WebSocket):
     except jwt.PyJWTError as e:
         print(e)
         await websocket.close(code=1008)
-        raise WebSocketDisconnect(code=status.WS_1008_POLICY_VIOLATION)
+        raise WebSocketDisconnect(code=status.WS_1008_POLICY_VIOLATION)

+ 0 - 1
app/config/config.py

@@ -1,4 +1,3 @@
-import os
 from pathlib import Path
 import yaml
 

+ 43 - 0
restart.sh

@@ -0,0 +1,43 @@
+#!/bin/bash
+
+# 定义变量
+VENV_PATH="./venv"  # 虚拟环境路径
+APP_NAME="main:app"
+HOST="0.0.0.0"
+PORT="9201"
+UVICORN_CMD="uvicorn $APP_NAME --host $HOST --port $PORT"
+
+# 激活虚拟环境
+source $VENV_PATH/bin/activate
+
+# 停止现有进程
+echo "Stopping existing processes..."
+PIDS=$(pgrep -f "$UVICORN_CMD")
+
+if [ -z "$PIDS" ]; then
+    echo "No running processes found."
+else
+    for PID in $PIDS; do
+        echo "Terminating process $PID..."
+        kill -15 $PID
+        sleep 1  # 等待1秒,确保进程有时间优雅关闭
+        if kill -0 $PID > /dev/null 2>&1; then
+            echo "Process $PID is still running, sending SIGKILL..."
+            kill -9 $PID
+        fi
+    done
+fi
+
+# 启动新进程
+echo "Starting new process..."
+$UVICORN_CMD > server.log 2>&1 &
+NEW_PID=$!
+echo "New process started with PID $NEW_PID."
+
+# 可选:检查新进程是否成功启动
+sleep 2  # 等待2秒,确保新进程有足够的时间启动
+if kill -0 $NEW_PID > /dev/null 2>&1; then
+    echo "New process with PID $NEW_PID is running."
+else
+    echo "Failed to start the new process."
+fi