Xget 部署指南


Xget 部署指南

概述

Xget 支持多种部署方式,包括 Cloudflare Workers、EdgeOne Pages、Vercel、Netlify、Deno Deploy、Docker 和 Podman。本文档详细介绍了各种部署方式的步骤和配置。

前置要求

通用要求

  • Node.js 18+ 或兼容的 JavaScript 运行环境
  • Git 客户端
  • 基本的命令行操作能力

特定平台要求

Cloudflare Workers

  • Cloudflare 账户(免费计划即可)
  • Wrangler CLI 工具

EdgeOne Pages

  • 腾讯云账户
  • EdgeOne 服务

Vercel

  • Vercel 账户
  • Vercel CLI 工具

Netlify

  • Netlify 账户
  • Netlify CLI 工具

Deno Deploy

  • Deno 账户
  • Deno CLI 工具

Docker/Podman

  • Docker 或 Podman 运行时
  • 服务器或本地机器

部署到 Cloudflare Workers

方式一:使用 Wrangler CLI

1. 安装 Wrangler CLI

1
npm install -g wrangler

2. 登录 Cloudflare

1
wrangler login

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]
# 自定义配置变量
# CUSTOM_DOMAIN = "your-domain.com"

# KV 命名空间(可选)
[[kv_namespaces]]
binding = "CACHE"
id = "your-kv-namespace-id"

5. 部署

1
wrangler deploy

6. 验证部署

1
curl https://xget.your-subdomain.workers.dev/gh/microsoft/vscode/archive/main.zip

方式二:使用 Cloudflare Dashboard

1. 登录 Cloudflare Dashboard

访问 Cloudflare Dashboard

2. 创建 Worker

  1. 点击 “Workers & Pages”
  2. 点击 “Create Application”
  3. 选择 “Create Worker”
  4. 输入 Worker 名称
  5. 点击 “Deploy”

3. 上传代码

  1. 在 Worker 编辑器中粘贴 Xget 代码
  2. 点击 “Save and Deploy”

4. 配置自定义域名(可选)

  1. 在 Worker 设置中点击 “Triggers”
  2. 点击 “Custom Domains”
  3. 添加自定义域名

配置选项

环境变量

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. 创建站点

  1. 点击 “站点列表”
  2. 点击 “新建站点”
  3. 输入域名信息
  4. 选择 “静态网站” 或 “Serverless”

5. 上传代码

方式一:Git 仓库

  1. 在站点设置中选择 “源站设置”
  2. 添加 Git 仓库
  3. 配置构建命令和输出目录

方式二:直接上传

  1. 将构建后的文件打包
  2. 在控制台上传文件包

6. 配置边缘函数

  1. 在站点设置中选择 “边缘函数”
  2. 创建新的边缘函数
  3. 粘贴 Xget 代码
  4. 配置路由规则

7. 验证部署

1
curl https://your-domain.com/gh/microsoft/vscode/archive/main.zip

部署到 Vercel

1. 安装 Vercel CLI

1
npm install -g vercel

2. 登录 Vercel

1
vercel login

3. 克隆仓库

1
2
git clone https://github.com/xixu-me/Xget.git
cd Xget

4. 部署

1
vercel

按照提示选择配置选项。

5. 配置环境变量

在 Vercel Dashboard 中添加环境变量:

  • CACHE_TTL: 1800
  • MAX_RETRIES: 3
  • REQUEST_TIMEOUT: 30000

6. 生产部署

1
vercel --prod

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

1
netlify login

3. 克隆仓库

1
2
git clone https://github.com/xixu-me/Xget.git
cd Xget

4. 初始化项目

1
netlify init

按照提示选择配置选项。

5. 配置 Netlify Functions

创建 netlify.toml 文件:

1
2
3
4
5
[build]
functions = "netlify/functions"

[functions]
node_bundler = "esbuild"

6. 部署

1
netlify deploy --prod

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
# 使用安装脚本(Linux/macOS)
curl -fsSL https://deno.land/install.sh | sh

# 使用 Homebrew(macOS)
brew install deno

# 使用 Chocolatey(Windows)
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
// 使用 Deno 标准库
import { serve } from "https://deno.land/[email protected]/http/server.ts";

async function handler(req) {
// Xget 核心逻辑
}

serve(handler);

4. 登录 Deno Deploy

访问 Deno Deploy 并登录。

5. 创建新项目

  1. 点击 “New Project”
  2. 选择 “Deploy from GitHub”
  3. 选择 Xget 仓库
  4. 配置入口文件
  5. 点击 “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
# 使用 Node.js 18 作为基础镜像
FROM node:18-alpine

# 设置工作目录
WORKDIR /app

# 复制 package 文件
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

启动服务:

1
docker-compose up -d

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
# Ubuntu/Debian
sudo apt install podman

# Fedora
sudo dnf install podman

# Arch Linux
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
# 使用 Node.js 18 作为基础镜像
FROM node:18-alpine

# 设置工作目录
WORKDIR /app

# 复制 package 文件
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

启动服务:

1
podman-compose up -d

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, // 15 分钟
max: 1000 // 每个窗口期最多 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. 安全问题

  • 检查 CORS 配置
  • 验证速率限制
  • 审查安全标头

调试技巧

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

# 检查 Worker 状态
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:自托管,完全控制

无论选择哪种部署方式,都建议:

  1. 配置适当的缓存策略
  2. 启用安全防护
  3. 实施监控和日志
  4. 定期更新和维护

文章作者: ZeroXin
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 ZeroXin !
评论
  目录