新增資料庫保持活躍API端點
Browse files- app/main.py +35 -0
- unified_server.py +36 -0
app/main.py
CHANGED
|
@@ -59,6 +59,41 @@ async def api_health_check():
|
|
| 59 |
"timestamp": "2024-09-09T00:00:00Z"
|
| 60 |
}
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
# FastAPI 固定使用 port 8000,Streamlit 使用 PORT 環境變數 (7860)
|
|
|
|
| 59 |
"timestamp": "2024-09-09T00:00:00Z"
|
| 60 |
}
|
| 61 |
|
| 62 |
+
@app.get("/api/keep-alive")
|
| 63 |
+
async def keep_alive():
|
| 64 |
+
"""
|
| 65 |
+
資料庫保持活躍 endpoint
|
| 66 |
+
執行簡單的資料庫查詢以保持 Supabase 連接活躍
|
| 67 |
+
"""
|
| 68 |
+
from datetime import datetime, timezone
|
| 69 |
+
from app.models.supabase_clients import supabase_clients
|
| 70 |
+
|
| 71 |
+
try:
|
| 72 |
+
# 查詢資料庫以保持連接活躍
|
| 73 |
+
client = supabase_clients.get_license_client()
|
| 74 |
+
if client:
|
| 75 |
+
# 執行簡單查詢:統計授權數量
|
| 76 |
+
response = client.table("licenses").select("id", count="exact").limit(1).execute()
|
| 77 |
+
db_status = "active"
|
| 78 |
+
license_count = response.count if hasattr(response, 'count') else 0
|
| 79 |
+
else:
|
| 80 |
+
db_status = "disconnected"
|
| 81 |
+
license_count = 0
|
| 82 |
+
|
| 83 |
+
return {
|
| 84 |
+
"status": "success",
|
| 85 |
+
"database": db_status,
|
| 86 |
+
"license_count": license_count,
|
| 87 |
+
"timestamp": datetime.now(timezone.utc).isoformat()
|
| 88 |
+
}
|
| 89 |
+
except Exception as e:
|
| 90 |
+
return {
|
| 91 |
+
"status": "error",
|
| 92 |
+
"database": "error",
|
| 93 |
+
"error": str(e),
|
| 94 |
+
"timestamp": datetime.now(timezone.utc).isoformat()
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
|
| 98 |
if __name__ == "__main__":
|
| 99 |
# FastAPI 固定使用 port 8000,Streamlit 使用 PORT 環境變數 (7860)
|
unified_server.py
CHANGED
|
@@ -113,6 +113,42 @@ async def api_root():
|
|
| 113 |
"docs": "/api/docs"
|
| 114 |
}
|
| 115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
# 通配符路由最後處理 (避免攔截 API)
|
| 117 |
@app.get("/{full_path:path}")
|
| 118 |
async def serve_spa_routes(full_path: str):
|
|
|
|
| 113 |
"docs": "/api/docs"
|
| 114 |
}
|
| 115 |
|
| 116 |
+
# 資料庫保持活躍端點
|
| 117 |
+
@app.get("/api/keep-alive")
|
| 118 |
+
async def keep_alive():
|
| 119 |
+
"""
|
| 120 |
+
資料庫保持活躍 endpoint
|
| 121 |
+
執行簡單的資料庫查詢以保持 Supabase 連接活躍
|
| 122 |
+
"""
|
| 123 |
+
from datetime import datetime, timezone
|
| 124 |
+
from app.models.supabase_clients import supabase_clients
|
| 125 |
+
|
| 126 |
+
try:
|
| 127 |
+
# 查詢資料庫以保持連接活躍
|
| 128 |
+
client = supabase_clients.get_license_client()
|
| 129 |
+
if client:
|
| 130 |
+
# 執行簡單查詢:統計授權數量
|
| 131 |
+
response = client.table("licenses").select("id", count="exact").limit(1).execute()
|
| 132 |
+
db_status = "active"
|
| 133 |
+
license_count = response.count if hasattr(response, 'count') else 0
|
| 134 |
+
else:
|
| 135 |
+
db_status = "disconnected"
|
| 136 |
+
license_count = 0
|
| 137 |
+
|
| 138 |
+
return {
|
| 139 |
+
"status": "success",
|
| 140 |
+
"database": db_status,
|
| 141 |
+
"license_count": license_count,
|
| 142 |
+
"timestamp": datetime.now(timezone.utc).isoformat()
|
| 143 |
+
}
|
| 144 |
+
except Exception as e:
|
| 145 |
+
return {
|
| 146 |
+
"status": "error",
|
| 147 |
+
"database": "error",
|
| 148 |
+
"error": str(e),
|
| 149 |
+
"timestamp": datetime.now(timezone.utc).isoformat()
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
# 通配符路由最後處理 (避免攔截 API)
|
| 153 |
@app.get("/{full_path:path}")
|
| 154 |
async def serve_spa_routes(full_path: str):
|