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:
@@ -29,14 +29,12 @@ class AuthHandler:
|
|||||||
|
|
||||||
if datetime.utcnow() > expire_time:
|
if datetime.utcnow() > expire_time:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
status_code=status.HTTP_401_UNAUTHORIZED, detail="Token expired"
|
||||||
detail="Token expired"
|
|
||||||
)
|
)
|
||||||
return payload["sub"]
|
return payload["sub"]
|
||||||
except jwt.PyJWTError:
|
except jwt.PyJWTError:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token"
|
||||||
detail="Invalid token"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@@ -2,13 +2,7 @@
|
|||||||
LightRAG FastAPI Server
|
LightRAG FastAPI Server
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from fastapi import (
|
from fastapi import FastAPI, Depends, HTTPException, status
|
||||||
FastAPI,
|
|
||||||
Depends,
|
|
||||||
HTTPException,
|
|
||||||
Request,
|
|
||||||
status
|
|
||||||
)
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
@@ -27,7 +21,6 @@ from lightrag.api.utils_api import (
|
|||||||
parse_args,
|
parse_args,
|
||||||
get_default_host,
|
get_default_host,
|
||||||
display_splash_screen,
|
display_splash_screen,
|
||||||
get_auth_dependency,
|
|
||||||
)
|
)
|
||||||
from lightrag import LightRAG
|
from lightrag import LightRAG
|
||||||
from lightrag.types import GPTKeywordExtractionFormat
|
from lightrag.types import GPTKeywordExtractionFormat
|
||||||
@@ -386,18 +379,17 @@ def create_app(args):
|
|||||||
if not (username and password):
|
if not (username and password):
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_501_NOT_IMPLEMENTED,
|
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:
|
if form_data.username != username or form_data.password != password:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
status_code=status.HTTP_401_UNAUTHORIZED, detail="Incorrect credentials"
|
||||||
detail="Incorrect credentials"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"access_token": auth_handler.create_token(username),
|
"access_token": auth_handler.create_token(username),
|
||||||
"token_type": "bearer"
|
"token_type": "bearer",
|
||||||
}
|
}
|
||||||
|
|
||||||
@app.get("/health", dependencies=[Depends(optional_api_key)])
|
@app.get("/health", dependencies=[Depends(optional_api_key)])
|
||||||
|
@@ -1,20 +1,20 @@
|
|||||||
aiofiles
|
aiofiles
|
||||||
ascii_colors
|
ascii_colors
|
||||||
|
asyncpg
|
||||||
|
distro
|
||||||
fastapi
|
fastapi
|
||||||
|
httpcore
|
||||||
|
httpx
|
||||||
|
jiter
|
||||||
numpy
|
numpy
|
||||||
|
openai
|
||||||
|
passlib[bcrypt]
|
||||||
pipmaster
|
pipmaster
|
||||||
|
PyJWT
|
||||||
python-dotenv
|
python-dotenv
|
||||||
|
python-jose[cryptography]
|
||||||
python-multipart
|
python-multipart
|
||||||
|
pytz
|
||||||
tenacity
|
tenacity
|
||||||
tiktoken
|
tiktoken
|
||||||
uvicorn
|
uvicorn
|
||||||
jiter
|
|
||||||
httpcore
|
|
||||||
distro
|
|
||||||
httpx
|
|
||||||
openai
|
|
||||||
asyncpg
|
|
||||||
pytz
|
|
||||||
python-jose[cryptography]
|
|
||||||
passlib[bcrypt]
|
|
||||||
PyJWT
|
|
@@ -18,7 +18,11 @@ from lightrag import LightRAG
|
|||||||
from lightrag.base import DocProcessingStatus, DocStatus
|
from lightrag.base import DocProcessingStatus, DocStatus
|
||||||
from ..utils_api import get_api_key_dependency, get_auth_dependency
|
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
|
# Temporary file prefix
|
||||||
temp_prefix = "__tmp__"
|
temp_prefix = "__tmp__"
|
||||||
|
@@ -9,12 +9,7 @@ import sys
|
|||||||
import logging
|
import logging
|
||||||
from ascii_colors import ASCIIColors
|
from ascii_colors import ASCIIColors
|
||||||
from lightrag.api import __api_version__
|
from lightrag.api import __api_version__
|
||||||
from fastapi import (
|
from fastapi import HTTPException, Security, Depends, Request
|
||||||
HTTPException,
|
|
||||||
Security,
|
|
||||||
Depends,
|
|
||||||
Request
|
|
||||||
)
|
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from fastapi.security import APIKeyHeader, OAuth2PasswordBearer
|
from fastapi.security import APIKeyHeader, OAuth2PasswordBearer
|
||||||
from starlette.status import HTTP_403_FORBIDDEN
|
from starlette.status import HTTP_403_FORBIDDEN
|
||||||
@@ -41,8 +36,8 @@ def get_auth_dependency():
|
|||||||
whitelist = os.getenv("WHITELIST_PATHS", "").split(",")
|
whitelist = os.getenv("WHITELIST_PATHS", "").split(",")
|
||||||
|
|
||||||
async def dependency(
|
async def dependency(
|
||||||
request: Request,
|
request: Request,
|
||||||
token: str = Depends(OAuth2PasswordBearer(tokenUrl="login", auto_error=False))
|
token: str = Depends(OAuth2PasswordBearer(tokenUrl="login", auto_error=False)),
|
||||||
):
|
):
|
||||||
if request.url.path in whitelist:
|
if request.url.path in whitelist:
|
||||||
return
|
return
|
||||||
|
Reference in New Issue
Block a user