+ Приведены все функции приложения в рабочий вид

+ Наведен порядок в файлах проекта
+ Наведен порядок в документации
+ Настроены скрипты установки, развертки и так далее, расширен MakeFile
This commit is contained in:
2025-11-02 06:09:55 +09:00
parent 367e1c932e
commit 2e535513b5
6103 changed files with 7040 additions and 1027861 deletions

39
scripts/create_superuser.sh Executable file
View File

@@ -0,0 +1,39 @@
#!/usr/bin/env bash
set -euo pipefail
# scripts/create_superuser.sh
# Usage: ./scripts/create_superuser.sh
# This script reads ADMIN_USERNAME, ADMIN_EMAIL, ADMIN_PASSWORD from .env (or environment)
# and creates the superuser inside the Django web container.
BASE_DIR="$(cd "$(dirname "$0")/.." && pwd)"
if [[ -f "$BASE_DIR/.env" ]]; then
export $(grep -v '^#' $BASE_DIR/.env | xargs)
fi
: ${ADMIN_USERNAME:="admin"}
: ${ADMIN_EMAIL:="admin@example.com"}
: ${ADMIN_PASSWORD:="changeme"}
echo "Creating superuser: $ADMIN_USERNAME"
docker-compose exec -T web python manage.py shell <<PY
from django.contrib.auth import get_user_model
User = get_user_model()
username = '${ADMIN_USERNAME}'
email = '${ADMIN_EMAIL}'
password = '${ADMIN_PASSWORD}'
if not User.objects.filter(username=username).exists():
User.objects.create_superuser(username=username, email=email, password=password)
print('Superuser created')
else:
u = User.objects.filter(username=username).first()
u.email = email
u.is_superuser = True
u.is_staff = True
u.set_password(password)
u.save()
print('Superuser updated')
PY
echo "Done. You can log in at http://localhost:8000/admin with the credentials above (if backend is running)."

70
scripts/generate_env.sh Executable file
View File

@@ -0,0 +1,70 @@
#!/usr/bin/env bash
set -euo pipefail
# scripts/generate_env.sh
# Interactive generator for .env from .env.example
# Usage: ./scripts/generate_env.sh [--yes]
BASE_DIR="$(cd "$(dirname "$0")/.." && pwd)"
EXAMPLE="$BASE_DIR/.env.example"
TARGET="$BASE_DIR/.env"
confirm=false
if [[ "${1:-}" == "--yes" ]]; then
confirm=true
fi
if [[ ! -f "$EXAMPLE" ]]; then
echo "Error: .env.example not found at $EXAMPLE"
exit 1
fi
echo "Generating $TARGET from $EXAMPLE"
if [[ -f "$TARGET" && "$confirm" = false ]]; then
read -p "$TARGET exists. Overwrite? (y/N): " ans
case "$ans" in
[Yy]*) ;;
*) echo "Aborted"; exit 0;;
esac
fi
# Copy example first
cp "$EXAMPLE" "$TARGET"
# For each variable in example, prompt the user to keep or change
while IFS= read -r line; do
if [[ "$line" =~ ^# ]] || [[ -z "$line" ]]; then
continue
fi
key=$(echo "$line" | cut -d= -f1)
val=$(echo "$line" | cut -d= -f2-)
# Trim
key=$(echo "$key" | xargs)
val=$(echo "$val" | xargs)
current=$(grep -E "^$key=" "$TARGET" || true)
current_val=""
if [[ -n "$current" ]]; then
current_val=$(echo "$current" | cut -d= -f2-)
fi
if [[ "$confirm" = true ]]; then
# non-interactive: keep example defaults or environment overrides
if [[ -n "${!key-}" ]]; then
new_val="${!key}"
sed -i "s|^$key=.*|$key=$new_val|" "$TARGET"
fi
continue
fi
read -p "$key [$current_val]: " new_val
if [[ -n "$new_val" ]]; then
sed -i "s|^$key=.*|$key=$new_val|" "$TARGET"
fi
done < <(grep -E '^[A-Z0-9_]+=.*' "$EXAMPLE")
echo "Written $TARGET"
echo "You can re-run this script with --yes to auto-fill from environment variables."

16
scripts/migrate_and_collect.sh Executable file
View File

@@ -0,0 +1,16 @@
#!/usr/bin/env bash
set -euo pipefail
# scripts/migrate_and_collect.sh
# run makemigrations, migrate and collectstatic inside web container
echo "Running makemigrations..."
docker-compose exec web python manage.py makemigrations || true
echo "Running migrate..."
docker-compose exec web python manage.py migrate
echo "Collecting static files..."
docker-compose exec web python manage.py collectstatic --noinput
echo "Done"

13
scripts/rebuild_no_cache.sh Executable file
View File

@@ -0,0 +1,13 @@
#!/usr/bin/env bash
set -euo pipefail
# scripts/rebuild_no_cache.sh
# Build and restart containers without cache
echo "Building containers without cache..."
docker compose build --no-cache
echo "Bringing up containers..."
docker compose up -d
echo "Containers are up"

71
scripts/ssl_setup.sh Executable file
View File

@@ -0,0 +1,71 @@
#!/usr/bin/env bash
set -euo pipefail
# scripts/ssl_setup.sh
# Host-side script that helps setup nginx reverse proxy and obtain Let's Encrypt certificates.
# IMPORTANT: run this script on the host machine (not inside containers) with sudo.
# Usage: sudo ./scripts/ssl_setup.sh domain example.com email you@example.com
if [[ $(id -u) -ne 0 ]]; then
echo "This script must be run as root (sudo)."
exit 2
fi
DOMAIN="$1"
EMAIL="$2"
if [[ -z "$DOMAIN" || -z "$EMAIL" ]]; then
echo "Usage: sudo ./scripts/ssl_setup.sh <domain> <email>"
exit 1
fi
echo "Installing nginx and certbot (Debian/Ubuntu)..."
apt-get update
apt-get install -y nginx certbot python3-certbot-nginx
echo "Creating nginx config for reverse proxy..."
NGINX_CONF="/etc/nginx/sites-available/links.conf"
cat > "$NGINX_CONF" <<'EOF'
server {
listen 80;
server_name REPLACE_DOMAIN;
location / {
proxy_pass http://127.0.0.1:3000; # frontend
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /api/ {
proxy_pass http://127.0.0.1:8000; # backend
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
}
EOF
# Replace domain
sed -i "s|REPLACE_DOMAIN|$DOMAIN|g" "$NGINX_CONF"
# Enable
ln -sf "$NGINX_CONF" /etc/nginx/sites-enabled/links.conf
mkdir -p /var/www/certbot
echo "Testing nginx config and reloading..."
nginx -t
systemctl reload nginx
echo "Obtaining Let's Encrypt certificate for $DOMAIN..."
certbot --nginx -d "$DOMAIN" --non-interactive --agree-tos --email "$EMAIL" || {
echo "certbot failed; you can retry manually: certbot --nginx -d $DOMAIN"
}
echo "SSL setup complete. Nginx should be proxying to frontend/backend on ports 3000/8000"