Хранить вложения в приватном S3 и сверять их с PostgreSQL.

This commit is contained in:
2026-11-06 20:13:44 +03:00
parent ec540a1a72
commit a6bb7cbb8b
34 changed files with 3285 additions and 31 deletions
+54
View File
@@ -0,0 +1,54 @@
#!/bin/sh
# Idempotent private bucket and least-privilege API user. Root credentials stay in this init container.
set -eu
: "${MINIO_ROOT_USER:?}"
: "${MINIO_ROOT_PASSWORD:?}"
: "${S3_ACCESS_KEY_ID:?}"
: "${S3_SECRET_ACCESS_KEY:?}"
: "${S3_BUCKET:?}"
export MC_CONFIG_DIR=/tmp/mc
export MC_HOST_local="http://${MINIO_ROOT_USER}:${MINIO_ROOT_PASSWORD}@minio:9000"
i=0
until mc mb --ignore-existing "local/${S3_BUCKET}" >/dev/null 2>&1; do
i=$((i + 1))
if [ "$i" -gt 40 ]; then
echo "bucket bootstrap failed" >&2
exit 1
fi
sleep 1
done
mc anonymous set none "local/${S3_BUCKET}" >/dev/null
cat > /tmp/messenger-api-policy.json <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
"Resource": ["arn:aws:s3:::${S3_BUCKET}/*"]
},
{
"Effect": "Allow",
"Action": ["s3:ListBucket", "s3:GetBucketLocation"],
"Resource": ["arn:aws:s3:::${S3_BUCKET}"]
}
]
}
EOF
if ! mc admin policy info local messenger-api >/dev/null 2>&1; then
mc admin policy create local messenger-api /tmp/messenger-api-policy.json >/dev/null
fi
if ! mc admin user info local "${S3_ACCESS_KEY_ID}" >/dev/null 2>&1; then
mc admin user add local "${S3_ACCESS_KEY_ID}" "${S3_SECRET_ACCESS_KEY}" >/dev/null
fi
mc admin policy attach local messenger-api --user "${S3_ACCESS_KEY_ID}" >/dev/null 2>&1 || true
echo "bucket bootstrap ok"
+22
View File
@@ -0,0 +1,22 @@
global
maxconn 256
defaults
mode http
timeout connect 2s
timeout client 120s
timeout server 120s
option http-server-close
frontend messenger
bind *:8080
default_backend api
backend api
balance roundrobin
option httpchk
http-check send meth GET uri /health/live
http-check expect status 200
default-server inter 1s fall 2 rise 1
server api-1 api-1:8080 check
server api-2 api-2:8080 check
+92
View File
@@ -0,0 +1,92 @@
CREATE TABLE IF NOT EXISTS users (
id uuid PRIMARY KEY,
username text NOT NULL UNIQUE,
display_name text NOT NULL,
password_hash text NOT NULL,
created_at timestamptz NOT NULL
);
CREATE TABLE IF NOT EXISTS sessions (
token_hash text PRIMARY KEY,
user_id uuid NOT NULL REFERENCES users (id),
expires_at timestamptz NOT NULL,
revoked_at timestamptz
);
CREATE INDEX IF NOT EXISTS sessions_user_id_idx ON sessions (user_id);
CREATE TABLE IF NOT EXISTS chats (
id uuid PRIMARY KEY,
title text NOT NULL,
created_at timestamptz NOT NULL
);
CREATE TABLE IF NOT EXISTS chat_members (
chat_id uuid NOT NULL REFERENCES chats (id),
user_id uuid NOT NULL REFERENCES users (id),
position integer NOT NULL,
PRIMARY KEY (chat_id, user_id)
);
CREATE INDEX IF NOT EXISTS chat_members_user_idx ON chat_members (user_id, chat_id);
CREATE TABLE IF NOT EXISTS attachments (
id uuid PRIMARY KEY,
chat_id uuid NOT NULL REFERENCES chats (id),
owner_id uuid NOT NULL REFERENCES users (id),
filename text NOT NULL,
content_type text NOT NULL,
size_bytes integer NOT NULL CHECK (size_bytes > 0 AND size_bytes <= 10485760),
sha256 text NOT NULL,
status text NOT NULL CHECK (status = 'ready'),
object_key text NOT NULL UNIQUE,
error_code text,
created_at timestamptz NOT NULL,
updated_at timestamptz NOT NULL
);
CREATE INDEX IF NOT EXISTS attachments_chat_idx ON attachments (chat_id, created_at);
CREATE TABLE IF NOT EXISTS messages (
id uuid PRIMARY KEY,
chat_id uuid NOT NULL REFERENCES chats (id),
sender_id uuid NOT NULL REFERENCES users (id),
text text NOT NULL,
created_at timestamptz NOT NULL
);
CREATE INDEX IF NOT EXISTS messages_chat_order_idx ON messages (chat_id, created_at, id);
CREATE TABLE IF NOT EXISTS message_attachments (
message_id uuid NOT NULL REFERENCES messages (id),
attachment_id uuid NOT NULL REFERENCES attachments (id),
position integer NOT NULL,
PRIMARY KEY (message_id, position)
);
CREATE INDEX IF NOT EXISTS message_attachments_attachment_idx ON message_attachments (attachment_id);
CREATE TABLE IF NOT EXISTS idempotency (
user_id uuid NOT NULL REFERENCES users (id),
http_method text NOT NULL,
path text NOT NULL,
idem_key text NOT NULL,
request_hash text NOT NULL,
status_code integer NOT NULL,
response_json jsonb NOT NULL,
created_at timestamptz NOT NULL,
PRIMARY KEY (user_id, http_method, path, idem_key)
);
CREATE TABLE IF NOT EXISTS object_ledger (
object_key text PRIMARY KEY,
attachment_id uuid,
chat_id uuid,
state text NOT NULL CHECK (state IN ('staging', 'committed', 'aborted', 'purged')),
note text,
size_bytes bigint,
created_at timestamptz NOT NULL,
updated_at timestamptz NOT NULL
);
CREATE INDEX IF NOT EXISTS object_ledger_state_idx ON object_ledger (state, created_at);
+15
View File
@@ -0,0 +1,15 @@
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
"Resource": ["arn:aws:s3:::messenger-attachments/*"]
},
{
"Effect": "Allow",
"Action": ["s3:ListBucket", "s3:GetBucketLocation"],
"Resource": ["arn:aws:s3:::messenger-attachments"]
}
]
}