mirror of
https://github.com/snailyp/gemini-balance.git
synced 2026-07-03 05:41:53 +08:00
feat: 增强应用日志记录并优化错误处理
This commit is contained in:
48
app/main.py
48
app/main.py
@@ -25,8 +25,13 @@ key_manager = None
|
||||
@app.on_event("startup")
|
||||
async def startup_event():
|
||||
global key_manager
|
||||
key_manager = await get_key_manager_instance(settings.API_KEYS)
|
||||
|
||||
logger.info("Application starting up...")
|
||||
try:
|
||||
key_manager = await get_key_manager_instance(settings.API_KEYS)
|
||||
logger.info("KeyManager initialized successfully")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to initialize KeyManager: {str(e)}")
|
||||
raise
|
||||
|
||||
# 添加中间件来处理未经身份验证的请求
|
||||
@app.middleware("http")
|
||||
@@ -41,7 +46,9 @@ async def auth_middleware(request: Request, call_next):
|
||||
not request.url.path.startswith("/hf")):
|
||||
auth_token = request.cookies.get("auth_token")
|
||||
if not auth_token or not verify_auth_token(auth_token):
|
||||
logger.warning(f"Unauthorized access attempt to {request.url.path}")
|
||||
return RedirectResponse(url="/")
|
||||
logger.debug("Request authenticated successfully")
|
||||
response = await call_next(request)
|
||||
return response
|
||||
|
||||
@@ -76,31 +83,40 @@ async def authenticate(request: Request):
|
||||
form = await request.form()
|
||||
auth_token = form.get("auth_token")
|
||||
if not auth_token:
|
||||
logger.warning("Authentication attempt with empty token")
|
||||
return RedirectResponse(url="/", status_code=302)
|
||||
|
||||
if verify_auth_token(auth_token):
|
||||
logger.info("Successful authentication")
|
||||
response = RedirectResponse(url="/keys", status_code=302)
|
||||
response.set_cookie(key="auth_token", value=auth_token, httponly=True, max_age=3600)
|
||||
return response
|
||||
logger.warning("Failed authentication attempt with invalid token")
|
||||
return RedirectResponse(url="/", status_code=302)
|
||||
except Exception as e:
|
||||
logger.error(f"Authentication error: {str(e)}")
|
||||
return RedirectResponse(url="/", status_code=302)
|
||||
|
||||
|
||||
@app.get("/keys", response_class=HTMLResponse)
|
||||
async def keys_page(request: Request):
|
||||
auth_token = request.cookies.get("auth_token")
|
||||
if not auth_token or not verify_auth_token(auth_token):
|
||||
return RedirectResponse(url="/", status_code=302)
|
||||
keys_status = await key_manager.get_keys_by_status()
|
||||
total = len(keys_status["valid_keys"]) + len(keys_status["invalid_keys"])
|
||||
return templates.TemplateResponse("keys_status.html", {
|
||||
"request": request,
|
||||
"valid_keys": keys_status["valid_keys"],
|
||||
"invalid_keys": keys_status["invalid_keys"],
|
||||
"total": total
|
||||
})
|
||||
try:
|
||||
auth_token = request.cookies.get("auth_token")
|
||||
if not auth_token or not verify_auth_token(auth_token):
|
||||
logger.warning("Unauthorized access attempt to keys page")
|
||||
return RedirectResponse(url="/", status_code=302)
|
||||
|
||||
keys_status = await key_manager.get_keys_by_status()
|
||||
total = len(keys_status["valid_keys"]) + len(keys_status["invalid_keys"])
|
||||
logger.info(f"Keys status retrieved successfully. Total keys: {total}")
|
||||
return templates.TemplateResponse("keys_status.html", {
|
||||
"request": request,
|
||||
"valid_keys": keys_status["valid_keys"],
|
||||
"invalid_keys": keys_status["invalid_keys"],
|
||||
"total": total
|
||||
})
|
||||
except Exception as e:
|
||||
logger.error(f"Error retrieving keys status: {str(e)}")
|
||||
raise
|
||||
|
||||
|
||||
@app.get("/health")
|
||||
@@ -108,7 +124,7 @@ async def health_check(request: Request):
|
||||
logger.info("Health check endpoint called")
|
||||
return {"status": "healthy"}
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
logger.info("Starting application server...")
|
||||
uvicorn.run(app, host="0.0.0.0", port=8001)
|
||||
|
||||
Reference in New Issue
Block a user