Skip to content

Commit f16e709

Browse files
author
shijiashuai
committed
feat: add docker-compose stack with Caddy HTTPS and coturn TURN
- docker-compose.yml: webrtc + caddy (auto-HTTPS) + coturn (host network) - Caddyfile: reverse proxy config with DOMAIN env var - turnserver.conf.example: coturn config template (credentials gitignored) - docs/deployment.md: deployment guide with port table and env vars - ROADMAP.md: mark Phase 4 items as completed
1 parent 7b8cb04 commit f16e709

6 files changed

Lines changed: 179 additions & 6 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,6 @@ _site/
4545
.env
4646
.env.*
4747
*.local
48+
49+
# TURN credentials (use turnserver.conf.example as template)
50+
turnserver.conf

Caddyfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{$DOMAIN:localhost} {
2+
reverse_proxy webrtc:8080
3+
}

ROADMAP.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ description: WebRTC 示例项目各阶段开发计划与进度追踪
7878

7979
- [x] **按钮状态管理**
8080
- [x] 根据状态禁用/启用 `Join / Call / Hangup` 按钮,避免误触。
81-
- [ ] 例如:
81+
- [x] 例如:
8282
- 未加入房间:只能点 `Join`
8383
- 已加入房间但未通话:可以点 `Call``Hangup` 禁用。
8484
- 通话中:`Call` 禁用,只能点 `Hangup`
@@ -157,12 +157,12 @@ description: WebRTC 示例项目各阶段开发计划与进度追踪
157157

158158
> 目标:在保持 Demo 定位的前提下,理解 TURN/HTTPS 和 Docker 打包的基本概念,方便在不同机器上演示。
159159
160-
- [ ] **基础 TURN 支持(可选)**
161-
- [ ] 搭建或使用一个简单的 TURN 服务器(例如 coturn)。
162-
- [ ] 在前端 `iceServers` 中增加 TURN 配置(注意不要在仓库中泄露真实凭证)。
160+
- [x] **基础 TURN 支持(可选)**
161+
- [x] 搭建或使用一个简单的 TURN 服务器(例如 coturn)。
162+
- [x] 在前端 `iceServers` 中增加 TURN 配置(注意不要在仓库中泄露真实凭证)。
163163

164-
- [ ] **HTTPS / WSS(可选)**
165-
- [ ] 使用 Nginx/Caddy 等为本项目提供 HTTPS 反向代理,信令走 `wss://`
164+
- [x] **HTTPS / WSS(可选)**
165+
- [x] 使用 Nginx/Caddy 等为本项目提供 HTTPS 反向代理,信令走 `wss://`
166166

167167
- [x] **Docker 打包**
168168
- [x] 编写多阶段 `Dockerfile`,包含 Go 服务与静态前端文件。

docker-compose.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
services:
2+
webrtc:
3+
build: .
4+
ports:
5+
- "8080:8080"
6+
environment:
7+
- WS_ALLOWED_ORIGINS=*
8+
# - RTC_CONFIG_JSON={"iceServers":[{"urls":"turn:HOST:3478","username":"user","credential":"pass"}]}
9+
restart: unless-stopped
10+
11+
caddy:
12+
image: caddy:2-alpine
13+
ports:
14+
- "80:80"
15+
- "443:443"
16+
- "443:443/udp"
17+
volumes:
18+
- ./Caddyfile:/etc/caddy/Caddyfile:ro
19+
- caddy_data:/data
20+
- caddy_config:/config
21+
environment:
22+
- DOMAIN=${DOMAIN:-localhost}
23+
depends_on:
24+
- webrtc
25+
restart: unless-stopped
26+
27+
coturn:
28+
image: coturn/coturn:latest
29+
network_mode: host
30+
volumes:
31+
- ./turnserver.conf:/etc/coturn/turnserver.conf:ro
32+
command: ["-c", "/etc/coturn/turnserver.conf"]
33+
restart: unless-stopped
34+
35+
volumes:
36+
caddy_data:
37+
caddy_config:

docs/deployment.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
---
2+
layout: default
3+
title: 部署指南 — WebRTC
4+
description: 使用 Docker Compose 部署 WebRTC Demo(Caddy HTTPS + coturn TURN)
5+
---
6+
7+
[← 返回首页]({{ site.baseurl }}/)
8+
9+
# 部署指南
10+
11+
本项目提供 `docker-compose.yml`,一键启动三个服务:
12+
13+
| 服务 | 作用 |
14+
|:-----|:-----|
15+
| **webrtc** | Go 信令服务 + 前端静态文件 |
16+
| **caddy** | 反向代理,自动 HTTPS(Let's Encrypt) |
17+
| **coturn** | TURN/STUN 服务器,NAT 穿透 |
18+
19+
---
20+
21+
## 快速启动
22+
23+
### 1. 配置 TURN(可选)
24+
25+
```bash
26+
cp turnserver.conf.example turnserver.conf
27+
# 编辑 turnserver.conf,修改 user=your_user:your_password
28+
```
29+
30+
### 2. 配置 RTC_CONFIG_JSON
31+
32+
`docker-compose.yml` 中取消 `RTC_CONFIG_JSON` 的注释,填入你的 TURN 信息:
33+
34+
```yaml
35+
environment:
36+
- RTC_CONFIG_JSON={"iceServers":[{"urls":"turn:YOUR_SERVER_IP:3478","username":"your_user","credential":"your_password"}]}
37+
```
38+
39+
### 3. 启动
40+
41+
```bash
42+
# 设置域名(用于 Caddy 自动签发证书)
43+
export DOMAIN=your-domain.com
44+
45+
# 启动所有服务
46+
docker compose up -d
47+
48+
# 查看日志
49+
docker compose logs -f
50+
```
51+
52+
访问 `https://your-domain.com` 即可使用。
53+
54+
---
55+
56+
## 仅本地测试(无需 HTTPS)
57+
58+
不需要 Caddy 和 TURN 时,可以直接运行 Go 服务:
59+
60+
```bash
61+
go run ./cmd/server
62+
# 浏览器访问 http://localhost:8080
63+
```
64+
65+
或只启动 webrtc 服务:
66+
67+
```bash
68+
docker compose up -d webrtc
69+
# 浏览器访问 http://localhost:8080
70+
```
71+
72+
---
73+
74+
## 服务说明
75+
76+
### Caddy(HTTPS 反向代理)
77+
78+
- 默认监听 80/443 端口
79+
- 通过 `DOMAIN` 环境变量配置域名
80+
- 未设置 `DOMAIN` 时默认 `localhost`(无自动证书)
81+
- 需要公网域名 + DNS 解析才能自动签发 Let's Encrypt 证书
82+
83+
### coturn(TURN 服务器)
84+
85+
- 使用 `host` 网络模式,直接暴露 3478(UDP/TCP)和 5349(TLS)
86+
- 配置文件:`turnserver.conf`(已在 `.gitignore` 中,不会提交凭证)
87+
- 模板文件:`turnserver.conf.example`
88+
89+
### 环境变量
90+
91+
| 变量 | 服务 | 说明 | 默认值 |
92+
|:-----|:-----|:-----|:-------|
93+
| `DOMAIN` | caddy | Caddy 站点域名 | `localhost` |
94+
| `WS_ALLOWED_ORIGINS` | webrtc | WebSocket 允许的来源 | `*` |
95+
| `RTC_CONFIG_JSON` | webrtc | ICE/TURN 配置 JSON | 内置 STUN |
96+
97+
---
98+
99+
## 端口清单
100+
101+
| 端口 | 协议 | 服务 | 说明 |
102+
|:-----|:-----|:-----|:-----|
103+
| 80 | TCP | caddy | HTTP(自动跳转 HTTPS) |
104+
| 443 | TCP/UDP | caddy | HTTPS |
105+
| 8080 | TCP | webrtc | 直接访问(调试用) |
106+
| 3478 | TCP/UDP | coturn | TURN/STUN |
107+
| 5349 | TCP/UDP | coturn | TURN/TLS |

turnserver.conf.example

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# coturn TURN server configuration
2+
# Copy to turnserver.conf and update credentials before use.
3+
# cp turnserver.conf.example turnserver.conf
4+
5+
listening-port=3478
6+
tls-listening-port=5349
7+
8+
realm=lessup.webrtc
9+
server-name=lessup.webrtc
10+
11+
# Authentication (long-term credentials)
12+
lt-cred-mech
13+
user=webrtc_user:webrtc_password
14+
15+
# Security
16+
fingerprint
17+
no-multicast-peers
18+
no-tlsv1
19+
no-tlsv1_1
20+
21+
# Logging
22+
log-file=stdout
23+
verbose

0 commit comments

Comments
 (0)