feat(config): 添加每日记忆整理配置和域名配置交互功能
Test start.ps1 (Windows) / test-windows (push) Has been cancelled

- 在 .env.AMS.sample 和 .env.sample 中新增记忆整理相关配置项
- 实现了 https.sh 脚本中的域名配置交互与文件生成功能
- 支持配置文件中域名的替换和新增操作
- 在 README.md 中补充了一键启动脚本使用说明和 HTTPS 配置指南
- 添加了 docker-compose.yml 单独更新命令说明
This commit is contained in:
2026-04-02 17:45:29 +08:00
parent c0ee3bee52
commit f33b0850fe
4 changed files with 272 additions and 5 deletions
+155 -2
View File
@@ -103,14 +103,167 @@ else
exit 1
fi
# ===== 域名配置交互与文件生成 =====
prompt_domain() {
local domain
while true; do
read -r -p "🌐 请输入域名(例如 demo.example.com: " domain
domain=$(echo "$domain" | xargs)
if [ -n "$domain" ] && [[ "$domain" =~ ^[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?)+$ ]]; then
echo "$domain"
return 0
fi
echo "❌ 域名格式不正确,请重新输入。"
done
}
escape_sed() {
echo "$1" | sed 's/[.[\*^$()+?{}|/]/\\&/g'
}
get_primary_domain_from_conf() {
local conf_file=$1
awk '
/server_name/ {
for (i = 2; i <= NF; i++) {
gsub(";", "", $i)
if ($i !~ /^www\./ && $i !~ /^\*/ && $i != "localhost") {
print $i
exit
}
}
}
' "$conf_file"
}
create_conf_by_template() {
local domain=$1
local target_file=$2
if [ -f "./demo.51easyai.com.conf" ]; then
cp "./demo.51easyai.com.conf" "./$target_file"
sed -i.bak "s/www\.demo\.51easyai\.com/www.$domain/g; s/demo\.51easyai\.com/$domain/g" "./$target_file"
rm -f "./$target_file.bak"
return 0
fi
if [ -f "./2.conf" ]; then
cp "./2.conf" "./$target_file"
sed -i.bak "s/www\.2/www.$domain/g; s/\b2\b/$domain/g" "./$target_file"
rm -f "./$target_file.bak"
return 0
fi
cat > "./$target_file" <<EOF
map \$http_upgrade \$connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
listen [::]:80;
server_name www.$domain;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
try_files \$uri =404;
}
location / {
return 301 https://$domain\$request_uri;
}
}
server {
listen 80;
listen [::]:80;
server_name $domain;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
try_files \$uri =404;
}
location / {
proxy_pass http://127.0.0.1:3010/;
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;
proxy_set_header Host \$host;
}
}
EOF
}
upsert_domain_for_conf() {
local conf_file=$1
local mode
local new_domain
echo "️ 检测到当前目录已有配置文件: $conf_file"
while true; do
echo "请选择域名处理方式:"
echo " 1) 替换当前域名"
echo " 2) 新增一个域名"
read -r -p "请输入选项 (1/2): " mode
case "$mode" in
1|2) break ;;
*) echo "❌ 无效选项,请输入 1 或 2" ;;
esac
done
new_domain=$(prompt_domain)
if [ "$mode" = "1" ]; then
local old_domain
old_domain=$(get_primary_domain_from_conf "./$conf_file")
if [ -z "$old_domain" ]; then
echo "⚠️ 未能自动识别旧域名,将直接尝试新增域名。"
mode="2"
else
local old_domain_escaped
local new_domain_escaped
old_domain_escaped=$(escape_sed "$old_domain")
new_domain_escaped=$(escape_sed "$new_domain")
sed -i.bak "s/www\\.$old_domain_escaped/www.$new_domain_escaped/g; s/$old_domain_escaped/$new_domain_escaped/g" "./$conf_file"
rm -f "./$conf_file.bak"
echo "✅ 已将域名从 $old_domain 替换为 $new_domain"
fi
fi
if [ "$mode" = "2" ]; then
local changed=0
if ! grep -Eq "server_name[^;]*[[:space:]]$new_domain([[:space:];]|$)" "./$conf_file"; then
sed -i.bak "/server_name/s/;/ $new_domain;/g" "./$conf_file"
changed=1
fi
if ! grep -Eq "server_name[^;]*[[:space:]]www\\.$new_domain([[:space:];]|$)" "./$conf_file"; then
sed -i.bak "/server_name/s/;/ www.$new_domain;/g" "./$conf_file"
changed=1
fi
rm -f "./$conf_file.bak"
if [ "$changed" -eq 1 ]; then
echo "✅ 已新增域名:$new_domain(含 www.$new_domain"
else
echo "ℹ️ 配置文件已包含该域名,无需新增。"
fi
fi
}
echo "🚀 复制当前目录的配置文件到nginx配置文件目录"
# 支持 EASYAI_PROXY_CONF 指定配置文件(如 51easyai.com.conf
CONF_FILE="${EASYAI_PROXY_CONF:-easyai-proxy.conf}"
if [ -f "./$CONF_FILE" ]; then
cp "./$CONF_FILE" "/etc/nginx/conf.d/$CONF_FILE"
upsert_domain_for_conf "$CONF_FILE"
else
cp -r ./easyai-proxy.conf /etc/nginx/conf.d/ 2>/dev/null || { echo "❌ 未找到 nginx 配置文件"; exit 1; }
echo "️ 当前目录未找到配置文件 ./$CONF_FILE"
input_domain=$(prompt_domain)
create_conf_by_template "$input_domain" "$CONF_FILE"
echo "✅ 已根据域名 $input_domain 创建配置文件: ./$CONF_FILE"
fi
cp "./$CONF_FILE" "/etc/nginx/conf.d/$CONF_FILE"
echo "🚀 重载nginx"
sudo nginx -s reload