Xget 部署指南
概述
Xget 支持多种部署方式,包括 Cloudflare Workers、EdgeOne Pages、Vercel、Netlify、Deno Deploy、Docker 和 Podman。本文档详细介绍了各种部署方式的步骤和配置。
前置要求
通用要求
- Node.js 18+ 或兼容的 JavaScript 运行环境
- Git 客户端
- 基本的命令行操作能力
特定平台要求
Cloudflare Workers
- Cloudflare 账户(免费计划即可)
- Wrangler CLI 工具
EdgeOne Pages
Vercel
Netlify
Deno Deploy
Docker/Podman
- Docker 或 Podman 运行时
- 服务器或本地机器
部署到 Cloudflare Workers
方式一:使用 Wrangler CLI
1. 安装 Wrangler CLI
2. 登录 Cloudflare
3. 克隆 Xget 仓库
1 2
| git clone https://github.com/xixu-me/Xget.git cd Xget
|
4. 配置 Wrangler
创建 wrangler.toml 文件:
1 2 3 4 5 6 7 8 9 10 11 12
| name = "xget" main = "src/index.js" compatibility_date = "2024-01-01"
[vars]
[[kv_namespaces]] binding = "CACHE" id = "your-kv-namespace-id"
|
5. 部署
6. 验证部署
1
| curl https://xget.your-subdomain.workers.dev/gh/microsoft/vscode/archive/main.zip
|
方式二:使用 Cloudflare Dashboard
1. 登录 Cloudflare Dashboard
访问 Cloudflare Dashboard
2. 创建 Worker
- 点击 “Workers & Pages”
- 点击 “Create Application”
- 选择 “Create Worker”
- 输入 Worker 名称
- 点击 “Deploy”
3. 上传代码
- 在 Worker 编辑器中粘贴 Xget 代码
- 点击 “Save and Deploy”
4. 配置自定义域名(可选)
- 在 Worker 设置中点击 “Triggers”
- 点击 “Custom Domains”
- 添加自定义域名
配置选项
环境变量
在 wrangler.toml 中配置:
1 2 3 4 5 6 7 8 9
| [vars]
CACHE_TTL = "1800"
MAX_RETRIES = "3"
REQUEST_TIMEOUT = "30000"
|
KV 存储
创建 KV 命名空间:
1
| wrangler kv:namespace create "CACHE"
|
将返回的 ID 添加到 wrangler.toml。
自定义域名
1
| wrangler domains add your-domain.com
|
部署到 EdgeOne Pages
1. 准备代码
1 2
| git clone https://github.com/xixu-me/Xget.git cd Xget
|
2. 构建项目
1 2
| npm install npm run build
|
3. 登录腾讯云
访问 腾讯云 EdgeOne 控制台
4. 创建站点
- 点击 “站点列表”
- 点击 “新建站点”
- 输入域名信息
- 选择 “静态网站” 或 “Serverless”
5. 上传代码
方式一:Git 仓库
- 在站点设置中选择 “源站设置”
- 添加 Git 仓库
- 配置构建命令和输出目录
方式二:直接上传
- 将构建后的文件打包
- 在控制台上传文件包
6. 配置边缘函数
- 在站点设置中选择 “边缘函数”
- 创建新的边缘函数
- 粘贴 Xget 代码
- 配置路由规则
7. 验证部署
1
| curl https://your-domain.com/gh/microsoft/vscode/archive/main.zip
|
部署到 Vercel
1. 安装 Vercel CLI
2. 登录 Vercel
3. 克隆仓库
1 2
| git clone https://github.com/xixu-me/Xget.git cd Xget
|
4. 部署
按照提示选择配置选项。
5. 配置环境变量
在 Vercel Dashboard 中添加环境变量:
CACHE_TTL: 1800MAX_RETRIES: 3REQUEST_TIMEOUT: 30000
6. 生产部署
7. 验证部署
1
| curl https://your-project.vercel.app/gh/microsoft/vscode/archive/main.zip
|
部署到 Netlify
1. 安装 Netlify CLI
1
| npm install -g netlify-cli
|
2. 登录 Netlify
3. 克隆仓库
1 2
| git clone https://github.com/xixu-me/Xget.git cd Xget
|
4. 初始化项目
按照提示选择配置选项。
5. 配置 Netlify Functions
创建 netlify.toml 文件:
1 2 3 4 5
| [build] functions = "netlify/functions"
[functions] node_bundler = "esbuild"
|
6. 部署
7. 验证部署
1
| curl https://your-site.netlify.app/gh/microsoft/vscode/archive/main.zip
|
部署到 Deno Deploy
1. 安装 Deno CLI
1 2 3 4 5 6 7 8
| curl -fsSL https://deno.land/install.sh | sh
brew install deno
choco install deno
|
2. 克隆仓库
1 2
| git clone https://github.com/xixu-me/Xget.git cd Xget
|
3. 适配 Deno
将代码适配为 Deno 兼容格式:
1 2 3 4 5 6 7 8
| import { serve } from "https://deno.land/[email protected]/http/server.ts";
async function handler(req) { }
serve(handler);
|
4. 登录 Deno Deploy
访问 Deno Deploy 并登录。
5. 创建新项目
- 点击 “New Project”
- 选择 “Deploy from GitHub”
- 选择 Xget 仓库
- 配置入口文件
- 点击 “Deploy”
6. 配置环境变量
在项目设置中添加环境变量。
7. 验证部署
1
| curl https://your-project.deno.dev/gh/microsoft/vscode/archive/main.zip
|
自托管部署(Docker)
1. 准备 Dockerfile
创建 Dockerfile:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD ["node", "src/index.js"]
|
2. 构建 Docker 镜像
1
| docker build -t xget:latest .
|
3. 运行容器
1 2 3 4 5 6 7
| docker run -d \ --name xget \ -p 3000:3000 \ -e CACHE_TTL=1800 \ -e MAX_RETRIES=3 \ -e REQUEST_TIMEOUT=30000 \ xget:latest
|
4. 使用 Docker Compose
创建 docker-compose.yml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| version: '3.8'
services: xget: build: . container_name: xget ports: - "3000:3000" environment: - CACHE_TTL=1800 - MAX_RETRIES=3 - REQUEST_TIMEOUT=30000 restart: unless-stopped networks: - xget-network
networks: xget-network: driver: bridge
|
启动服务:
5. 配置反向代理(Nginx)
创建 Nginx 配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| server { listen 80; server_name your-domain.com;
location / { proxy_pass http://localhost:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; 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; } }
|
重启 Nginx:
1 2
| sudo nginx -t sudo systemctl restart nginx
|
6. 配置 SSL(Let’s Encrypt)
使用 Certbot:
1 2
| sudo apt install certbot python3-certbot-nginx sudo certbot --nginx -d your-domain.com
|
7. 验证部署
1
| curl https://your-domain.com/gh/microsoft/vscode/archive/main.zip
|
自托管部署(Podman)
1. 安装 Podman
1 2 3 4 5 6 7 8
| sudo apt install podman
sudo dnf install podman
sudo pacman -S podman
|
2. 准备 Containerfile
创建 Containerfile(Podman 使用 Containerfile 而非 Dockerfile):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD ["node", "src/index.js"]
|
3. 构建镜像
1
| podman build -t xget:latest .
|
4. 运行容器
1 2 3 4 5 6 7
| podman run -d \ --name xget \ -p 3000:3000 \ -e CACHE_TTL=1800 \ -e MAX_RETRIES=3 \ -e REQUEST_TIMEOUT=30000 \ xget:latest
|
5. 使用 Podman Compose
安装 Podman Compose:
1
| pip install podman-compose
|
创建 docker-compose.yml(与 Docker 相同):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| version: '3.8'
services: xget: build: . container_name: xget ports: - "3000:3000" environment: - CACHE_TTL=1800 - MAX_RETRIES=3 - REQUEST_TIMEOUT=30000 restart: unless-stopped networks: - xget-network
networks: xget-network: driver: bridge
|
启动服务:
6. 配置系统服务
创建 systemd 服务文件 /etc/systemd/system/xget.service:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| [Unit] Description=Xget Service After=network.target
[Service] Type=forking User=podman Group=podman ExecStart=/usr/bin/podman-compose -f /path/to/docker-compose.yml up -d ExecStop=/usr/bin/podman-compose -f /path/to/docker-compose.yml down Restart=always
[Install] WantedBy=multi-user.target
|
启用并启动服务:
1 2 3
| sudo systemctl daemon-reload sudo systemctl enable xget sudo systemctl start xget
|
7. 验证部署
1
| curl https://your-domain.com/gh/microsoft/vscode/archive/main.zip
|
配置优化
性能优化
1. 缓存配置
Cloudflare Workers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| const cache = caches.default;
async function cachedFetch(request) { const cacheKey = new Request(request.url, request); const cached = await cache.match(cacheKey);
if (cached) { return cached; }
const response = await fetch(request); const clonedResponse = response.clone(); await cache.put(cacheKey, clonedResponse);
return response; }
|
自托管
1 2
| const NodeCache = require('node-cache'); const cache = new NodeCache({ stdTTL: 1800 });
|
2. 压缩配置
1 2 3
| const compression = require('compression'); app.use(compression());
|
3. 连接池配置
1 2 3 4 5 6 7 8
| const http = require('http'); const https = require('https');
const agent = new https.Agent({ keepAlive: true, maxSockets: 100, maxFreeSockets: 10 });
|
安全配置
1. CORS 配置
1 2 3 4 5 6
| app.use((req, res, next) => { res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Methods', 'GET, HEAD, OPTIONS'); res.header('Access-Control-Allow-Headers', 'Content-Type'); next(); });
|
2. 速率限制
1 2 3 4 5 6 7 8
| const rateLimit = require('express-rate-limit');
const limiter = rateLimit({ windowMs: 15 * 60 * 1000, max: 1000 });
app.use(limiter);
|
3. 安全标头
1 2
| const helmet = require('helmet'); app.use(helmet());
|
监控配置
1. 日志记录
1 2 3 4 5 6 7 8 9 10
| const winston = require('winston');
const logger = winston.createLogger({ level: 'info', format: winston.format.json(), transports: [ new winston.transports.File({ filename: 'error.log', level: 'error' }), new winston.transports.File({ filename: 'combined.log' }) ] });
|
2. 性能监控
1 2 3 4 5 6 7
| const promClient = require('prom-client');
const httpRequestDuration = new promClient.Histogram({ name: 'http_request_duration_seconds', help: 'Duration of HTTP requests in seconds', labelNames: ['method', 'route', 'code'] });
|
故障排查
常见问题
1. 部署失败
Cloudflare Workers
- 检查
wrangler.toml 配置 - 确认账户权限
- 查看部署日志
Docker
- 检查 Dockerfile 语法
- 确认端口映射
- 查看容器日志
2. 性能问题
3. 安全问题
调试技巧
1. 启用调试日志
1 2 3 4 5 6 7
| const DEBUG = process.env.DEBUG === 'true';
function debug(message) { if (DEBUG) { console.log(`[DEBUG] ${message}`); } }
|
2. 性能分析
1 2 3 4 5 6
| const start = Date.now();
const duration = Date.now() - start; console.log(`Operation took ${duration}ms`);
|
3. 错误追踪
1 2 3 4 5 6 7 8
| process.on('uncaughtException', (error) => { console.error('Uncaught Exception:', error); process.exit(1); });
process.on('unhandledRejection', (reason, promise) => { console.error('Unhandled Rejection at:', promise, 'reason:', reason); });
|
维护指南
更新部署
Cloudflare Workers
1 2
| git pull wrangler deploy
|
Docker
1 2 3 4
| git pull docker-compose down docker-compose build docker-compose up -d
|
备份配置
1 2 3
| cp wrangler.toml wrangler.toml.backup cp docker-compose.yml docker-compose.yml.backup
|
监控健康
1 2 3 4 5 6 7 8
| curl https://your-domain.com/health
docker ps
wrangler tail
|
最佳实践
1. 使用环境变量
避免硬编码配置,使用环境变量:
1 2 3 4 5
| const config = { cacheTTL: process.env.CACHE_TTL || 1800, maxRetries: process.env.MAX_RETRIES || 3, requestTimeout: process.env.REQUEST_TIMEOUT || 30000 };
|
2. 实现健康检查
1 2 3
| app.get('/health', (req, res) => { res.json({ status: 'ok', timestamp: Date.now() }); });
|
3. 优雅关闭
1 2 3 4 5 6
| process.on('SIGTERM', () => { server.close(() => { console.log('Server closed'); process.exit(0); }); });
|
4. 版本管理
使用语义化版本号:
1 2 3 4
| v1.0.0 - 初始版本 v1.0.1 - 补丁更新 v1.1.0 - 次要更新 v2.0.0 - 主要更新
|
总结
Xget 支持多种部署方式,选择合适的部署方式取决于你的需求:
- Cloudflare Workers:推荐用于生产环境,性能最佳
- EdgeOne Pages:适合国内用户,访问速度快
- Vercel/Netlify:易于部署,适合个人项目
- Deno Deploy:现代化部署,支持 TypeScript
- Docker/Podman:自托管,完全控制
无论选择哪种部署方式,都建议:
- 配置适当的缓存策略
- 启用安全防护
- 实施监控和日志
- 定期更新和维护