/** * FTP 代理服务测试示例 * 演示如何使用 FTP 代理控制器提供文件直链服务 */ import Fastify from 'fastify'; import ftpProxyController from '../controllers/ftp-proxy.js'; // 创建 Fastify 实例 const fastify = Fastify({ logger: { level: 'info' } }); // 添加根路径处理 fastify.get('/', async (request, reply) => { return reply.type('text/html').send(` FTP 代理服务测试

🚀 FTP 代理服务测试

这是一个 FTP 文件代理服务,提供 HTTP 直链访问 FTP 服务器上的文件。

⚠️ 注意:请确保已正确配置 json/ftp.json 文件,或在请求中提供 FTP 配置参数。

📋 可用接口

GET /ftp/health - 健康检查
示例:
curl http://localhost:3000/ftp/health
GET /ftp/status - 获取服务状态
示例:
curl http://localhost:3000/ftp/status
GET /ftp/list - 列出目录内容
参数:
path - 目录路径(可选,默认为 /)
config - FTP 配置(可选,JSON 格式)

示例:
curl "http://localhost:3000/ftp/list?path=/"
curl "http://localhost:3000/ftp/list?path=/uploads"
GET /ftp/info - 获取文件信息
参数:
path - 文件路径(必需)
config - FTP 配置(可选,JSON 格式)

示例:
curl "http://localhost:3000/ftp/info?path=/readme.txt"
GET /ftp/file - 获取文件直链(下载文件)
参数:
path - 文件路径(必需)
config - FTP 配置(可选,JSON 格式)

示例:
curl "http://localhost:3000/ftp/file?path=/readme.txt"
curl "http://localhost:3000/ftp/file?path=/images/photo.jpg" -o photo.jpg

支持 Range 请求:
curl -H "Range: bytes=0-1023" "http://localhost:3000/ftp/file?path=/large-file.zip"
POST /ftp/config - 测试 FTP 配置
请求体:
{
  "host": "192.168.31.10",
  "port": 2121,
  "username": "anonymous",
  "password": "anonymous@example.com",
  "secure": false,
  "pasv": true
}
示例:
curl -X POST -H "Content-Type: application/json" -d '{"host":"192.168.31.10","port":2121}' http://localhost:3000/ftp/config
DELETE /ftp/cache - 清理缓存
示例:
curl -X DELETE http://localhost:3000/ftp/cache

🔧 配置说明

FTP 配置文件位置:json/ftp.json

配置示例:
{
  "host": "192.168.31.10",
  "port": 2121,
  "username": "anonymous",
  "password": "anonymous@example.com",
  "secure": false,
  "pasv": true,
  "timeout": 30000,
  "verbose": false
}

🌐 使用场景

📝 测试步骤

  1. 确保 FTP 服务器正在运行并可访问
  2. 配置 json/ftp.json 文件
  3. 启动此代理服务:node ftp-proxy-example.js
  4. 测试健康检查:curl http://localhost:3000/ftp/health
  5. 列出根目录:curl "http://localhost:3000/ftp/list?path=/"
  6. 下载文件:curl "http://localhost:3000/ftp/file?path=/your-file.txt"
💡 提示:
`); }); // 启动服务器 const start = async () => { try { // 注册 FTP 代理控制器 await fastify.register(ftpProxyController); const port = process.env.PORT || 3000; const host = process.env.HOST || '0.0.0.0'; await fastify.listen({ port, host }); console.log(`🚀 FTP 代理服务已启动!`); console.log(`📍 服务地址: http://localhost:${port}`); console.log(`📋 API 文档: http://localhost:${port}`); console.log(`🔍 健康检查: http://localhost:${port}/ftp/health`); console.log(`📊 服务状态: http://localhost:${port}/ftp/status`); console.log(''); console.log('🔧 快速测试命令:'); console.log(` curl http://localhost:${port}/ftp/health`); console.log(` curl "http://localhost:${port}/ftp/list?path=/"`); console.log(` curl "http://localhost:${port}/ftp/status"`); } catch (err) { console.error('❌ 服务启动失败:', err); process.exit(1); } }; // 优雅关闭 process.on('SIGINT', async () => { console.log('\n🛑 正在关闭 FTP 代理服务...'); try { await fastify.close(); console.log('✅ 服务已安全关闭'); process.exit(0); } catch (err) { console.error('❌ 关闭服务时出错:', err); process.exit(1); } }); start();