Files
mtusi2026lab3/app/routes.py
T
dawudukandCursor c0d313703e Перенести API с общими сессиями и идемпотентной отправкой сообщения.
Реплика больше не держит корректность в своей памяти: сессия в Redis, а повтор POST сообщения фиксируется в Postgres вместе с X-Instance-Id.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-07 12:46:37 +03:00

285 lines
11 KiB
Python

import base64
import binascii
from starlette.requests import Request
from starlette.responses import JSONResponse, Response
from app import cache, config, db, rediscli, sessions, store
from app.cursor_codec import decode_cursor, encode_cursor
from app.errors import BASIC_CHALLENGE, BEARER_CHALLENGE, AppError, error_response
from app.passwords import dummy_hash, hash_password, verify_password
from app.validation import (
parse_json_body,
require_exact_keys,
validate_display_name,
validate_limit,
validate_idempotency_key,
validate_message_text,
validate_password,
validate_title,
validate_username,
validate_uuid,
)
def _json(data: dict, request_id: str, status: int = 200) -> JSONResponse:
return JSONResponse(status_code=status, headers={"X-Request-Id": request_id}, content=data)
def _empty(request_id: str) -> Response:
return Response(status_code=204, headers={"X-Request-Id": request_id})
async def _bearer_user(request: Request) -> dict:
header = request.headers.get("Authorization") or ""
scheme, _, rest = header.partition(" ")
if scheme.lower() != "bearer" or not rest.strip():
raise AppError(401, "authentication required", BEARER_CHALLENGE)
user_id = str(await sessions.resolve(rest.strip()))
user = await store.get_user(user_id)
if user is None:
raise AppError(401, "authentication required", BEARER_CHALLENGE)
return user
async def health_live(request: Request) -> Response:
return _json({"status": "ok"}, request.state.request_id)
async def health_ready(request: Request) -> Response:
rid = request.state.request_id
if request.app.state.shutting_down:
return error_response(503, "service is shutting down", rid)
await db.ping()
await rediscli.ping()
return _json({"status": "ok"}, rid)
async def register_user(request: Request) -> Response:
rid = request.state.request_id
if request.headers.get("content-type", "").split(";")[0].strip().lower() not in ("application/json", ""):
if request.headers.get("content-type"):
return error_response(415, "unsupported media type", rid)
raw = await request.body()
body, err = parse_json_body(raw)
if err:
return error_response(400, err, rid)
assert body is not None
if require_exact_keys(body, {"username", "display_name", "password"}, {"username", "display_name", "password"}):
return error_response(400, "invalid user payload", rid)
if validate_username(body["username"]) or validate_display_name(body["display_name"]) or validate_password(body["password"]):
return error_response(400, "invalid user fields", rid)
user, conflict = await store.create_user(body["username"], body["display_name"], hash_password(body["password"]))
if conflict:
return error_response(409, "username already taken", rid)
assert user is not None
return _json(user, rid, 201)
async def current_user(request: Request) -> Response:
user = await _bearer_user(request)
return _json(user, request.state.request_id)
async def create_session(request: Request) -> Response:
rid = request.state.request_id
header = request.headers.get("Authorization") or ""
scheme, _, rest = header.partition(" ")
if scheme.lower() != "basic" or not rest.strip():
return error_response(401, "authentication required", rid, {"WWW-Authenticate": BASIC_CHALLENGE})
try:
decoded = base64.b64decode(rest.strip(), validate=True).decode("utf-8")
except (binascii.Error, UnicodeError, ValueError):
return error_response(401, "authentication required", rid, {"WWW-Authenticate": BASIC_CHALLENGE})
username, sep, password = decoded.partition(":")
if not sep:
return error_response(401, "authentication required", rid, {"WWW-Authenticate": BASIC_CHALLENGE})
row = await store.get_user_by_username(username)
password_hash = row["password_hash"] if row else dummy_hash()
if row is None or not verify_password(password_hash, password):
return error_response(401, "authentication required", rid, {"WWW-Authenticate": BASIC_CHALLENGE})
token, expires_at = await sessions.create(row["id"])
return _json(
{
"token": token,
"token_type": "Bearer",
"expires_at": expires_at,
"user": store.user_dict(row),
},
rid,
201,
)
async def revoke_session(request: Request) -> Response:
header = request.headers.get("Authorization") or ""
scheme, _, rest = header.partition(" ")
if scheme.lower() != "bearer" or not rest.strip():
raise AppError(401, "authentication required", BEARER_CHALLENGE)
await sessions.revoke(rest.strip())
return _empty(request.state.request_id)
async def create_chat(request: Request) -> Response:
rid = request.state.request_id
user = await _bearer_user(request)
ct = request.headers.get("content-type", "").split(";")[0].strip().lower()
if ct and ct != "application/json":
return error_response(415, "unsupported media type", rid)
raw = await request.body()
body, err = parse_json_body(raw)
if err:
return error_response(400, err, rid)
assert body is not None
if require_exact_keys(body, {"title", "member_ids"}, {"title", "member_ids"}):
return error_response(400, "invalid chat payload", rid)
if validate_title(body["title"]):
return error_response(400, "invalid title", rid)
members = body["member_ids"]
if not isinstance(members, list) or not members:
return error_response(400, "invalid member_ids", rid)
if len(members) > 100:
return error_response(400, "too many members", rid)
for mid in members:
if validate_uuid(mid):
return error_response(400, "invalid member id", rid)
chat, create_err = await store.create_chat(body["title"], members, user["id"])
if create_err == "caller not in members":
return error_response(400, "caller must be a member", rid)
if create_err in ("duplicate members", "unknown member"):
return error_response(400, "invalid members", rid)
assert chat is not None
return _json(chat, rid, 201)
async def list_chats(request: Request) -> Response:
rid = request.state.request_id
user = await _bearer_user(request)
limit, limit_err = validate_limit(request.query_params.get("limit"))
if limit_err:
return error_response(400, limit_err, rid)
assert limit is not None
cursor_raw = request.query_params.get("cursor")
after = None
if cursor_raw is not None:
if not cursor_raw.strip():
return error_response(400, "invalid cursor", rid)
try:
created_at, entity_id = decode_cursor(cursor_raw, user["id"])
except ValueError:
return error_response(400, "invalid cursor", rid)
after = (created_at, entity_id)
items, next_key = await store.list_chats(user["id"], limit, after)
next_cursor = None
if next_key is not None:
next_cursor = encode_cursor(user["id"], next_key[0], next_key[1])
return _json({"items": items, "next_cursor": next_cursor}, rid)
async def get_chat(request: Request) -> Response:
rid = request.state.request_id
user = await _bearer_user(request)
chat_id = request.path_params["chat_id"]
if validate_uuid(chat_id):
return error_response(400, "invalid chat id", rid)
chat = await store.get_chat(chat_id, user["id"])
if chat is None:
return error_response(404, "chat not found", rid)
return _json(chat, rid)
async def create_message(request: Request) -> Response:
rid = request.state.request_id
user = await _bearer_user(request)
chat_id = request.path_params["chat_id"]
if validate_uuid(chat_id):
return error_response(400, "invalid chat id", rid)
ct = request.headers.get("content-type", "").split(";")[0].strip().lower()
if ct and ct != "application/json":
return error_response(415, "unsupported media type", rid)
raw = await request.body()
body, err = parse_json_body(raw)
if err:
return error_response(400, err, rid)
assert body is not None
if require_exact_keys(body, {"text"}, {"text"}):
return error_response(400, "invalid message payload", rid)
if validate_message_text(body["text"]):
return error_response(400, "invalid message text", rid)
if validate_idempotency_key(request.headers.get("Idempotency-Key")):
return error_response(400, "invalid idempotency key", rid)
message, msg_err = await store.add_message(
chat_id,
user["id"],
body["text"],
request.headers.get("Idempotency-Key") or "",
request.url.path,
)
if msg_err == "not_found":
return error_response(404, "chat not found", rid)
if msg_err == "conflict":
return error_response(409, "idempotency key reused with a different body", rid)
assert message is not None
return _json(message, rid, 201)
async def list_messages(request: Request) -> Response:
rid = request.state.request_id
user = await _bearer_user(request)
chat_id = request.path_params["chat_id"]
if validate_uuid(chat_id):
return error_response(400, "invalid chat id", rid)
limit, limit_err = validate_limit(request.query_params.get("limit"))
if limit_err:
return error_response(400, limit_err, rid)
assert limit is not None
cursor_raw = request.query_params.get("cursor")
after = None
if cursor_raw is not None:
if not cursor_raw.strip():
return error_response(400, "invalid cursor", rid)
try:
created_at, entity_id = decode_cursor(cursor_raw, chat_id)
except ValueError:
return error_response(400, "invalid cursor", rid)
after = (created_at, entity_id)
page, next_key, list_err = await store.list_messages(chat_id, user["id"], limit, after)
if list_err == "not_found":
return error_response(404, "chat not found", rid)
assert page is not None
next_cursor = None
if next_key is not None:
next_cursor = encode_cursor(chat_id, next_key[0], next_key[1])
return _json({"items": page, "next_cursor": next_cursor}, rid)
async def lab_metrics(request: Request) -> Response:
if not config.lab_mode():
return error_response(404, "not found", request.state.request_id)
return _json(
{
"singleflight": await cache.singleflight_enabled(),
"invalidate": await cache.invalidate_enabled(),
"history_sql": await cache.history_sql_count(),
},
request.state.request_id,
)
async def lab_flags(request: Request) -> Response:
if not config.lab_mode():
return error_response(404, "not found", request.state.request_id)
raw = await request.body()
body, err = parse_json_body(raw)
if err:
return error_response(400, err, request.state.request_id)
assert body is not None
if body.get("reset_sql"):
await cache.reset_history_sql_count()
flags = await cache.set_flags(
singleflight=body.get("singleflight"),
invalidate=body.get("invalidate"),
delay_ms=body.get("delay_ms"),
)
return _json(flags, request.state.request_id)