forked from mtusi2026/mtusi2026lab5
Add 4 lab code
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
worker_processes 1;
|
||||
worker_shutdown_timeout 8s;
|
||||
events { worker_connections 1024; }
|
||||
http {
|
||||
upstream messenger {
|
||||
zone messenger 64k;
|
||||
include /etc/nginx/ready-backends.conf;
|
||||
}
|
||||
log_format upstream '$request_method $uri $status upstream=$upstream_addr '
|
||||
'upstream_status=$upstream_status time=$request_time';
|
||||
access_log /var/log/nginx/access.log upstream;
|
||||
server {
|
||||
listen 8080;
|
||||
# API enforces the byte limit and returns the contracted JSON error.
|
||||
client_max_body_size 0;
|
||||
client_body_timeout 5s;
|
||||
client_header_timeout 5s;
|
||||
keepalive_timeout 5s;
|
||||
location / {
|
||||
proxy_pass http://messenger;
|
||||
proxy_http_version 1.1;
|
||||
proxy_request_buffering off;
|
||||
proxy_buffering off;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header Connection close;
|
||||
proxy_connect_timeout 1s;
|
||||
proxy_send_timeout 35s;
|
||||
proxy_read_timeout 120s;
|
||||
# A 503 is an API response (for example, S3 is unavailable), so
|
||||
# preserve it for the client instead of marking every replica down.
|
||||
proxy_next_upstream error timeout http_502 http_504;
|
||||
proxy_next_upstream_tries 2;
|
||||
proxy_next_upstream_timeout 8s;
|
||||
# Never transparently retry a POST already sent to an API.
|
||||
# A client retries messages using the original Idempotency-Key.
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
#!/bin/sh
|
||||
# Only ready API addresses enter the upstream, including at nginx startup.
|
||||
update_backends() {
|
||||
next=/etc/nginx/ready-backends.conf.next
|
||||
: > "$next"
|
||||
for address in $(getent ahostsv4 api | awk '{print $1}' | sort -u); do
|
||||
if wget -q -T 1 -O /dev/null "http://$address:8080/health/ready"; then
|
||||
echo "server $address:8080;" >> "$next"
|
||||
fi
|
||||
done
|
||||
# If every running API temporarily loses a shared dependency (for example
|
||||
# S3), retain the last addresses. The API can then return the contracted
|
||||
# 503 instead of Nginx replacing it with a 502. At initial startup there
|
||||
# is no previous healthy set, so use an inert placeholder.
|
||||
if [ ! -s "$next" ]; then
|
||||
if [ -s /etc/nginx/ready-backends.conf ] && ! grep -q '127.0.0.1:9 down' /etc/nginx/ready-backends.conf; then
|
||||
rm -f "$next"
|
||||
return
|
||||
fi
|
||||
echo 'server 127.0.0.1:9 down;' > "$next"
|
||||
fi
|
||||
if ! cmp -s "$next" /etc/nginx/ready-backends.conf; then
|
||||
mv "$next" /etc/nginx/ready-backends.conf
|
||||
if [ "${1:-}" = reload ] && [ -f /var/run/nginx.pid ]; then
|
||||
nginx -s reload
|
||||
fi
|
||||
else
|
||||
rm -f "$next"
|
||||
fi
|
||||
}
|
||||
|
||||
update_backends
|
||||
(
|
||||
while sleep 2; do
|
||||
update_backends reload
|
||||
done
|
||||
) &
|
||||
exec nginx -g 'daemon off;'
|
||||
@@ -0,0 +1,9 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
test "$MINIO_ROOT_USER" != "$S3_ACCESS_KEY_ID"
|
||||
mc alias set local http://s3:9000 "$MINIO_ROOT_USER" "$MINIO_ROOT_PASSWORD" >/dev/null
|
||||
mc mb --ignore-existing local/messenger-attachments
|
||||
mc anonymous set none local/messenger-attachments
|
||||
mc admin user add local "$S3_ACCESS_KEY_ID" "$S3_SECRET_ACCESS_KEY" >/dev/null
|
||||
mc admin policy info local attachments >/dev/null 2>&1 || mc admin policy create local attachments /policy.json
|
||||
mc admin policy attach local attachments --user "$S3_ACCESS_KEY_ID"
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"Version": "2012-10-17",
|
||||
"Statement": [
|
||||
{
|
||||
"Effect": "Allow",
|
||||
"Action": ["s3:ListBucket", "s3:GetBucketLocation"],
|
||||
"Resource": ["arn:aws:s3:::messenger-attachments"]
|
||||
},
|
||||
{
|
||||
"Effect": "Allow",
|
||||
"Action": ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
|
||||
"Resource": ["arn:aws:s3:::messenger-attachments/originals/*"]
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user