forked from mtusi2026/mtusi2026lab3
grade3
This commit is contained in:
@@ -2,6 +2,8 @@ import os
|
|||||||
import base64
|
import base64
|
||||||
import secrets
|
import secrets
|
||||||
import uuid
|
import uuid
|
||||||
|
import json
|
||||||
|
import re
|
||||||
from datetime import datetime, timezone, timedelta
|
from datetime import datetime, timezone, timedelta
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
@@ -16,55 +18,24 @@ from pydantic import BaseModel, Field, field_validator, ConfigDict
|
|||||||
|
|
||||||
import redis
|
import redis
|
||||||
|
|
||||||
app = FastAPI(title="MTUSI Messenger — lab2", version="1.2.0")
|
import hashlib
|
||||||
|
import hmac
|
||||||
|
|
||||||
|
app = FastAPI(title="MTUSI Messenger — lab3", version="1.3.0")
|
||||||
|
|
||||||
DATABASE_URL = os.getenv("DATABASE_URL", "postgresql://postgres:postgres@postgres:5432/messenger")
|
DATABASE_URL = os.getenv("DATABASE_URL", "postgresql://postgres:postgres@postgres:5432/messenger")
|
||||||
SESSION_TTL_SECONDS = int(os.getenv("SESSION_TTL_SECONDS", "3600"))
|
SESSION_TTL_SECONDS = int(os.getenv("SESSION_TTL_SECONDS", "3600"))
|
||||||
|
INSTANCE_ID = os.getenv("INSTANCE_ID", "instance-1")
|
||||||
|
|
||||||
REDIS_URL = os.getenv("REDIS_URL", "redis://redis:6379/0")
|
REDIS_URL = os.getenv("REDIS_URL", "redis://redis:6379/0")
|
||||||
redis_client = redis.Redis.from_url(REDIS_URL, decode_responses=True)
|
redis_client = redis.Redis.from_url(REDIS_URL, decode_responses=True)
|
||||||
|
|
||||||
|
SECRET_HMAC_KEY = os.environ["SECRET_HMAC_KEY"]
|
||||||
|
|
||||||
def get_db_connection():
|
def get_db_connection():
|
||||||
conn = psycopg2.connect(DATABASE_URL, cursor_factory=psycopg2.extras.RealDictCursor)
|
conn = psycopg2.connect(DATABASE_URL, cursor_factory=psycopg2.extras.RealDictCursor)
|
||||||
return conn
|
return conn
|
||||||
|
|
||||||
def init_db():
|
|
||||||
conn = get_db_connection()
|
|
||||||
with conn.cursor() as cur:
|
|
||||||
cur.execute("""
|
|
||||||
CREATE TABLE IF NOT EXISTS users (
|
|
||||||
id UUID PRIMARY KEY,
|
|
||||||
username VARCHAR(32) UNIQUE NOT NULL,
|
|
||||||
display_name VARCHAR(100) NOT NULL,
|
|
||||||
password_hash VARCHAR(255) NOT NULL,
|
|
||||||
created_at TIMESTAMPTZ NOT NULL
|
|
||||||
);
|
|
||||||
CREATE TABLE IF NOT EXISTS chats (
|
|
||||||
id UUID PRIMARY KEY,
|
|
||||||
title VARCHAR(100) NOT NULL,
|
|
||||||
created_at TIMESTAMPTZ NOT NULL
|
|
||||||
);
|
|
||||||
CREATE TABLE IF NOT EXISTS chat_members (
|
|
||||||
chat_id UUID REFERENCES chats(id) ON DELETE CASCADE,
|
|
||||||
user_id UUID REFERENCES users(id) ON DELETE CASCADE,
|
|
||||||
PRIMARY KEY (chat_id, user_id)
|
|
||||||
);
|
|
||||||
CREATE TABLE IF NOT EXISTS messages (
|
|
||||||
id UUID PRIMARY KEY,
|
|
||||||
chat_id UUID REFERENCES chats(id) ON DELETE CASCADE,
|
|
||||||
sender_id UUID REFERENCES users(id) ON DELETE CASCADE,
|
|
||||||
text VARCHAR(4000) NOT NULL,
|
|
||||||
created_at TIMESTAMPTZ NOT NULL
|
|
||||||
);
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_messages_chat_pagination ON messages(chat_id, created_at, id);
|
|
||||||
""")
|
|
||||||
conn.commit()
|
|
||||||
conn.close()
|
|
||||||
|
|
||||||
@app.on_event("startup")
|
|
||||||
def startup_event():
|
|
||||||
init_db()
|
|
||||||
|
|
||||||
def generate_request_id() -> str:
|
def generate_request_id() -> str:
|
||||||
return uuid.uuid4().hex
|
return uuid.uuid4().hex
|
||||||
|
|
||||||
@@ -75,7 +46,7 @@ def make_error_response(
|
|||||||
req_id: str,
|
req_id: str,
|
||||||
extra_headers: Optional[dict] = None,
|
extra_headers: Optional[dict] = None,
|
||||||
) -> JSONResponse:
|
) -> JSONResponse:
|
||||||
headers = {"X-Request-Id": req_id, "X-Instance-Id": "instance-1"}
|
headers = {"X-Request-Id": req_id, "X-Instance-Id": INSTANCE_ID}
|
||||||
if extra_headers:
|
if extra_headers:
|
||||||
headers.update(extra_headers)
|
headers.update(extra_headers)
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
@@ -90,6 +61,33 @@ def make_error_response(
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def encode_cursor(chat_id: str, created_at: str, msg_id: str) -> str:
|
||||||
|
payload = f"{chat_id}|{created_at}|{msg_id}"
|
||||||
|
signature = hmac.new(SECRET_HMAC_KEY.encode(), payload.encode(), hashlib.sha256).hexdigest()[:16]
|
||||||
|
raw = f"{payload}|{signature}"
|
||||||
|
return base64.urlsafe_b64encode(raw.encode()).decode()
|
||||||
|
|
||||||
|
def decode_cursor(cursor: str, expected_chat_id: str) -> tuple:
|
||||||
|
try:
|
||||||
|
decoded = base64.urlsafe_b64decode(cursor.encode()).decode()
|
||||||
|
parts = decoded.split("|")
|
||||||
|
if len(parts) != 4:
|
||||||
|
raise ValueError()
|
||||||
|
chat_id, created_at, msg_id, signature = parts
|
||||||
|
|
||||||
|
if chat_id != expected_chat_id:
|
||||||
|
raise HTTPException(status_code=400, detail="Cursor belongs to another chat")
|
||||||
|
|
||||||
|
expected_sig = hmac.new(SECRET_HMAC_KEY.encode(), f"{chat_id}|{created_at}|{msg_id}".encode(), hashlib.sha256).hexdigest()[:16]
|
||||||
|
if not hmac.compare_digest(signature, expected_sig):
|
||||||
|
raise ValueError()
|
||||||
|
|
||||||
|
return created_at, msg_id
|
||||||
|
except HTTPException:
|
||||||
|
raise
|
||||||
|
except Exception:
|
||||||
|
raise HTTPException(status_code=400, detail="Invalid cursor format")
|
||||||
|
|
||||||
@app.exception_handler(RequestValidationError)
|
@app.exception_handler(RequestValidationError)
|
||||||
async def validation_exception_handler(request: Request, exc: RequestValidationError):
|
async def validation_exception_handler(request: Request, exc: RequestValidationError):
|
||||||
req_id = request.headers.get("X-Request-Id") or generate_request_id()
|
req_id = request.headers.get("X-Request-Id") or generate_request_id()
|
||||||
@@ -103,7 +101,7 @@ async def validation_exception_handler(request: Request, exc: RequestValidationE
|
|||||||
@app.exception_handler(HTTPException)
|
@app.exception_handler(HTTPException)
|
||||||
async def http_exception_handler(request: Request, exc: HTTPException):
|
async def http_exception_handler(request: Request, exc: HTTPException):
|
||||||
req_id = request.headers.get("X-Request-Id") or generate_request_id()
|
req_id = request.headers.get("X-Request-Id") or generate_request_id()
|
||||||
headers = {"X-Request-Id": req_id, "X-Instance-Id": "instance-1"}
|
headers = {"X-Request-Id": req_id, "X-Instance-Id": INSTANCE_ID}
|
||||||
if exc.headers:
|
if exc.headers:
|
||||||
headers.update(exc.headers)
|
headers.update(exc.headers)
|
||||||
|
|
||||||
@@ -135,6 +133,16 @@ async def http_exception_handler(request: Request, exc: HTTPException):
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@app.exception_handler(Exception)
|
||||||
|
async def unhandled_exception_handler(request: Request, exc: Exception):
|
||||||
|
req_id = request.headers.get("X-Request-Id") or generate_request_id()
|
||||||
|
return make_error_response(
|
||||||
|
status_code=500,
|
||||||
|
code="internal_error",
|
||||||
|
message="Internal server error",
|
||||||
|
req_id=req_id,
|
||||||
|
)
|
||||||
|
|
||||||
class CreateUser(BaseModel):
|
class CreateUser(BaseModel):
|
||||||
model_config = ConfigDict(extra='forbid')
|
model_config = ConfigDict(extra='forbid')
|
||||||
username: str = Field(..., pattern=r"^[a-z0-9_]{3,32}$")
|
username: str = Field(..., pattern=r"^[a-z0-9_]{3,32}$")
|
||||||
@@ -172,14 +180,14 @@ class CreateMessage(BaseModel):
|
|||||||
def health_live(response: Response):
|
def health_live(response: Response):
|
||||||
req_id = generate_request_id()
|
req_id = generate_request_id()
|
||||||
response.headers["X-Request-Id"] = req_id
|
response.headers["X-Request-Id"] = req_id
|
||||||
response.headers["X-Instance-Id"] = "instance-1"
|
response.headers["X-Instance-Id"] = INSTANCE_ID
|
||||||
return {"status": "ok"}
|
return {"status": "ok"}
|
||||||
|
|
||||||
@app.get("/health/ready")
|
@app.get("/health/ready")
|
||||||
def health_ready(response: Response):
|
def health_ready(response: Response):
|
||||||
req_id = generate_request_id()
|
req_id = generate_request_id()
|
||||||
response.headers["X-Request-Id"] = req_id
|
response.headers["X-Request-Id"] = req_id
|
||||||
response.headers["X-Instance-Id"] = "instance-1"
|
response.headers["X-Instance-Id"] = INSTANCE_ID
|
||||||
try:
|
try:
|
||||||
conn = get_db_connection()
|
conn = get_db_connection()
|
||||||
conn.close()
|
conn.close()
|
||||||
@@ -192,7 +200,7 @@ def health_ready(response: Response):
|
|||||||
def register_user(body: CreateUser, response: Response):
|
def register_user(body: CreateUser, response: Response):
|
||||||
req_id = generate_request_id()
|
req_id = generate_request_id()
|
||||||
response.headers["X-Request-Id"] = req_id
|
response.headers["X-Request-Id"] = req_id
|
||||||
response.headers["X-Instance-Id"] = "instance-1"
|
response.headers["X-Instance-Id"] = INSTANCE_ID
|
||||||
|
|
||||||
password_hash = bcrypt.hashpw(body.password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
|
password_hash = bcrypt.hashpw(body.password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
|
||||||
user_id = str(uuid.uuid4())
|
user_id = str(uuid.uuid4())
|
||||||
@@ -223,7 +231,7 @@ def register_user(body: CreateUser, response: Response):
|
|||||||
def create_session(authorization: Optional[str] = Header(None), response: Response = None):
|
def create_session(authorization: Optional[str] = Header(None), response: Response = None):
|
||||||
req_id = generate_request_id()
|
req_id = generate_request_id()
|
||||||
response.headers["X-Request-Id"] = req_id
|
response.headers["X-Request-Id"] = req_id
|
||||||
response.headers["X-Instance-Id"] = "instance-1"
|
response.headers["X-Instance-Id"] = INSTANCE_ID
|
||||||
|
|
||||||
if not authorization or not authorization.startswith("Basic "):
|
if not authorization or not authorization.startswith("Basic "):
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
@@ -260,7 +268,6 @@ def create_session(authorization: Optional[str] = Header(None), response: Respon
|
|||||||
redis_client.setex(f"session:{token}", SESSION_TTL_SECONDS, user['id'])
|
redis_client.setex(f"session:{token}", SESSION_TTL_SECONDS, user['id'])
|
||||||
expires_at = datetime.now(timezone.utc) + timedelta(seconds=SESSION_TTL_SECONDS)
|
expires_at = datetime.now(timezone.utc) + timedelta(seconds=SESSION_TTL_SECONDS)
|
||||||
expires_at_str = expires_at.isoformat().replace("+00:00", "Z")
|
expires_at_str = expires_at.isoformat().replace("+00:00", "Z")
|
||||||
created_at_str = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"token": token,
|
"token": token,
|
||||||
@@ -327,7 +334,6 @@ def revoke_session(authorization: Optional[str] = Header(None)):
|
|||||||
)
|
)
|
||||||
|
|
||||||
token = authorization.split(" ")[1]
|
token = authorization.split(" ")[1]
|
||||||
|
|
||||||
deleted = redis_client.delete(f"session:{token}")
|
deleted = redis_client.delete(f"session:{token}")
|
||||||
|
|
||||||
if not deleted:
|
if not deleted:
|
||||||
@@ -341,7 +347,7 @@ def revoke_session(authorization: Optional[str] = Header(None)):
|
|||||||
status_code=204,
|
status_code=204,
|
||||||
headers={
|
headers={
|
||||||
"X-Request-Id": req_id,
|
"X-Request-Id": req_id,
|
||||||
"X-Instance-Id": "instance-1"
|
"X-Instance-Id": INSTANCE_ID
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -349,14 +355,14 @@ def revoke_session(authorization: Optional[str] = Header(None)):
|
|||||||
def current_user(response: Response, authorization: Optional[str] = Header(None)):
|
def current_user(response: Response, authorization: Optional[str] = Header(None)):
|
||||||
req_id = generate_request_id()
|
req_id = generate_request_id()
|
||||||
response.headers["X-Request-Id"] = req_id
|
response.headers["X-Request-Id"] = req_id
|
||||||
response.headers["X-Instance-Id"] = "instance-1"
|
response.headers["X-Instance-Id"] = INSTANCE_ID
|
||||||
return get_current_user_from_token(authorization)
|
return get_current_user_from_token(authorization)
|
||||||
|
|
||||||
@app.post("/api/v1/chats", status_code=201)
|
@app.post("/api/v1/chats", status_code=201)
|
||||||
def create_chat(body: CreateChat, response: Response, authorization: Optional[str] = Header(None)):
|
def create_chat(body: CreateChat, response: Response, authorization: Optional[str] = Header(None)):
|
||||||
req_id = generate_request_id()
|
req_id = generate_request_id()
|
||||||
response.headers["X-Request-Id"] = req_id
|
response.headers["X-Request-Id"] = req_id
|
||||||
response.headers["X-Instance-Id"] = "instance-1"
|
response.headers["X-Instance-Id"] = INSTANCE_ID
|
||||||
user = get_current_user_from_token(authorization)
|
user = get_current_user_from_token(authorization)
|
||||||
|
|
||||||
member_ids_str = [str(mid) for mid in body.member_ids]
|
member_ids_str = [str(mid) for mid in body.member_ids]
|
||||||
@@ -377,7 +383,6 @@ def create_chat(body: CreateChat, response: Response, authorization: Optional[st
|
|||||||
chat_id = str(uuid.uuid4())
|
chat_id = str(uuid.uuid4())
|
||||||
created_at = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
|
created_at = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
|
||||||
|
|
||||||
conn = get_db_connection()
|
|
||||||
try:
|
try:
|
||||||
with conn.cursor() as cur:
|
with conn.cursor() as cur:
|
||||||
cur.execute(
|
cur.execute(
|
||||||
@@ -407,7 +412,7 @@ def create_chat(body: CreateChat, response: Response, authorization: Optional[st
|
|||||||
def list_chats(limit: int = 50, cursor: Optional[str] = None, response: Response = None, authorization: Optional[str] = Header(None)):
|
def list_chats(limit: int = 50, cursor: Optional[str] = None, response: Response = None, authorization: Optional[str] = Header(None)):
|
||||||
req_id = generate_request_id()
|
req_id = generate_request_id()
|
||||||
response.headers["X-Request-Id"] = req_id
|
response.headers["X-Request-Id"] = req_id
|
||||||
response.headers["X-Instance-Id"] = "instance-1"
|
response.headers["X-Instance-Id"] = INSTANCE_ID
|
||||||
user = get_current_user_from_token(authorization)
|
user = get_current_user_from_token(authorization)
|
||||||
|
|
||||||
if limit < 1 or limit > 100:
|
if limit < 1 or limit > 100:
|
||||||
@@ -466,7 +471,7 @@ def list_chats(limit: int = 50, cursor: Optional[str] = None, response: Response
|
|||||||
def get_chat(chat_id: UUID, response: Response, authorization: Optional[str] = Header(None)):
|
def get_chat(chat_id: UUID, response: Response, authorization: Optional[str] = Header(None)):
|
||||||
req_id = generate_request_id()
|
req_id = generate_request_id()
|
||||||
response.headers["X-Request-Id"] = req_id
|
response.headers["X-Request-Id"] = req_id
|
||||||
response.headers["X-Instance-Id"] = "instance-1"
|
response.headers["X-Instance-Id"] = INSTANCE_ID
|
||||||
user = get_current_user_from_token(authorization)
|
user = get_current_user_from_token(authorization)
|
||||||
|
|
||||||
chat_id_str = str(chat_id)
|
chat_id_str = str(chat_id)
|
||||||
@@ -494,54 +499,111 @@ def get_chat(chat_id: UUID, response: Response, authorization: Optional[str] = H
|
|||||||
}
|
}
|
||||||
|
|
||||||
@app.post("/api/v1/chats/{chat_id}/messages", status_code=201)
|
@app.post("/api/v1/chats/{chat_id}/messages", status_code=201)
|
||||||
def create_message(chat_id: UUID, body: CreateMessage, response: Response, authorization: Optional[str] = Header(None)):
|
def create_message(
|
||||||
|
chat_id: UUID,
|
||||||
|
body: CreateMessage,
|
||||||
|
response: Response,
|
||||||
|
authorization: Optional[str] = Header(None),
|
||||||
|
idempotency_key: Optional[str] = Header(None, alias="Idempotency-Key")
|
||||||
|
):
|
||||||
req_id = generate_request_id()
|
req_id = generate_request_id()
|
||||||
response.headers["X-Request-Id"] = req_id
|
response.headers["X-Request-Id"] = req_id
|
||||||
response.headers["X-Instance-Id"] = "instance-1"
|
response.headers["X-Instance-Id"] = INSTANCE_ID
|
||||||
user = get_current_user_from_token(authorization)
|
user = get_current_user_from_token(authorization)
|
||||||
|
|
||||||
|
if not idempotency_key or not re.match(r"^[A-Za-z0-9._:-]{1,128}$", idempotency_key):
|
||||||
|
raise HTTPException(status_code=400, detail="Missing or invalid Idempotency-Key")
|
||||||
|
|
||||||
chat_id_str = str(chat_id)
|
chat_id_str = str(chat_id)
|
||||||
|
method = "POST"
|
||||||
|
path = f"/api/v1/chats/{chat_id_str}/messages"
|
||||||
|
|
||||||
|
body_json_str = json.dumps(body.model_dump(), sort_keys=True)
|
||||||
|
request_hash = hashlib.sha256(body_json_str.encode("utf-8")).hexdigest()
|
||||||
|
|
||||||
conn = get_db_connection()
|
conn = get_db_connection()
|
||||||
with conn.cursor() as cur:
|
try:
|
||||||
cur.execute("""
|
with conn.cursor() as cur:
|
||||||
SELECT 1 FROM chat_members WHERE chat_id = %s AND user_id = %s
|
cur.execute(
|
||||||
""", (chat_id_str, user["id"]))
|
"SELECT 1 FROM chat_members WHERE chat_id = %s AND user_id = %s",
|
||||||
if not cur.fetchone():
|
(chat_id_str, user["id"])
|
||||||
conn.close()
|
)
|
||||||
raise HTTPException(status_code=404, detail="Chat not found")
|
if not cur.fetchone():
|
||||||
|
conn.close()
|
||||||
|
raise HTTPException(status_code=404, detail="Chat not found")
|
||||||
|
|
||||||
|
msg_id = str(uuid.uuid4())
|
||||||
|
created_at = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
|
||||||
|
|
||||||
msg_id = str(uuid.uuid4())
|
msg_response = {
|
||||||
created_at = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
|
"id": msg_id,
|
||||||
|
"chat_id": chat_id_str,
|
||||||
with conn.cursor() as cur:
|
"sender_id": user["id"],
|
||||||
cur.execute(
|
"text": body.text,
|
||||||
"INSERT INTO messages (id, chat_id, sender_id, text, created_at) VALUES (%s, %s, %s, %s, %s)",
|
"created_at": created_at
|
||||||
(msg_id, chat_id_str, user["id"], body.text, created_at)
|
}
|
||||||
)
|
|
||||||
conn.commit()
|
cur.execute("""
|
||||||
conn.close()
|
INSERT INTO idempotency_keys (user_id, method, path, key, request_hash, response_status, response_body)
|
||||||
|
VALUES (%s, %s, %s, %s, %s, %s, %s)
|
||||||
return {
|
ON CONFLICT (user_id, method, path, key) DO NOTHING
|
||||||
"id": msg_id,
|
""", (user["id"], method, path, idempotency_key, request_hash, 201, json.dumps(msg_response)))
|
||||||
"chat_id": chat_id_str,
|
|
||||||
"sender_id": user["id"],
|
if cur.rowcount == 0:
|
||||||
"text": body.text,
|
cur.execute("""
|
||||||
"created_at": created_at
|
SELECT request_hash, response_status, response_body
|
||||||
}
|
FROM idempotency_keys
|
||||||
|
WHERE user_id = %s AND method = %s AND path = %s AND key = %s
|
||||||
|
""", (user["id"], method, path, idempotency_key))
|
||||||
|
row = cur.fetchone()
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
if row["request_hash"] != request_hash:
|
||||||
|
raise HTTPException(status_code=409, detail="Idempotency key conflict with different payload")
|
||||||
|
|
||||||
|
return row["response_body"]
|
||||||
|
|
||||||
|
cur.execute(
|
||||||
|
"INSERT INTO messages (id, chat_id, sender_id, text, created_at) VALUES (%s, %s, %s, %s, %s)",
|
||||||
|
(msg_id, chat_id_str, user["id"], body.text, created_at)
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
except HTTPException:
|
||||||
|
raise
|
||||||
|
except Exception as exc:
|
||||||
|
conn.rollback()
|
||||||
|
conn.close()
|
||||||
|
raise HTTPException(status_code=500, detail="Internal error processing message")
|
||||||
|
finally:
|
||||||
|
if not conn.closed:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
return msg_response
|
||||||
|
|
||||||
@app.get("/api/v1/chats/{chat_id}/messages")
|
@app.get("/api/v1/chats/{chat_id}/messages")
|
||||||
def list_messages(chat_id: UUID, limit: int = 50, cursor: Optional[str] = None, response: Response = None, authorization: Optional[str] = Header(None)):
|
def list_messages(chat_id: UUID, limit: int = 50, cursor: Optional[str] = None, response: Response = None, authorization: Optional[str] = Header(None)):
|
||||||
req_id = generate_request_id()
|
req_id = generate_request_id()
|
||||||
response.headers["X-Request-Id"] = req_id
|
response.headers["X-Request-Id"] = req_id
|
||||||
response.headers["X-Instance-Id"] = "instance-1"
|
response.headers["X-Instance-Id"] = INSTANCE_ID
|
||||||
user = get_current_user_from_token(authorization)
|
user = get_current_user_from_token(authorization)
|
||||||
|
|
||||||
chat_id_str = str(chat_id)
|
chat_id_str = str(chat_id)
|
||||||
conn = get_db_connection()
|
conn = get_db_connection()
|
||||||
|
|
||||||
|
with conn.cursor() as cur:
|
||||||
|
cur.execute(
|
||||||
|
"SELECT 1 FROM chat_members WHERE chat_id = %s AND user_id = %s",
|
||||||
|
(chat_id_str, user["id"])
|
||||||
|
)
|
||||||
|
if not cur.fetchone():
|
||||||
|
conn.close()
|
||||||
|
raise HTTPException(status_code=404, detail="Chat not found")
|
||||||
|
|
||||||
if limit < 1 or limit > 100:
|
if limit < 1 or limit > 100:
|
||||||
|
conn.close()
|
||||||
raise HTTPException(status_code=400, detail="Invalid limit")
|
raise HTTPException(status_code=400, detail="Invalid limit")
|
||||||
|
|
||||||
query = """
|
query = """
|
||||||
SELECT id, chat_id, sender_id, text, created_at FROM messages
|
SELECT id, chat_id, sender_id, text, created_at FROM messages
|
||||||
WHERE chat_id = %s
|
WHERE chat_id = %s
|
||||||
@@ -549,17 +611,11 @@ def list_messages(chat_id: UUID, limit: int = 50, cursor: Optional[str] = None,
|
|||||||
params = [chat_id_str]
|
params = [chat_id_str]
|
||||||
|
|
||||||
if cursor:
|
if cursor:
|
||||||
try:
|
c_time, c_id = decode_cursor(cursor, chat_id_str)
|
||||||
parts = cursor.split("_")
|
query += " AND (created_at, id) > (%s, %s)"
|
||||||
c_time, c_id = parts[0], parts[1]
|
params.extend([c_time, c_id])
|
||||||
query += " AND (created_at, id) > (%s, %s)"
|
|
||||||
params.extend([c_time, c_id])
|
|
||||||
except Exception:
|
|
||||||
conn.close()
|
|
||||||
raise HTTPException(status_code=400, detail="Invalid cursor format")
|
|
||||||
|
|
||||||
query += " ORDER BY created_at ASC, id ASC LIMIT %s"
|
query += " ORDER BY created_at ASC, id ASC LIMIT %s"
|
||||||
|
|
||||||
params.append(limit + 1)
|
params.append(limit + 1)
|
||||||
|
|
||||||
with conn.cursor() as cur:
|
with conn.cursor() as cur:
|
||||||
@@ -583,6 +639,6 @@ def list_messages(chat_id: UUID, limit: int = 50, cursor: Optional[str] = None,
|
|||||||
next_cursor = None
|
next_cursor = None
|
||||||
if has_next and formatted_msgs:
|
if has_next and formatted_msgs:
|
||||||
last = formatted_msgs[-1]
|
last = formatted_msgs[-1]
|
||||||
next_cursor = f"{last['created_at']}_{last['id']}"
|
next_cursor = encode_cursor(chat_id_str, last['created_at'], last['id'])
|
||||||
|
|
||||||
return {"items": formatted_msgs, "next_cursor": next_cursor}
|
return {"items": formatted_msgs, "next_cursor": next_cursor}
|
||||||
+55
-6
@@ -1,13 +1,12 @@
|
|||||||
services:
|
services:
|
||||||
postgres:
|
postgres:
|
||||||
image: postgres:15-alpine
|
image: postgres:15.5-alpine3.19
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_DB: messenger
|
POSTGRES_DB: messenger
|
||||||
POSTGRES_USER: postgres
|
POSTGRES_USER: postgres
|
||||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env}
|
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env}
|
||||||
volumes:
|
volumes:
|
||||||
- pgdata:/var/lib/postgresql/data
|
- pgdata:/var/lib/postgresql/data
|
||||||
- ./migrations/init.sql:/docker-entrypoint-initdb.d/init.sql
|
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD-SHELL", "pg_isready -U postgres -d messenger"]
|
test: ["CMD-SHELL", "pg_isready -U postgres -d messenger"]
|
||||||
interval: 3s
|
interval: 3s
|
||||||
@@ -15,7 +14,7 @@ services:
|
|||||||
retries: 5
|
retries: 5
|
||||||
|
|
||||||
redis:
|
redis:
|
||||||
image: redis:7-alpine
|
image: redis:7.2.4-alpine
|
||||||
command: redis-server --save 60 1 --loglevel warning
|
command: redis-server --save 60 1 --loglevel warning
|
||||||
volumes:
|
volumes:
|
||||||
- redisdata:/data
|
- redisdata:/data
|
||||||
@@ -25,25 +24,75 @@ services:
|
|||||||
timeout: 3s
|
timeout: 3s
|
||||||
retries: 5
|
retries: 5
|
||||||
|
|
||||||
api:
|
migrate:
|
||||||
|
image: postgres:15.5-alpine3.19
|
||||||
|
environment:
|
||||||
|
PGPASSWORD: ${POSTGRES_PASSWORD}
|
||||||
|
volumes:
|
||||||
|
- ./migrations:/migrations
|
||||||
|
command: >
|
||||||
|
sh -c "until pg_isready -h postgres -U postgres; do sleep 1; done;
|
||||||
|
psql -h postgres -U postgres -d messenger -f /migrations/init.sql"
|
||||||
|
depends_on:
|
||||||
|
postgres:
|
||||||
|
condition: service_healthy
|
||||||
|
restart: "no"
|
||||||
|
|
||||||
|
api-1:
|
||||||
build: .
|
build: .
|
||||||
ports:
|
|
||||||
- "8080:8080"
|
|
||||||
environment:
|
environment:
|
||||||
- DATABASE_URL=postgresql://postgres:${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env}@postgres:5432/messenger
|
- DATABASE_URL=postgresql://postgres:${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env}@postgres:5432/messenger
|
||||||
- REDIS_URL=redis://redis:6379/0
|
- REDIS_URL=redis://redis:6379/0
|
||||||
- SESSION_TTL_SECONDS=3600
|
- SESSION_TTL_SECONDS=3600
|
||||||
|
- INSTANCE_ID=api-replica-1
|
||||||
depends_on:
|
depends_on:
|
||||||
postgres:
|
postgres:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
redis:
|
redis:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
|
migrate:
|
||||||
|
condition: service_completed_successfully
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8080/health/ready')"]
|
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8080/health/ready')"]
|
||||||
interval: 5s
|
interval: 5s
|
||||||
timeout: 3s
|
timeout: 3s
|
||||||
retries: 3
|
retries: 3
|
||||||
|
|
||||||
|
api-2:
|
||||||
|
build: .
|
||||||
|
environment:
|
||||||
|
- DATABASE_URL=postgresql://postgres:${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env}@postgres:5432/messenger
|
||||||
|
- REDIS_URL=redis://redis:6379/0
|
||||||
|
- SESSION_TTL_SECONDS=3600
|
||||||
|
- INSTANCE_ID=api-replica-2
|
||||||
|
depends_on:
|
||||||
|
postgres:
|
||||||
|
condition: service_healthy
|
||||||
|
redis:
|
||||||
|
condition: service_healthy
|
||||||
|
migrate:
|
||||||
|
condition: service_completed_successfully
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8080/health/ready')"]
|
||||||
|
interval: 5s
|
||||||
|
timeout: 3s
|
||||||
|
retries: 3
|
||||||
|
|
||||||
|
nginx:
|
||||||
|
image: nginx:1.25.4-alpine
|
||||||
|
ports:
|
||||||
|
- "8080:8080"
|
||||||
|
volumes:
|
||||||
|
- ./nginx.conf:/etc/nginx/nginx.conf:ro
|
||||||
|
depends_on:
|
||||||
|
- api-1
|
||||||
|
- api-2
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "nginx", "-t"]
|
||||||
|
interval: 5s
|
||||||
|
timeout: 3s
|
||||||
|
retries: 3
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
pgdata:
|
pgdata:
|
||||||
redisdata:
|
redisdata:
|
||||||
+13
-1
@@ -26,4 +26,16 @@ CREATE TABLE IF NOT EXISTS messages (
|
|||||||
created_at TIMESTAMPTZ NOT NULL
|
created_at TIMESTAMPTZ NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_messages_chat_pagination ON messages(chat_id, created_at, id);
|
CREATE INDEX IF NOT EXISTS idx_messages_chat_pagination ON messages(chat_id, created_at, id);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS idempotency_keys (
|
||||||
|
user_id UUID NOT NULL,
|
||||||
|
method VARCHAR(16) NOT NULL,
|
||||||
|
path TEXT NOT NULL,
|
||||||
|
key VARCHAR(128) NOT NULL,
|
||||||
|
request_hash VARCHAR(64) NOT NULL,
|
||||||
|
response_status INT NOT NULL,
|
||||||
|
response_body JSONB NOT NULL,
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
PRIMARY KEY (user_id, method, path, key)
|
||||||
|
);
|
||||||
+19
@@ -0,0 +1,19 @@
|
|||||||
|
events { worker_connections 1024; }
|
||||||
|
|
||||||
|
http {
|
||||||
|
upstream messenger_api {
|
||||||
|
server api-1:8080;
|
||||||
|
server api-2:8080;
|
||||||
|
}
|
||||||
|
|
||||||
|
server {
|
||||||
|
listen 8080;
|
||||||
|
|
||||||
|
location / {
|
||||||
|
proxy_pass http://messenger_api;
|
||||||
|
proxy_set_header Host $host;
|
||||||
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -eu
|
||||||
|
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
|
||||||
|
|
||||||
|
if [ "$TARGET_INSTANCE_ID" = "api-replica-1" ]; then
|
||||||
|
docker compose stop -t 3 api-1
|
||||||
|
elif [ "$TARGET_INSTANCE_ID" = "api-replica-2" ]; then
|
||||||
|
docker compose stop -t 3 api-2
|
||||||
|
else
|
||||||
|
echo "Unknown target instance: $TARGET_INSTANCE_ID" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
+1
-1
@@ -14,7 +14,7 @@ import uuid
|
|||||||
|
|
||||||
ROOT = Path(__file__).resolve().parents[1]
|
ROOT = Path(__file__).resolve().parents[1]
|
||||||
LAB = json.loads((ROOT / 'lab.json').read_text())['number']
|
LAB = json.loads((ROOT / 'lab.json').read_text())['number']
|
||||||
SPEC = json.loads((ROOT / 'contracts/openapi.json').read_text())
|
SPEC = json.loads((ROOT / 'contracts/openapi.json').read_text(encoding='utf-8'))
|
||||||
|
|
||||||
class Failure(AssertionError):
|
class Failure(AssertionError):
|
||||||
pass
|
pass
|
||||||
|
|||||||
+1
-1
@@ -55,7 +55,7 @@ def main():
|
|||||||
check(len(observed)>=2,'less than two API instances observed before failure; verify LB/session affinity')
|
check(len(observed)>=2,'less than two API instances observed before failure; verify LB/session affinity')
|
||||||
print('Executing explicit '+args.mode+' hook; hook output suppressed to avoid leaking secrets.')
|
print('Executing explicit '+args.mode+' hook; hook output suppressed to avoid leaking secrets.')
|
||||||
# No shell interpolation and no Docker commands in this test.
|
# No shell interpolation and no Docker commands in this test.
|
||||||
proc = subprocess.Popen([str(script)],env=env,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
|
proc = subprocess.Popen(['sh', str(script)], env=env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||||
started = time.monotonic()
|
started = time.monotonic()
|
||||||
successes,failures,after_instances = [],0,[]
|
successes,failures,after_instances = [],0,[]
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user