@@ -0,0 +1,237 @@
|
||||
# 打包编译说明
|
||||
|
||||
## 快速编译
|
||||
|
||||
### Windows
|
||||
|
||||
```bash
|
||||
# 方法1: 使用批处理脚本
|
||||
build.bat
|
||||
|
||||
# 方法2: 手动编译
|
||||
go build -ldflags="-s -w" -o software-download-center.exe .
|
||||
```
|
||||
|
||||
### Linux/macOS
|
||||
|
||||
```bash
|
||||
# 方法1: 使用 Shell 脚本
|
||||
chmod +x build.sh
|
||||
./build.sh
|
||||
|
||||
# 方法2: 手动编译
|
||||
go build -ldflags="-s -w" -o software-download-center .
|
||||
```
|
||||
|
||||
## 交叉编译
|
||||
|
||||
### 编译 Windows 版本(在 Linux/macOS 上)
|
||||
|
||||
```bash
|
||||
GOOS=windows GOARCH=amd64 go build -ldflags="-s -w" -o software-download-center.exe .
|
||||
```
|
||||
|
||||
### 编译 Linux 版本(在 Windows 上)
|
||||
|
||||
```bash
|
||||
set GOOS=linux
|
||||
set GOARCH=amd64
|
||||
go build -ldflags="-s -w" -o software-download-center .
|
||||
```
|
||||
|
||||
### 编译 macOS 版本
|
||||
|
||||
```bash
|
||||
# Intel 版本
|
||||
GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w" -o software-download-center .
|
||||
|
||||
# Apple Silicon (M1/M2) 版本
|
||||
GOOS=darwin GOARCH=arm64 go build -ldflags="-s -w" -o software-download-center .
|
||||
```
|
||||
|
||||
## 编译参数说明
|
||||
|
||||
- `-ldflags="-s -w"`: 减小可执行文件大小
|
||||
- `-s`: 去除符号表
|
||||
- `-w`: 去除调试信息
|
||||
|
||||
## 部署文件结构
|
||||
|
||||
编译后的目录结构:
|
||||
|
||||
```
|
||||
output/
|
||||
├── software-download-center_*.exe (或可执行文件)
|
||||
├── start.bat (Windows 启动脚本)
|
||||
├── start.sh (Linux/macOS 启动脚本)
|
||||
├── public/ (静态资源目录)
|
||||
│ ├── css/
|
||||
│ ├── img/
|
||||
│ ├── fonts/
|
||||
│ ├── lang/
|
||||
│ └── *.json
|
||||
├── views/ (模板目录)
|
||||
│ ├── index.html
|
||||
│ ├── admin.html
|
||||
│ ├── 404.html
|
||||
│ └── 500.html
|
||||
├── README.md
|
||||
└── ADMIN.md
|
||||
```
|
||||
|
||||
## 运行服务端
|
||||
|
||||
### Windows
|
||||
|
||||
```bash
|
||||
# 方法1: 使用启动脚本
|
||||
start.bat
|
||||
|
||||
# 方法2: 直接运行
|
||||
software-download-center_windows_amd64.exe
|
||||
|
||||
# 方法3: 指定端口
|
||||
set PORT=8080
|
||||
software-download-center_windows_amd64.exe
|
||||
```
|
||||
|
||||
### Linux/macOS
|
||||
|
||||
```bash
|
||||
# 方法1: 使用启动脚本
|
||||
chmod +x start.sh
|
||||
./start.sh
|
||||
|
||||
# 方法2: 直接运行
|
||||
chmod +x software-download-center_linux_amd64
|
||||
./software-download-center_linux_amd64
|
||||
|
||||
# 方法3: 指定端口
|
||||
PORT=8080 ./software-download-center_linux_amd64
|
||||
```
|
||||
|
||||
## 作为系统服务运行
|
||||
|
||||
### Linux (systemd)
|
||||
|
||||
创建服务文件 `/etc/systemd/system/software-download-center.service`:
|
||||
|
||||
```ini
|
||||
[Unit]
|
||||
Description=Software Download Center
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=www-data
|
||||
WorkingDirectory=/opt/software-download-center
|
||||
ExecStart=/opt/software-download-center/software-download-center_linux_amd64
|
||||
Restart=always
|
||||
RestartSec=5
|
||||
Environment="PORT=3355"
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
```
|
||||
|
||||
启动服务:
|
||||
|
||||
```bash
|
||||
sudo systemctl daemon-reload
|
||||
sudo systemctl enable software-download-center
|
||||
sudo systemctl start software-download-center
|
||||
sudo systemctl status software-download-center
|
||||
```
|
||||
|
||||
### Windows (NSSM)
|
||||
|
||||
使用 NSSM 将程序安装为 Windows 服务:
|
||||
|
||||
```bash
|
||||
# 下载 NSSM: https://nssm.cc/download
|
||||
nssm install SoftwareDownloadCenter "C:\path\to\software-download-center.exe"
|
||||
nssm set SoftwareDownloadCenter AppDirectory "C:\path\to"
|
||||
nssm set SoftwareDownloadCenter AppEnvironmentExtra PORT=3355
|
||||
nssm start SoftwareDownloadCenter
|
||||
```
|
||||
|
||||
## 生产环境建议
|
||||
|
||||
1. **使用反向代理**(Nginx/Caddy)
|
||||
```nginx
|
||||
server {
|
||||
listen 80;
|
||||
server_name your-domain.com;
|
||||
|
||||
location / {
|
||||
proxy_pass http://localhost:3355;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
2. **使用进程管理器**
|
||||
- Linux: systemd, supervisor, pm2
|
||||
- Windows: NSSM, Windows Service
|
||||
- 跨平台: PM2
|
||||
|
||||
3. **配置 HTTPS**
|
||||
- 使用 Let's Encrypt 免费证书
|
||||
- 配置自动续期
|
||||
|
||||
4. **日志管理**
|
||||
- 配置日志轮转
|
||||
- 使用日志收集工具(如 ELK)
|
||||
|
||||
5. **监控**
|
||||
- 配置健康检查
|
||||
- 使用监控工具(如 Prometheus)
|
||||
|
||||
## 文件大小优化
|
||||
|
||||
编译后的文件大小通常在 15-25MB 左右。如需进一步优化:
|
||||
|
||||
1. 使用 UPX 压缩(可选):
|
||||
```bash
|
||||
upx --best software-download-center.exe
|
||||
```
|
||||
|
||||
2. 使用静态链接(减小依赖):
|
||||
```bash
|
||||
CGO_ENABLED=0 go build -ldflags="-s -w" -o software-download-center .
|
||||
```
|
||||
|
||||
## 常见问题
|
||||
|
||||
### 编译失败
|
||||
|
||||
- 确保 Go 版本 >= 1.21
|
||||
- 运行 `go mod download` 下载依赖
|
||||
- 检查网络连接(需要访问 Go 模块仓库)
|
||||
|
||||
### 运行时错误
|
||||
|
||||
- 确保 `public` 和 `views` 目录存在
|
||||
- 检查文件权限
|
||||
- 查看日志输出
|
||||
|
||||
### 端口被占用
|
||||
|
||||
- 修改 `PORT` 环境变量
|
||||
- 或修改代码中的默认端口
|
||||
|
||||
## 版本信息
|
||||
|
||||
编译时可以通过 `-ldflags` 注入版本信息:
|
||||
|
||||
```bash
|
||||
go build -ldflags="-X main.Version=1.0.0 -X main.BuildTime=$(date +%Y-%m-%d)" -o software-download-center .
|
||||
```
|
||||
|
||||
然后在代码中定义:
|
||||
|
||||
```go
|
||||
var Version = "dev"
|
||||
var BuildTime = "unknown"
|
||||
```
|
||||
Reference in New Issue
Block a user