346 lines
8.0 KiB
TypeScript
346 lines
8.0 KiB
TypeScript
/**
|
|
* 配置生成器
|
|
*
|
|
* 根据硬件配置生成 docker-compose.yml 和 .env 文件
|
|
*/
|
|
|
|
import { DeploymentConfig, DeploymentMode } from './types'
|
|
import { randomBytes } from 'crypto'
|
|
|
|
/**
|
|
* 生成安全的随机密码
|
|
*/
|
|
export function generateSecurePassword(length: number = 32): string {
|
|
return randomBytes(length).toString('base64').slice(0, length)
|
|
}
|
|
|
|
/**
|
|
* 生成默认部署配置
|
|
*/
|
|
export function generateDefaultConfig(mode: DeploymentMode): DeploymentConfig {
|
|
return {
|
|
mode,
|
|
webPort: 3000,
|
|
adminPassword: generateSecurePassword(16),
|
|
enableHttps: false,
|
|
dbPassword: generateSecurePassword(32),
|
|
minioUser: 'admin',
|
|
minioPassword: generateSecurePassword(32),
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 生成 .env 配置文件
|
|
*/
|
|
export function generateEnvFile(config: DeploymentConfig): string {
|
|
return `# 3D 家具安装展示系统 - 环境配置
|
|
# 生成时间: ${new Date().toISOString()}
|
|
|
|
# 部署模式
|
|
DEPLOYMENT_MODE=${config.mode}
|
|
|
|
# Web 服务端口
|
|
WEB_PORT=${config.webPort}
|
|
|
|
# 管理员配置
|
|
ADMIN_USERNAME=admin
|
|
ADMIN_PASSWORD=${config.adminPassword}
|
|
|
|
# 数据库配置
|
|
DATABASE_URL=postgresql://postgres:${config.dbPassword}@postgres:5432/furniture_db
|
|
DB_PASSWORD=${config.dbPassword}
|
|
|
|
# MinIO 配置
|
|
S3_ENDPOINT=http://minio:9000
|
|
S3_BUCKET=furniture-3d-models
|
|
MINIO_USER=${config.minioUser}
|
|
MINIO_PASSWORD=${config.minioPassword}
|
|
|
|
# Redis 配置
|
|
REDIS_URL=redis://redis:6379
|
|
|
|
# HTTPS 配置
|
|
ENABLE_HTTPS=${config.enableHttps}
|
|
${config.customDomain ? `CUSTOM_DOMAIN=${config.customDomain}` : '# CUSTOM_DOMAIN='}
|
|
|
|
# 应用配置
|
|
NODE_ENV=production
|
|
`
|
|
}
|
|
|
|
/**
|
|
* 生成 Docker Compose 配置
|
|
*/
|
|
export function generateDockerCompose(mode: DeploymentMode): string {
|
|
if (mode === 'lightweight') {
|
|
return generateLightweightCompose()
|
|
}
|
|
return generateStandardCompose()
|
|
}
|
|
|
|
/**
|
|
* 生成标准模式 Docker Compose
|
|
*/
|
|
function generateStandardCompose(): string {
|
|
return `# 3D 家具安装展示系统 - 标准模式 (8GB+ 内存)
|
|
version: '3.8'
|
|
|
|
services:
|
|
frontend:
|
|
image: furniture-3d-frontend:latest
|
|
container_name: furniture-frontend
|
|
restart: unless-stopped
|
|
ports:
|
|
- "\${WEB_PORT:-3000}:80"
|
|
environment:
|
|
- NGINX_WORKER_PROCESSES=4
|
|
- NGINX_WORKER_CONNECTIONS=1024
|
|
- ENABLE_GZIP_COMPRESSION=true
|
|
volumes:
|
|
- nginx-cache:/var/cache/nginx
|
|
depends_on:
|
|
backend:
|
|
condition: service_healthy
|
|
mem_limit: 1g
|
|
cpus: 2.0
|
|
|
|
backend:
|
|
image: furniture-3d-backend:latest
|
|
container_name: furniture-backend
|
|
restart: unless-stopped
|
|
environment:
|
|
- NODE_ENV=production
|
|
- DATABASE_URL=\${DATABASE_URL}
|
|
- REDIS_URL=\${REDIS_URL}
|
|
- S3_ENDPOINT=\${S3_ENDPOINT}
|
|
- S3_BUCKET=\${S3_BUCKET}
|
|
- MINIO_USER=\${MINIO_USER}
|
|
- MINIO_PASSWORD=\${MINIO_PASSWORD}
|
|
- DB_POOL_SIZE=20
|
|
- ENABLE_COMPRESSION=true
|
|
- MAX_CONCURRENT_REQUESTS=50
|
|
- MODEL_CACHE_SIZE=200
|
|
depends_on:
|
|
postgres:
|
|
condition: service_healthy
|
|
redis:
|
|
condition: service_started
|
|
minio:
|
|
condition: service_started
|
|
healthcheck:
|
|
test: ["CMD", "curl", "-f", "http://localhost:3001/health"]
|
|
interval: 30s
|
|
timeout: 10s
|
|
retries: 3
|
|
start_period: 40s
|
|
mem_limit: 2g
|
|
cpus: 4.0
|
|
|
|
postgres:
|
|
image: postgres:15-alpine
|
|
container_name: furniture-postgres
|
|
restart: unless-stopped
|
|
environment:
|
|
- POSTGRES_DB=furniture_db
|
|
- POSTGRES_USER=postgres
|
|
- POSTGRES_PASSWORD=\${DB_PASSWORD}
|
|
command: >
|
|
postgres
|
|
-c shared_buffers=256MB
|
|
-c max_connections=100
|
|
-c work_mem=16MB
|
|
-c maintenance_work_mem=128MB
|
|
-c checkpoint_completion_target=0.9
|
|
volumes:
|
|
- postgres-data:/var/lib/postgresql/data
|
|
healthcheck:
|
|
test: ["CMD-SHELL", "pg_isready -U postgres"]
|
|
interval: 10s
|
|
timeout: 5s
|
|
retries: 5
|
|
mem_limit: 1g
|
|
cpus: 2.0
|
|
|
|
minio:
|
|
image: minio/minio:latest
|
|
container_name: furniture-minio
|
|
restart: unless-stopped
|
|
environment:
|
|
- MINIO_ROOT_USER=\${MINIO_USER}
|
|
- MINIO_ROOT_PASSWORD=\${MINIO_PASSWORD}
|
|
command: server /data --console-address ":9001"
|
|
volumes:
|
|
- minio-data:/data
|
|
mem_limit: 1g
|
|
cpus: 1.0
|
|
|
|
redis:
|
|
image: redis:7-alpine
|
|
container_name: furniture-redis
|
|
restart: unless-stopped
|
|
command: redis-server --maxmemory 400mb --maxmemory-policy allkeys-lru
|
|
volumes:
|
|
- redis-data:/data
|
|
mem_limit: 512m
|
|
cpus: 1.0
|
|
|
|
volumes:
|
|
postgres-data:
|
|
driver: local
|
|
minio-data:
|
|
driver: local
|
|
redis-data:
|
|
driver: local
|
|
nginx-cache:
|
|
driver: local
|
|
`
|
|
}
|
|
|
|
/**
|
|
* 生成轻量级模式 Docker Compose
|
|
*/
|
|
function generateLightweightCompose(): string {
|
|
return `# 3D 家具安装展示系统 - 轻量级模式 (4-8GB 内存)
|
|
version: '3.8'
|
|
|
|
services:
|
|
frontend:
|
|
image: furniture-3d-frontend:latest
|
|
container_name: furniture-frontend
|
|
restart: unless-stopped
|
|
ports:
|
|
- "\${WEB_PORT:-3000}:80"
|
|
environment:
|
|
- NGINX_WORKER_PROCESSES=2
|
|
- NGINX_WORKER_CONNECTIONS=512
|
|
- ENABLE_GZIP_COMPRESSION=true
|
|
volumes:
|
|
- nginx-cache:/var/cache/nginx
|
|
depends_on:
|
|
backend:
|
|
condition: service_healthy
|
|
mem_limit: 512m
|
|
cpus: 1.0
|
|
|
|
backend:
|
|
image: furniture-3d-backend:latest
|
|
container_name: furniture-backend
|
|
restart: unless-stopped
|
|
environment:
|
|
- NODE_ENV=production
|
|
- DATABASE_URL=\${DATABASE_URL}
|
|
- REDIS_URL=\${REDIS_URL}
|
|
- S3_ENDPOINT=\${S3_ENDPOINT}
|
|
- S3_BUCKET=\${S3_BUCKET}
|
|
- MINIO_USER=\${MINIO_USER}
|
|
- MINIO_PASSWORD=\${MINIO_PASSWORD}
|
|
- DB_POOL_SIZE=10
|
|
- ENABLE_COMPRESSION=true
|
|
- MAX_CONCURRENT_REQUESTS=20
|
|
- MODEL_CACHE_SIZE=100
|
|
depends_on:
|
|
postgres:
|
|
condition: service_healthy
|
|
redis:
|
|
condition: service_started
|
|
minio:
|
|
condition: service_started
|
|
healthcheck:
|
|
test: ["CMD", "curl", "-f", "http://localhost:3001/health"]
|
|
interval: 30s
|
|
timeout: 10s
|
|
retries: 3
|
|
start_period: 40s
|
|
mem_limit: 1g
|
|
cpus: 2.0
|
|
|
|
postgres:
|
|
image: postgres:15-alpine
|
|
container_name: furniture-postgres
|
|
restart: unless-stopped
|
|
environment:
|
|
- POSTGRES_DB=furniture_db
|
|
- POSTGRES_USER=postgres
|
|
- POSTGRES_PASSWORD=\${DB_PASSWORD}
|
|
command: >
|
|
postgres
|
|
-c shared_buffers=128MB
|
|
-c max_connections=50
|
|
-c work_mem=8MB
|
|
-c maintenance_work_mem=64MB
|
|
-c checkpoint_completion_target=0.9
|
|
volumes:
|
|
- postgres-data:/var/lib/postgresql/data
|
|
healthcheck:
|
|
test: ["CMD-SHELL", "pg_isready -U postgres"]
|
|
interval: 10s
|
|
timeout: 5s
|
|
retries: 5
|
|
mem_limit: 512m
|
|
cpus: 1.0
|
|
|
|
minio:
|
|
image: minio/minio:latest
|
|
container_name: furniture-minio
|
|
restart: unless-stopped
|
|
environment:
|
|
- MINIO_ROOT_USER=\${MINIO_USER}
|
|
- MINIO_ROOT_PASSWORD=\${MINIO_PASSWORD}
|
|
command: server /data --console-address ":9001"
|
|
volumes:
|
|
- minio-data:/data
|
|
mem_limit: 512m
|
|
cpus: 0.5
|
|
|
|
redis:
|
|
image: redis:7-alpine
|
|
container_name: furniture-redis
|
|
restart: unless-stopped
|
|
command: redis-server --maxmemory 200mb --maxmemory-policy allkeys-lru
|
|
volumes:
|
|
- redis-data:/data
|
|
mem_limit: 256m
|
|
cpus: 0.5
|
|
|
|
volumes:
|
|
postgres-data:
|
|
driver: local
|
|
minio-data:
|
|
driver: local
|
|
redis-data:
|
|
driver: local
|
|
nginx-cache:
|
|
driver: local
|
|
`
|
|
}
|
|
|
|
/**
|
|
* 验证配置
|
|
*/
|
|
export function validateConfig(config: DeploymentConfig): { valid: boolean; errors: string[] } {
|
|
const errors: string[] = []
|
|
|
|
// 验证端口
|
|
if (config.webPort < 1 || config.webPort > 65535) {
|
|
errors.push('Web 端口必须在 1-65535 之间')
|
|
}
|
|
|
|
// 验证密码强度
|
|
if (config.adminPassword.length < 8) {
|
|
errors.push('管理员密码长度至少为 8 个字符')
|
|
}
|
|
|
|
if (config.dbPassword.length < 16) {
|
|
errors.push('数据库密码长度至少为 16 个字符')
|
|
}
|
|
|
|
if (config.minioPassword.length < 16) {
|
|
errors.push('MinIO 密码长度至少为 16 个字符')
|
|
}
|
|
|
|
return {
|
|
valid: errors.length === 0,
|
|
errors,
|
|
}
|
|
}
|