Moved interal part of API to differen repository
This commit is contained in:
parent
0e7d235a6d
commit
b2af6fe289
@ -3,7 +3,7 @@ CREATE TABLE users (
|
|||||||
username VARCHAR(100) UNIQUE NOT NULL,
|
username VARCHAR(100) UNIQUE NOT NULL,
|
||||||
email VARCHAR(255) UNIQUE NOT NULL,
|
email VARCHAR(255) UNIQUE NOT NULL,
|
||||||
password_hash CHAR(64) DEFAULT NULL, -- SHA256 hash or similar
|
password_hash CHAR(64) DEFAULT NULL, -- SHA256 hash or similar
|
||||||
api_key CHAR(64) UNIQUE NOT NULL, -- for API authentication
|
api_key VARCHAR(255) UNIQUE NOT NULL, -- for API authentication
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
last_login TIMESTAMP NULL DEFAULT NULL,
|
last_login TIMESTAMP NULL DEFAULT NULL,
|
||||||
status ENUM('active','inactive','banned') NOT NULL DEFAULT 'active'
|
status ENUM('active','inactive','banned') NOT NULL DEFAULT 'active'
|
||||||
|
|||||||
23
main.py
23
main.py
@ -1,15 +1,14 @@
|
|||||||
from fastapi import FastAPI, Query, Depends, HTTPException, Header
|
from fastapi import FastAPI, Depends, HTTPException
|
||||||
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 starlette.exceptions import HTTPException as StarletteHTTPException
|
||||||
from typing import Optional,List,Dict
|
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 validator import is_valid_platform,is_valid_token,verify_api_key
|
||||||
from hvac_handler import encrypt_token
|
from hvac_handler import encrypt_token
|
||||||
from db import get_db
|
from db import get_db
|
||||||
from logger_handler import setup_logger
|
from logger_handler import setup_logger
|
||||||
import uuid
|
import uuid
|
||||||
from rabbitmq_handler import send_message_to_rmq
|
|
||||||
from hashlib import sha256
|
from hashlib import sha256
|
||||||
import uvicorn
|
import uvicorn
|
||||||
from uvicorn_logging_config import LOGGING_CONFIG
|
from uvicorn_logging_config import LOGGING_CONFIG
|
||||||
@ -19,7 +18,7 @@ from uvicorn_logging_config import LOGGING_CONFIG
|
|||||||
logger = setup_logger(__name__)
|
logger = setup_logger(__name__)
|
||||||
|
|
||||||
api_key_header = APIKeyHeader(name="X-API-Key")
|
api_key_header = APIKeyHeader(name="X-API-Key")
|
||||||
api_key_header_internal = APIKeyHeader(name="X-API-Key-Internal")
|
|
||||||
|
|
||||||
def hash_token(token: str) -> str:
|
def hash_token(token: str) -> str:
|
||||||
return sha256(token.encode()).hexdigest()
|
return sha256(token.encode()).hexdigest()
|
||||||
@ -32,18 +31,12 @@ class TokenRequest(BaseModel):
|
|||||||
locale : Optional[str] = None
|
locale : Optional[str] = None
|
||||||
topics : Optional[List[str]] = None
|
topics : Optional[List[str]] = None
|
||||||
|
|
||||||
class Notification(BaseModel):
|
|
||||||
user_id : int
|
|
||||||
message : Dict
|
|
||||||
|
|
||||||
api = FastAPI(
|
api = FastAPI(
|
||||||
title="Device Token Management",
|
title="Device Token Management",
|
||||||
description="API for requesting tokens",
|
description="API for requesting tokens",
|
||||||
version="1.0.0"
|
version="1.0.0"
|
||||||
)
|
)
|
||||||
|
|
||||||
def verify_api_key_dependency_internal():
|
|
||||||
return True
|
|
||||||
|
|
||||||
def verify_api_key_dependency(db=Depends(get_db), api_key: str = Depends(api_key_header)) -> int:
|
def verify_api_key_dependency(db=Depends(get_db), api_key: str = Depends(api_key_header)) -> int:
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
@ -145,16 +138,6 @@ def unregister_token(
|
|||||||
return {"status":"unregistered"}
|
return {"status":"unregistered"}
|
||||||
|
|
||||||
|
|
||||||
@api.post("/internal/receive-notifications")
|
|
||||||
def receive_notifications(
|
|
||||||
notification_data: Notification,
|
|
||||||
db = Depends(get_db),
|
|
||||||
is_allowed: bool = Depends(verify_api_key_dependency_internal)
|
|
||||||
):
|
|
||||||
send_message_to_rmq(notification_data.user_id,notification_data.message)
|
|
||||||
return {"status": "queued"}
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
uvicorn.run(
|
uvicorn.run(
|
||||||
"main:api",
|
"main:api",
|
||||||
|
|||||||
29
rabbitmq.md
29
rabbitmq.md
@ -1,29 +0,0 @@
|
|||||||
rabbitmqctl add_vhost app_notifications
|
|
||||||
rabbitmqctl add_user notifier strongpassword
|
|
||||||
rabbitmqctl set_user_tags notifier management
|
|
||||||
rabbitmqctl set_permissions -p app_notifications notifier ".*" ".*" ".*"
|
|
||||||
rabbitmqadmin --username "admin" --password "admin" declare exchange --vhost "app_notifications" --name "app_notifications" --type "topic" --durable "true"
|
|
||||||
rabbitmqadmin --username "admin" --password "admin" declare queue --vhost "app_notifications" --name "notifications_retry"
|
|
||||||
--durable "true"
|
|
||||||
rabbitmqadmin --username "admin" --password "admin" declare queue --vhost "app_notifications" --name "notifications_dlq"
|
|
||||||
--durable "true"
|
|
||||||
rabbitmqadmin --username "admin" --password "admin" declare queue --vhost "app_notifications" --name "notifications"
|
|
||||||
--durable "true"
|
|
||||||
rabbitmqadmin --username "admin" --password "admin" declare binding --vhost "app_notifications" --source "app_notifications" --destination "notifications" --destination-type "queue" --routing-key "notify.*"
|
|
||||||
|
|
||||||
# Retry policy: messages stay for 30s before going back to main queue
|
|
||||||
rabbitmqctl set_policy \
|
|
||||||
--vhost app_notifications \
|
|
||||||
retry_policy "^notifications_retry$" \
|
|
||||||
'{"dead-letter-exchange":"app_notifications",
|
|
||||||
"dead-letter-routing-key":"notify.retry",
|
|
||||||
"message-ttl":30000}' \
|
|
||||||
--apply-to queues
|
|
||||||
|
|
||||||
# DLQ policy: permanent dead letter storage
|
|
||||||
rabbitmqctl set_policy \
|
|
||||||
--vhost app_notifications \
|
|
||||||
dlq_policy "^notifications$" \
|
|
||||||
'{"dead-letter-exchange":"app_notifications",
|
|
||||||
"dead-letter-routing-key":"notify.dlq"}' \
|
|
||||||
--apply-to queues
|
|
||||||
@ -1,21 +0,0 @@
|
|||||||
import pika
|
|
||||||
from typing import Dict
|
|
||||||
import json
|
|
||||||
|
|
||||||
def send_message_to_rmq(user_id: int, message: Dict):
|
|
||||||
credentials = pika.credentials.PlainCredentials(username="notifier",password="strongpassword")
|
|
||||||
conn_params = pika.ConnectionParameters(host="localhost",
|
|
||||||
credentials=credentials,virtual_host="app_notifications")
|
|
||||||
connection = pika.BlockingConnection(conn_params)
|
|
||||||
#connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
|
|
||||||
channel = connection.channel()
|
|
||||||
channel.confirm_delivery()
|
|
||||||
|
|
||||||
channel.basic_publish(exchange='app_notifications',
|
|
||||||
routing_key=f"notify.user.{user_id}",
|
|
||||||
body=json.dumps(message),
|
|
||||||
properties=pika.BasicProperties(
|
|
||||||
content_type="application/json",
|
|
||||||
delivery_mode=2
|
|
||||||
))
|
|
||||||
|
|
||||||
@ -6,19 +6,18 @@ certifi==2025.8.3
|
|||||||
cffi==2.0.0
|
cffi==2.0.0
|
||||||
charset-normalizer==3.4.3
|
charset-normalizer==3.4.3
|
||||||
click==8.3.0
|
click==8.3.0
|
||||||
fastapi==0.117.1
|
fastapi==0.118.0
|
||||||
h11==0.16.0
|
h11==0.16.0
|
||||||
hvac==2.3.0
|
hvac==2.3.0
|
||||||
idna==3.10
|
idna==3.10
|
||||||
mysql-connector-python==9.4.0
|
mysql-connector-python==9.4.0
|
||||||
pika==1.3.2
|
|
||||||
pycparser==2.23
|
pycparser==2.23
|
||||||
pydantic==2.11.9
|
pydantic==2.11.9
|
||||||
pydantic_core==2.33.2
|
pydantic_core==2.33.2
|
||||||
requests==2.32.5
|
requests==2.32.5
|
||||||
sniffio==1.3.1
|
sniffio==1.3.1
|
||||||
starlette==0.48.0
|
starlette==0.48.0
|
||||||
typing-inspection==0.4.1
|
typing-inspection==0.4.2
|
||||||
typing_extensions==4.15.0
|
typing_extensions==4.15.0
|
||||||
urllib3==2.5.0
|
urllib3==2.5.0
|
||||||
uvicorn==0.37.0
|
uvicorn==0.37.0
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user