跳转到内容

gtc start

gtc start 命令会启动 Greentic runtime 服务器。它会启动所有必要服务,包括 HTTP 服务器、NATS 总线和 flow 执行器。

Terminal window
gtc start [OPTIONS] <BUNDLE_PATH>
选项说明默认值
--host <HOST>绑定地址0.0.0.0
--port <PORT>HTTP 端口8080
--cloudflared <on/off>启用 Cloudflare tunnelon
--ngrok <on/off>启用 ngrok tunneloff
--nats <on/off>启用内嵌 NATSon
--skip-setup跳过 provider setup flowsfalse
--force-setup强制重新运行 setup flowsfalse
-v, --verbose启用详细/debug 日志false
-q, --quiet最小化输出false
Terminal window
gtc start ./my-bundle

这会启动:

  • 监听在 0.0.0.0:8080 的 HTTP 服务器
  • 内嵌 NATS 服务器
  • Cloudflared tunnel(如果可用)
Terminal window
# Use ngrok instead of cloudflared
gtc start ./my-bundle --cloudflared off --ngrok on
# With verbose logging
gtc start ./my-bundle --verbose
# Skip setup (if already configured)
gtc start ./my-bundle --skip-setup
Terminal window
# No tunnels, external NATS
gtc start ./my-bundle --cloudflared off --ngrok off
# Custom port
gtc start ./my-bundle --port 3000 --cloudflared off

Cloudflared 会创建一个通往 Cloudflare 网络的安全 tunnel:

Terminal window
gtc start ./my-bundle --cloudflared on
# Output:
# Cloudflare tunnel started: https://random-words.trycloudflare.com
# Webhook URLs will use: https://random-words.trycloudflare.com

在开发期间使用 ngrok 以获得稳定 URL:

Terminal window
gtc start ./my-bundle --ngrok on
# Output:
# ngrok tunnel started: https://abc123.ngrok-free.app
# Webhook URLs will use: https://abc123.ngrok-free.app

用于拥有公共域名的生产环境:

Terminal window
gtc start ./my-bundle --cloudflared off --ngrok off
# Make sure your server is publicly accessible
# and update provider webhooks with your domain
Terminal window
gtc start ./my-bundle --nats on
# Starts embedded NATS on localhost:4222
Terminal window
# Disable embedded, use external
gtc start ./my-bundle --nats off
# Set NATS URL via environment
GREENTIC_NATS_URL=nats://nats.example.com:4222 gtc start ./my-bundle --nats off

默认情况下,如果 providers 尚未配置,gtc start 会运行 setup flows:

Terminal window
gtc start ./my-bundle
# Runs setup flows for unconfigured providers

完全跳过 setup flows(使用现有配置):

Terminal window
gtc start ./my-bundle --skip-setup

强制重新运行所有 setup flows(在 URL 变更后很有用):

Terminal window
gtc start ./my-bundle --force-setup

运行时,服务器会暴露以下端点:

端点用途
GET /health健康检查
GET /ready就绪探针
POST /webhook/{provider}/{tenant}/{team}Provider webhooks
GET /api/v1/sessions会话管理
POST /api/v1/messages发送消息
GET /auth/configOAuth 配置端点
POST /oauth/token-exchange服务端 OIDC token exchange 代理
Terminal window
curl http://localhost:8080/health
# {"status": "healthy"}

Runtime 为需要用户认证的消息 provider(如 WebChat 或 Teams)提供两个 OAuth/OIDC 集成端点。

从 secrets store 读取 OAuth 配置并返回给客户端。客户端使用此端点发现发起授权流程所需的身份提供商设置。

Terminal window
curl http://localhost:8080/auth/config
# {
# "authority": "https://login.microsoftonline.com/{tenant-id}/v2.0",
# "client_id": "your-client-id",
# "redirect_uri": "https://your-public-url/auth/callback",
# "scope": "openid profile"
# }

这是一个用于 OIDC 授权码交换的服务端 token exchange 代理。它允许客户端在不直接从前端调用身份提供商 token 端点的情况下交换授权码,从而避免浏览器 CORS 限制。

Terminal window
curl -X POST http://localhost:8080/oauth/token-exchange \
-H "Content-Type: application/json" \
-d '{"code": "authorization_code_here", "redirect_uri": "https://your-public-url/auth/callback"}'

静态路由(例如健康检查和 OAuth 端点)无需显式 public base URL 即可配置。这意味着即使 tunnel 或 public URL 尚未建立,这些端点也会在服务器启动后立即可用。

Terminal window
# Debug (most verbose)
GREENTIC_LOG_LEVEL=debug gtc start ./my-bundle
# Info (default)
GREENTIC_LOG_LEVEL=info gtc start ./my-bundle
# Warning
GREENTIC_LOG_LEVEL=warn gtc start ./my-bundle
Terminal window
gtc start ./my-bundle --verbose
# Equivalent to GREENTIC_LOG_LEVEL=debug
Terminal window
# JSON format (for production)
GREENTIC_LOG_FORMAT=json gtc start ./my-bundle
# Pretty format (for development, default)
GREENTIC_LOG_FORMAT=pretty gtc start ./my-bundle
信号行为
SIGTERM优雅关闭
SIGINT (Ctrl+C)优雅关闭
SIGHUP重新加载配置
变量说明
GREENTIC_HOST绑定地址
GREENTIC_PORTHTTP 端口
GREENTIC_LOG_LEVEL日志详细程度
GREENTIC_LOG_FORMAT日志格式(json/pretty)
GREENTIC_NATS_URL外部 NATS URL
GREENTIC_REDIS_URL用于 sessions 的 Redis URL
Error: Address already in use (os error 48)

另一个进程正在使用端口 8080。停止该进程或使用其他端口:

Terminal window
gtc start ./my-bundle --port 3000
Error: Failed to connect to NATS

如果使用外部 NATS,请确保它正在运行且可访问。否则请启用内嵌 NATS:

Terminal window
gtc start ./my-bundle --nats on
Error: Failed to start cloudflared tunnel

安装 cloudflared,或改用 ngrok:

Terminal window
# Install cloudflared
brew install cloudflared
# Or use ngrok
gtc start ./my-bundle --cloudflared off --ngrok on
Error: Setup flow failed for messaging-telegram

检查 answers 文件中的凭据,并确保 public URL 可访问:

Terminal window
# Re-run setup
gtc start ./my-bundle --force-setup --verbose
Dockerfile
FROM rust:1.90 as builder
WORKDIR /app
COPY . .
RUN cargo build --release
FROM debian:bookworm-slim
COPY --from=builder /app/target/release/gtc /usr/local/bin/
COPY my-bundle /app/bundle
WORKDIR /app
EXPOSE 8080
CMD ["gtc", "start", "/app/bundle", "--cloudflared", "off"]
Terminal window
docker build -t my-worker .
docker run -p 8080:8080 my-worker