refactor(api): Fix issues reported by pre-commit

- Modified code layout and formatting in multiple files, improving code readability.
  - Updated import statements, removing unused libraries.
  - Simplified the writing of some functions and exception handling.
This commit is contained in:
Milin
2025-03-06 14:23:52 +08:00
parent 59e3b2eec1
commit c015296081
5 changed files with 25 additions and 36 deletions

View File

@@ -29,14 +29,12 @@ class AuthHandler:
if datetime.utcnow() > expire_time:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Token expired"
status_code=status.HTTP_401_UNAUTHORIZED, detail="Token expired"
)
return payload["sub"]
except jwt.PyJWTError:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid token"
status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token"
)

View File

@@ -2,13 +2,7 @@
LightRAG FastAPI Server
"""
from fastapi import (
FastAPI,
Depends,
HTTPException,
Request,
status
)
from fastapi import FastAPI, Depends, HTTPException, status
import asyncio
import os
import logging
@@ -27,7 +21,6 @@ from lightrag.api.utils_api import (
parse_args,
get_default_host,
display_splash_screen,
get_auth_dependency,
)
from lightrag import LightRAG
from lightrag.types import GPTKeywordExtractionFormat
@@ -386,18 +379,17 @@ def create_app(args):
if not (username and password):
raise HTTPException(
status_code=status.HTTP_501_NOT_IMPLEMENTED,
detail="Authentication not configured"
detail="Authentication not configured",
)
if form_data.username != username or form_data.password != password:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect credentials"
status_code=status.HTTP_401_UNAUTHORIZED, detail="Incorrect credentials"
)
return {
"access_token": auth_handler.create_token(username),
"token_type": "bearer"
"token_type": "bearer",
}
@app.get("/health", dependencies=[Depends(optional_api_key)])

View File

@@ -1,20 +1,20 @@
aiofiles
ascii_colors
asyncpg
distro
fastapi
httpcore
httpx
jiter
numpy
openai
passlib[bcrypt]
pipmaster
PyJWT
python-dotenv
python-jose[cryptography]
python-multipart
pytz
tenacity
tiktoken
uvicorn
jiter
httpcore
distro
httpx
openai
asyncpg
pytz
python-jose[cryptography]
passlib[bcrypt]
PyJWT

View File

@@ -18,7 +18,11 @@ from lightrag import LightRAG
from lightrag.base import DocProcessingStatus, DocStatus
from ..utils_api import get_api_key_dependency, get_auth_dependency
router = APIRouter(prefix="/documents", tags=["documents"], dependencies=[Depends(get_auth_dependency())])
router = APIRouter(
prefix="/documents",
tags=["documents"],
dependencies=[Depends(get_auth_dependency())],
)
# Temporary file prefix
temp_prefix = "__tmp__"

View File

@@ -9,12 +9,7 @@ import sys
import logging
from ascii_colors import ASCIIColors
from lightrag.api import __api_version__
from fastapi import (
HTTPException,
Security,
Depends,
Request
)
from fastapi import HTTPException, Security, Depends, Request
from dotenv import load_dotenv
from fastapi.security import APIKeyHeader, OAuth2PasswordBearer
from starlette.status import HTTP_403_FORBIDDEN
@@ -41,8 +36,8 @@ def get_auth_dependency():
whitelist = os.getenv("WHITELIST_PATHS", "").split(",")
async def dependency(
request: Request,
token: str = Depends(OAuth2PasswordBearer(tokenUrl="login", auto_error=False))
request: Request,
token: str = Depends(OAuth2PasswordBearer(tokenUrl="login", auto_error=False)),
):
if request.url.path in whitelist:
return