diff --git a/Azure_auth/jwt_auth.py b/Azure_auth/jwt_auth.py index bbaa20b..7754454 100644 --- a/Azure_auth/jwt_auth.py +++ b/Azure_auth/jwt_auth.py @@ -6,6 +6,7 @@ # load .env file load_dotenv() jwt_secret = os.getenv("EDITOR_SECRET_KEY") + #define a HTTPBearer to handle the token from client client_token_scheme = HTTPBearer() # JWT authentication function diff --git a/utils/get_manifest_conn.py b/utils/get_manifest_conn.py index ec20287..c61d567 100644 --- a/utils/get_manifest_conn.py +++ b/utils/get_manifest_conn.py @@ -16,9 +16,6 @@ logging.basicConfig(level=logging.INFO,handlers=[logging.StreamHandler()]) logger = logging.getLogger(__name__) -# Connect to Swift -conn = get_swift_connection() - async def get_manifest_conn(slug:str,request: Request): """ Retrieve file content from the specified Swift container by filename, and format JSON data. @@ -34,6 +31,7 @@ async def get_manifest_conn(slug:str,request: Request): try: #Access Swift and Redis objects from the app's state redis = request.app.state.redis + conn = request.app.state.conn manifest_name = f'{slug}/manifest.json' #Check Redis cache,if exists return from redis if (cached_profile := await redis.get(f"manifest_{slug}")) is not None: diff --git a/utils/lifespan_handler.py b/utils/lifespan_handler.py index 1993b3e..e24cacd 100644 --- a/utils/lifespan_handler.py +++ b/utils/lifespan_handler.py @@ -9,6 +9,7 @@ import os import redis.asyncio as aioredis from fastapi import HTTPException +from swift_config.swift_config import get_swift_connection #config logger logging.basicConfig(level=logging.INFO) @@ -54,6 +55,8 @@ async def lifespan(app) -> AsyncGenerator[None,None]: decode_responses=False ) + conn = get_swift_connection() + app.state.conn = conn yield except Exception as e: diff --git a/utils/upload_manifest.py b/utils/upload_manifest.py index 3457b0e..c616405 100644 --- a/utils/upload_manifest.py +++ b/utils/upload_manifest.py @@ -21,9 +21,6 @@ logging.basicConfig(level=logging.INFO,handlers=[logging.StreamHandler()]) logger = logging.getLogger(__name__) -# Connect to Swift -conn = get_swift_connection() - # Create an async context manager for acquiring and releasing a Redis lock @asynccontextmanager async def acquire_lock(redis_client: Redis, key: str, timeout: int = 30): @@ -47,6 +44,7 @@ async def upload_manifest_backend( swift_storage_url = request.app.state.swift_storage_url """ redis = request.app.state.redis + conn = request.app.state.conn """ @@ -131,7 +129,6 @@ async def upload_manifest_backend( canvas_item['items'][0]['items'][0]['id'] = annotation_id new_manifest_items.append(canvas_item) #updated_canvas_content = json.dumps(canvas_item) - # Upload manifest to Swift manifest['items'] = new_manifest_items updated_manifest = json.dumps(manifest)