|
|
@@ -1,23 +1,25 @@
|
|
|
|
from fastapi import FastAPI, Depends, HTTPException, Request
|
|
|
|
import asyncio
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
from contextlib import asynccontextmanager
|
|
|
|
|
|
|
|
from hashlib import sha256
|
|
|
|
|
|
|
|
from typing import List, Optional
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import httpx
|
|
|
|
|
|
|
|
import uvicorn
|
|
|
|
|
|
|
|
from fastapi import Depends, FastAPI, HTTPException, Request
|
|
|
|
from fastapi.responses import JSONResponse
|
|
|
|
from fastapi.responses import JSONResponse
|
|
|
|
from fastapi.security.api_key import APIKeyHeader
|
|
|
|
from fastapi.security.api_key import APIKeyHeader
|
|
|
|
from starlette.exceptions import HTTPException as StarletteHTTPException
|
|
|
|
|
|
|
|
from typing import Optional,List
|
|
|
|
|
|
|
|
from pydantic import BaseModel
|
|
|
|
from pydantic import BaseModel
|
|
|
|
from validator import is_valid_platform,is_valid_token,verify_api_key
|
|
|
|
from starlette.exceptions import HTTPException as StarletteHTTPException
|
|
|
|
from secret_manager.fernet_encryption import encrypt_token
|
|
|
|
|
|
|
|
from secret_manager import cleanup_secret_files, return_credentials
|
|
|
|
from db_module import close_connection_pool, create_connection_pool, get_db, start_healthcheck_thread
|
|
|
|
from db_module import get_db, create_connection_pool, close_connection_pool, start_healthcheck_thread
|
|
|
|
|
|
|
|
from simple_logger_handler import setup_logger, LOG_LEVEL
|
|
|
|
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
from hashlib import sha256
|
|
|
|
|
|
|
|
import uvicorn
|
|
|
|
|
|
|
|
from contextlib import asynccontextmanager
|
|
|
|
|
|
|
|
from metrics_server import REQUEST_COUNTER
|
|
|
|
from metrics_server import REQUEST_COUNTER
|
|
|
|
import asyncio
|
|
|
|
from secret_manager import cleanup_secret_files, return_credentials
|
|
|
|
|
|
|
|
from secret_manager.fernet_encryption import encrypt_token
|
|
|
|
|
|
|
|
from simple_logger_handler import LOG_LEVEL, setup_logger
|
|
|
|
from uvicorn_logger_config import LOGGING_CONFIG
|
|
|
|
from uvicorn_logger_config import LOGGING_CONFIG
|
|
|
|
import httpx
|
|
|
|
from validator import is_valid_platform, is_valid_token, verify_api_key
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logger = setup_logger(__name__)
|
|
|
|
logger = setup_logger(__name__)
|
|
|
|
api_key_header = APIKeyHeader(name="X-API-Key")
|
|
|
|
api_key_header = APIKeyHeader(name="X-API-Key")
|
|
|
@@ -168,7 +170,7 @@ def register_token(
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute(
|
|
|
|
cursor.execute(
|
|
|
|
"SELECT * FROM device_tokens WHERE user_id=%s AND hashed_token=%s",
|
|
|
|
"SELECT * FROM device_tokens WHERE user_id=%s AND hashed_token=%s",
|
|
|
|
(user_id,hashed_token))
|
|
|
|
(user_id,hashed_token))
|
|
|
|
existing = cursor.fetchone()
|
|
|
|
existing = cursor.fetchone()
|
|
|
|
logger.debug(f"[FastAPI] Existing token fetched: {existing}")
|
|
|
|
logger.debug(f"[FastAPI] Existing token fetched: {existing}")
|
|
|
|
|
|
|
|
|
|
|
@@ -178,8 +180,8 @@ def register_token(
|
|
|
|
UPDATE device_tokens
|
|
|
|
UPDATE device_tokens
|
|
|
|
SET platform=%s, app_ver=%s, locale=%s, topics=%s, last_seen_at=NOW()
|
|
|
|
SET platform=%s, app_ver=%s, locale=%s, topics=%s, last_seen_at=NOW()
|
|
|
|
WHERE user_id=%s AND hashed_token=%s
|
|
|
|
WHERE user_id=%s AND hashed_token=%s
|
|
|
|
""", (request_data.platform,
|
|
|
|
""",(request_data.platform,
|
|
|
|
request_data.app_ver,
|
|
|
|
request_data.app_ver,
|
|
|
|
request_data.locale,
|
|
|
|
request_data.locale,
|
|
|
|
request_data.topics,
|
|
|
|
request_data.topics,
|
|
|
|
user_id,
|
|
|
|
user_id,
|
|
|
@@ -193,7 +195,7 @@ def register_token(
|
|
|
|
(token_id, user_id, platform, token, hashed_token, status, app_ver, locale, topics, created_at)
|
|
|
|
(token_id, user_id, platform, token, hashed_token, status, app_ver, locale, topics, created_at)
|
|
|
|
VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,NOW())
|
|
|
|
VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,NOW())
|
|
|
|
""", (token_id,
|
|
|
|
""", (token_id,
|
|
|
|
user_id,
|
|
|
|
user_id,
|
|
|
|
request_data.platform,
|
|
|
|
request_data.platform,
|
|
|
|
secure_token,
|
|
|
|
secure_token,
|
|
|
|
hashed_token,
|
|
|
|
hashed_token,
|
|
|
@@ -207,7 +209,7 @@ def register_token(
|
|
|
|
logger.info(f"[FastAPI] Success: Registering token for user_id={user_id}, platform={request_data.platform}")
|
|
|
|
logger.info(f"[FastAPI] Success: Registering token for user_id={user_id}, platform={request_data.platform}")
|
|
|
|
except Exception as e:
|
|
|
|
except Exception as e:
|
|
|
|
logger.exception(f"[FastAPI] Failed to register token for user_id={user_id}: {e}")
|
|
|
|
logger.exception(f"[FastAPI] Failed to register token for user_id={user_id}: {e}")
|
|
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
raise HTTPException(status_code=500, detail=str(e)) from e
|
|
|
|
|
|
|
|
|
|
|
|
return {"status":"registered"}
|
|
|
|
return {"status":"registered"}
|
|
|
|
|
|
|
|
|
|
|
@@ -230,7 +232,7 @@ def unregister_token(
|
|
|
|
""", ('expired', hashed_token))
|
|
|
|
""", ('expired', hashed_token))
|
|
|
|
except Exception as e:
|
|
|
|
except Exception as e:
|
|
|
|
logger.exception(f"[FastAPI] Failed to unregister token for user_id={user_id}: {e}")
|
|
|
|
logger.exception(f"[FastAPI] Failed to unregister token for user_id={user_id}: {e}")
|
|
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
raise HTTPException(status_code=500, detail=str(e)) from e
|
|
|
|
logger.info(f"[FastAPI] Success: Unregistering token for user_id={user_id}, platform={request_data.platform}")
|
|
|
|
logger.info(f"[FastAPI] Success: Unregistering token for user_id={user_id}, platform={request_data.platform}")
|
|
|
|
return {"status":"unregistered"}
|
|
|
|
return {"status":"unregistered"}
|
|
|
|
|
|
|
|
|
|
|
@@ -248,10 +250,10 @@ async def pull_notifications(
|
|
|
|
data = PullResponse.model_validate(resp.json())
|
|
|
|
data = PullResponse.model_validate(resp.json())
|
|
|
|
except httpx.HTTPStatusError as e:
|
|
|
|
except httpx.HTTPStatusError as e:
|
|
|
|
logger.error(f"[FastAPI] Upstream returned {e.response.status_code} for user_id={user_id}")
|
|
|
|
logger.error(f"[FastAPI] Upstream returned {e.response.status_code} for user_id={user_id}")
|
|
|
|
raise HTTPException(status_code=502, detail="Backend-API-Internal unavailable")
|
|
|
|
raise HTTPException(status_code=502, detail="Backend-API-Internal unavailable") from e
|
|
|
|
except ValueError as e:
|
|
|
|
except ValueError as e:
|
|
|
|
logger.error(f"[FastAPI] Backend-API-Internal call failed with ValueError for user_id={user_id}: {e!r}")
|
|
|
|
logger.error(f"[FastAPI] Backend-API-Internal call failed with ValueError for user_id={user_id}: {e!r}")
|
|
|
|
raise HTTPException(status_code=502, detail="Backend-API-Internal unavailable")
|
|
|
|
raise HTTPException(status_code=502, detail="Backend-API-Internal unavailable") from e
|
|
|
|
|
|
|
|
|
|
|
|
logger.info(f"[FastAPI] Returning {len(data.messages)} message(s) for user_id={user_id}, has_more={data.has_more}")
|
|
|
|
logger.info(f"[FastAPI] Returning {len(data.messages)} message(s) for user_id={user_id}, has_more={data.has_more}")
|
|
|
|
return data
|
|
|
|
return data
|
|
|
|