feat: 安装向导页支持滚动并新增 MySQL/Redis 连接测试
Install 页作为独立满屏路由自身承载纵向滚动,修复内容超出视口无法滚动; MySQL/Redis 卡片新增测试连接按钮,后端新增 /api/install/test-connection(仅安装模式可用, 不建库不落盘;MySQL 库不存在也视为可连接因安装时会自动建库)。
This commit is contained in:
@@ -119,6 +119,38 @@ export class InstallStorageDto {
|
||||
ossAccessKeySecret?: string;
|
||||
}
|
||||
|
||||
export class TestConnectionDto {
|
||||
@ApiProperty({ enum: ['mysql', 'redis'] })
|
||||
@IsIn(['mysql', 'redis'])
|
||||
target!: 'mysql' | 'redis';
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
host!: string;
|
||||
|
||||
@ApiProperty()
|
||||
@Type(() => Number)
|
||||
@IsInt()
|
||||
@Min(1)
|
||||
port!: number;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
database?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
username?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
password?: string;
|
||||
}
|
||||
|
||||
export class InstallRequestDto {
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Body, Controller, Get, Post } from '@nestjs/common';
|
||||
import { ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||
import { Public } from '../common/decorators/public.decorator';
|
||||
import { InstallRequestDto } from './dto/install.dto';
|
||||
import { InstallRequestDto, TestConnectionDto } from './dto/install.dto';
|
||||
import { InstallService } from './install.service';
|
||||
|
||||
@ApiTags('Install')
|
||||
@@ -16,6 +16,13 @@ export class InstallController {
|
||||
return this.installService.status();
|
||||
}
|
||||
|
||||
@Public()
|
||||
@Post('test-connection')
|
||||
@ApiOperation({ summary: 'Test MySQL/Redis connectivity' })
|
||||
testConnection(@Body() dto: TestConnectionDto) {
|
||||
return this.installService.testConnection(dto);
|
||||
}
|
||||
|
||||
@Public()
|
||||
@Post()
|
||||
@ApiOperation({ summary: 'Initialize installation' })
|
||||
|
||||
@@ -7,7 +7,7 @@ import * as bcrypt from 'bcryptjs';
|
||||
import { PrismaClient, RoleType, UserStatus } from '@prisma/client';
|
||||
import Redis from 'ioredis';
|
||||
import { envFilePath, isInstallMode } from '../config/runtime-config';
|
||||
import { InstallRequestDto } from './dto/install.dto';
|
||||
import { InstallRequestDto, TestConnectionDto } from './dto/install.dto';
|
||||
|
||||
const ENCRYPTED_PREFIX = 'enc:v1:';
|
||||
|
||||
@@ -48,6 +48,58 @@ export class InstallService {
|
||||
return { installed: true, restartRequired: true };
|
||||
}
|
||||
|
||||
// 安装页「测试连接」:只连通、不建库、不落盘。target=mysql|redis
|
||||
async testConnection(dto: TestConnectionDto): Promise<{ success: true; message: string }> {
|
||||
// 仅安装模式可用,避免安装完成后被当作公开的内网端口探测工具
|
||||
if (!isInstallMode()) {
|
||||
throw new Error('系统已安装,连接测试不可用');
|
||||
}
|
||||
if (dto.target === 'redis') {
|
||||
const redis = new Redis({
|
||||
host: dto.host,
|
||||
port: dto.port,
|
||||
password: dto.password || undefined,
|
||||
maxRetriesPerRequest: 1,
|
||||
connectTimeout: 5000,
|
||||
lazyConnect: true,
|
||||
});
|
||||
try {
|
||||
await redis.connect();
|
||||
await redis.ping();
|
||||
} finally {
|
||||
redis.disconnect();
|
||||
}
|
||||
return { success: true, message: 'Redis 连接成功' };
|
||||
}
|
||||
// MySQL:优先连业务库,库不存在(P1049)也视为「可连接」,因为安装时会自动建库
|
||||
const user = encodeURIComponent(dto.username ?? '');
|
||||
const password = encodeURIComponent(dto.password ?? '');
|
||||
const base = `mysql://${user}:${password}@${dto.host}:${dto.port}`;
|
||||
const dbName = dto.database || '';
|
||||
const tryConnect = async (url: string) => {
|
||||
const prisma = new PrismaClient({ datasources: { db: { url } } });
|
||||
try {
|
||||
await prisma.$connect();
|
||||
await prisma.$queryRaw`SELECT 1`;
|
||||
} finally {
|
||||
await prisma.$disconnect().catch(() => undefined);
|
||||
}
|
||||
};
|
||||
try {
|
||||
await tryConnect(`${base}/${encodeURIComponent(dbName)}`);
|
||||
return { success: true, message: 'MySQL 连接成功,业务库已存在' };
|
||||
} catch (e) {
|
||||
const code = (e as { code?: string }).code;
|
||||
const msg = (e as Error).message || '';
|
||||
if (code === 'P1003' || /Unknown database|P1049|does not exist/i.test(msg)) {
|
||||
// 库不存在,但服务器可连接:再连 information_schema 确认凭据可用
|
||||
await tryConnect(`${base}/information_schema`);
|
||||
return { success: true, message: 'MySQL 连接成功,业务库不存在(安装时将自动创建)' };
|
||||
}
|
||||
throw new Error(`MySQL 连接失败:${msg}`);
|
||||
}
|
||||
}
|
||||
|
||||
private buildMysqlUrl(dto: InstallRequestDto): string {
|
||||
const user = encodeURIComponent(dto.mysql.username);
|
||||
const password = encodeURIComponent(dto.mysql.password);
|
||||
|
||||
Reference in New Issue
Block a user