Skip to content

Commit d849024

Browse files
committed
feat(docker): 添加基本认证功能和环境变量配置
- 在 Dockerfile 中添加 htpasswd 工具和 dos2unix - 更新 docker-compose.yml 文件以支持 ACCESS_USERNAME 和 ACCESS_PASSWORD 环境变量 - 修改 dev.md 和 README 文件,增加访问密码配置说明 - 更新 Nginx 配置以引入基本认证 - 将版本号更新至 1.0.6
1 parent a9b1108 commit d849024

12 files changed

+136
-146
lines changed

Dockerfile

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,25 @@ RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
1010
RUN pnpm run build
1111

1212
FROM nginx:stable-alpine
13+
# 安装htpasswd工具和dos2unix
14+
RUN apk add --no-cache apache2-utils dos2unix
15+
16+
# 复制Nginx配置
1317
COPY docker/nginx.conf /etc/nginx/conf.d/default.conf
18+
19+
# 复制Web应用
1420
COPY --from=build /app/packages/web/dist /usr/share/nginx/html
15-
# 复制启动脚本
21+
22+
# 复制并设置启动脚本
1623
COPY docker/generate-config.sh /docker-entrypoint.d/40-generate-config.sh
24+
COPY docker/generate-auth.sh /docker-entrypoint.d/30-generate-auth.sh
25+
26+
# 确保脚本有执行权限
1727
RUN chmod +x /docker-entrypoint.d/40-generate-config.sh
28+
RUN chmod +x /docker-entrypoint.d/30-generate-auth.sh
29+
30+
# 转换可能的Windows行尾符为Unix格式
31+
RUN dos2unix /docker-entrypoint.d/40-generate-config.sh
32+
RUN dos2unix /docker-entrypoint.d/30-generate-auth.sh
1833

1934
EXPOSE 80

README.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
![GitHub forks](https://img.shields.io/github/forks/linshenkx/prompt-optimizer?style=flat)
1313
[![Deploy with Vercel](https://img.shields.io/badge/Vercel-indigo?style=flat&logo=vercel)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Flinshenkx%2Fprompt-optimizer)
1414

15-
[在线体验](https://prompt.always200.com) | [快速开始](#快速开始) | [常见问题](#常见问题) | [开发文档](dev.md) | [Vercel部署指南](docs/vercel.md) ([密码保护](docs/vercel-password-protection_zh.md)) | [Chrome插件](https://chromewebstore.google.com/detail/prompt-optimizer/cakkkhboolfnadechdlgdcnjammejlna)
15+
[在线体验](https://prompt.always200.com) | [快速开始](#快速开始) | [常见问题](#常见问题) | [开发文档](dev.md) | [Vercel部署指南](docs/vercel.md) | [Chrome插件](https://chromewebstore.google.com/detail/prompt-optimizer/cakkkhboolfnadechdlgdcnjammejlna)
1616

1717
</div>
1818

@@ -53,10 +53,12 @@ Prompt Optimizer是一个强大的AI提示词优化工具,帮助你编写更
5353
- 先Fork项目到自己的GitHub
5454
- 然后在Vercel中导入该项目
5555
- 可跟踪源项目更新,便于同步最新功能和修复
56-
56+
- 配置环境变量:
57+
- `ACCESS_PASSWORD`:设置访问密码,启用访问限制
58+
- `VITE_OPENAI_API_KEY`等:配置各AI服务商的API密钥
59+
5760
更多详细的部署步骤和注意事项,请查看:
5861
- [Vercel部署指南](docs/vercel.md)
59-
- [Vercel密码保护指南](docs/vercel-password-protection_zh.md)
6062

6163
### 3. 安装Chrome插件
6264
1. 从Chrome商店安装(由于审批较慢,可能不是最新的):[Chrome商店地址](https://chromewebstore.google.com/detail/prompt-optimizer/cakkkhboolfnadechdlgdcnjammejlna)
@@ -67,12 +69,15 @@ Prompt Optimizer是一个强大的AI提示词优化工具,帮助你编写更
6769
# 运行容器(默认配置)
6870
docker run -d -p 80:80 --restart unless-stopped --name prompt-optimizer linshen/prompt-optimizer
6971

70-
# 运行容器(配置API密钥
72+
# 运行容器(配置API密钥和访问密码
7173
docker run -d -p 80:80 \
7274
-e VITE_OPENAI_API_KEY=your_key \
75+
-e ACCESS_USERNAME=your_username \ # 可选,默认为"admin"
76+
-e ACCESS_PASSWORD=your_password \ # 设置访问密码
7377
--restart unless-stopped \
7478
--name prompt-optimizer \
7579
linshen/prompt-optimizer
80+
7681
```
7782

7883
### 5. Docker Compose部署
@@ -81,11 +86,16 @@ docker run -d -p 80:80 \
8186
git clone https://github.com/linshenkx/prompt-optimizer.git
8287
cd prompt-optimizer
8388

84-
# 2. 可选:创建.env文件配置API密钥
89+
# 2. 可选:创建.env文件配置API密钥和访问认证
8590
cat > .env << EOF
91+
# API密钥配置
8692
VITE_OPENAI_API_KEY=your_openai_api_key
8793
VITE_GEMINI_API_KEY=your_gemini_api_key
8894
VITE_DEEPSEEK_API_KEY=your_deepseek_api_key
95+
96+
# Basic认证配置(密码保护)
97+
ACCESS_USERNAME=your_username # 可选,默认为"admin"
98+
ACCESS_PASSWORD=your_password # 设置访问密码
8999
EOF
90100

91101
# 3. 启动服务
@@ -171,7 +181,6 @@ pnpm dev:fresh # 完整重置并重新启动开发环境
171181
- [项目状态](docs/project-status.md) - 当前进度和计划
172182
- [产品需求](docs/prd.md) - 产品需求文档
173183
- [Vercel部署指南](docs/vercel.md) - Vercel部署详细说明
174-
- [Vercel密码保护指南](docs/vercel-password-protection_zh.md) - Vercel密码保护详细说明
175184

176185

177186
## Star History

README_EN.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@ Method 2: Fork the project and import to Vercel (Recommended):
5353
- First fork the project to your GitHub account
5454
- Then import the project to Vercel
5555
- This allows tracking of source project updates for easy syncing of new features and fixes
56+
- Configure environment variables:
57+
- `ACCESS_PASSWORD`: Set access password to enable access restriction
58+
- `VITE_OPENAI_API_KEY` etc.: Configure API keys for various AI service providers
5659

5760
For more detailed deployment steps and important notes, please check:
5861
- [Vercel Deployment Guide](docs/vercel_en.md)
59-
- [Vercel Password Protection Guide](docs/vercel-password-protection.md)
6062

6163
### 3. Install Chrome Extension
6264
1. Install from Chrome Web Store (may not be the latest version due to approval delays): [Chrome Web Store](https://chromewebstore.google.com/detail/prompt-optimizer/cakkkhboolfnadechdlgdcnjammejlna)
@@ -67,9 +69,11 @@ For more detailed deployment steps and important notes, please check:
6769
# Run container (default configuration)
6870
docker run -d -p 80:80 --restart unless-stopped --name prompt-optimizer linshen/prompt-optimizer
6971

70-
# Run container (with API key configuration)
72+
# Run container (with API key configuration and password protection)
7173
docker run -d -p 80:80 \
7274
-e VITE_OPENAI_API_KEY=your_key \
75+
-e ACCESS_USERNAME=your_username \ # Optional, defaults to "admin"
76+
-e ACCESS_PASSWORD=your_password \ # Required for password protection
7377
--restart unless-stopped \
7478
--name prompt-optimizer \
7579
linshen/prompt-optimizer
@@ -81,11 +85,16 @@ docker run -d -p 80:80 \
8185
git clone https://github.com/linshenkx/prompt-optimizer.git
8286
cd prompt-optimizer
8387

84-
# 2. Optional: Create .env file for API keys
88+
# 2. Optional: Create .env file for API keys and authentication
8589
cat > .env << EOF
90+
# API Key Configuration
8691
VITE_OPENAI_API_KEY=your_openai_api_key
8792
VITE_GEMINI_API_KEY=your_gemini_api_key
8893
VITE_DEEPSEEK_API_KEY=your_deepseek_api_key
94+
95+
# Basic Authentication
96+
ACCESS_USERNAME=your_username # Optional, defaults to "admin"
97+
ACCESS_PASSWORD=your_password # Required for authentication
8998
EOF
9099

91100
# 3. Start the service
@@ -171,7 +180,6 @@ For detailed project status, see [Project Status Document](docs/project-status.m
171180
- [Project Status](docs/project-status.md) - Current progress and plans
172181
- [Product Requirements](docs/prd.md) - Product requirements document
173182
- [Vercel Deployment Guide](docs/vercel_en.md) - Detailed instructions for Vercel deployment
174-
- [Vercel Password Protection Guide](docs/vercel-password-protection.md) - Detailed instructions for Vercel password protection
175183

176184
## Star History
177185

dev.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ docker build -t linshen/prompt-optimizer:$VERSION .
5151
docker tag linshen/prompt-optimizer:$VERSION linshen/prompt-optimizer:latest
5252

5353
# 运行容器
54-
docker run -d -p 80:80 --restart unless-stopped --name prompt-optimizer linshen/prompt-optimizer:$VERSION
54+
docker run -d -p 80:80 --restart unless-stopped --name prompt-optimizer -e ACCESS_PASSWORD=1234!@#$ linshen/prompt-optimizer:$VERSION
5555

5656

5757
# 推送

docker-compose.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
version: '3'
2+
13
services:
24
prompt-optimizer:
35
image: linshen/prompt-optimizer:latest
@@ -22,6 +24,9 @@ services:
2224
- VITE_CUSTOM_API_KEY=${VITE_CUSTOM_API_KEY:-}
2325
- VITE_CUSTOM_API_BASE_URL=${VITE_CUSTOM_API_BASE_URL:-}
2426
- VITE_CUSTOM_API_MODEL=${VITE_CUSTOM_API_MODEL:-}
27+
# Basic认证配置(可选)
28+
- ACCESS_USERNAME=${ACCESS_USERNAME:-admin} # 可选,默认为"admin"
29+
- ACCESS_PASSWORD=${ACCESS_PASSWORD:-} # 设置访问密码
2530
# 健康检查
2631
healthcheck:
2732
test: ["CMD", "curl", "-f", "http://localhost:80"]

docker/generate-auth.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/sh
2+
3+
# 检查是否设置了ACCESS_PASSWORD环境变量
4+
if [ -n "$ACCESS_PASSWORD" ]; then
5+
# 检查密码是否为空字符串
6+
if [ "$ACCESS_PASSWORD" = "" ]; then
7+
echo "警告: 设置了空密码,不安全。不启用Basic认证"
8+
# 创建空的auth配置(禁用认证)
9+
cat > /etc/nginx/conf.d/auth.conf << EOF
10+
# Basic认证未启用 - 密码为空
11+
auth_basic off;
12+
EOF
13+
exit 0
14+
fi
15+
16+
echo "启用Basic认证..."
17+
18+
# 创建认证文件目录
19+
mkdir -p /etc/nginx/auth
20+
21+
# 确定用户名(如果未设置ACCESS_USERNAME则使用默认值"admin")
22+
USERNAME=${ACCESS_USERNAME:-admin}
23+
24+
# 生成htpasswd文件 - 使用printf避免特殊字符问题
25+
printf '%s' "$ACCESS_PASSWORD" | htpasswd -i -c /etc/nginx/auth/.htpasswd "$USERNAME"
26+
27+
# 容器环境中简化权限管理 - 确保所有人都可读取认证文件
28+
chmod -R a+r /etc/nginx/auth
29+
30+
# 创建启用认证的配置
31+
cat > /etc/nginx/conf.d/auth.conf << EOF
32+
# 此文件由generate-auth.sh脚本自动生成
33+
auth_basic "请输入访问凭据 (Please enter your credentials)";
34+
auth_basic_user_file /etc/nginx/auth/.htpasswd;
35+
EOF
36+
37+
echo "Basic认证已配置,用户名: $USERNAME"
38+
else
39+
echo "未设置ACCESS_PASSWORD环境变量,不启用Basic认证"
40+
41+
# 创建空的auth配置(禁用认证)
42+
cat > /etc/nginx/conf.d/auth.conf << EOF
43+
# Basic认证未启用
44+
auth_basic off;
45+
EOF
46+
fi

docker/nginx.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ server {
3838

3939
# SPA应用路由支持
4040
location / {
41+
# 引入Basic认证配置
42+
include /etc/nginx/conf.d/auth.conf;
43+
4144
try_files $uri $uri/ /index.html;
4245
expires -1;
4346
add_header Cache-Control "no-store, no-cache, must-revalidate";

docs/vercel-password-protection.md

Lines changed: 0 additions & 58 deletions
This file was deleted.

docs/vercel-password-protection_zh.md

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)