Skip to content

soulteary/gorge-notification

Repository files navigation

gorge-notification

Gorge 平台中的实时通知推送微服务,以 Go 实现,替代 Phorge 原有的 Node.js Aphlict 通知服务器。

采用双服务器架构(Admin HTTP + Client WebSocket),通过 Hub 消息中枢管理连接和推送,支持多实例隔离和集群节点间消息转发。协议与原始 Aphlict 实现完全兼容。

架构

服务器 默认端口 协议 职责
Admin :22281 HTTP 接收 PHP 端的通知推送请求,转发到集群节点
Client :22280 WebSocket 浏览器端长连接,支持订阅、消息推送、历史回放

配置

支持两种配置方式:环境变量(默认)和 Aphlict 兼容的 JSON 配置文件。

环境变量

变量 默认值 说明
CLIENT_PORT 22280 WebSocket 客户端端口
ADMIN_PORT 22281 Admin HTTP 端口
LISTEN_ADDR 0.0.0.0 监听地址
APHLICT_CONFIG (空) JSON 配置文件路径,设置后从文件加载配置

JSON 配置文件

当设置 APHLICT_CONFIG 时,从指定的 JSON 文件加载配置,格式兼容原始 Aphlict:

{
  "servers": [
    {"type": "client", "port": 22280, "listen": "0.0.0.0"},
    {"type": "admin", "port": 22281, "listen": "127.0.0.1"}
  ],
  "cluster": [
    {"host": "peer1.example.com", "port": 22281, "protocol": "http"}
  ]
}

API

Admin 端点

POST /

发布通知到已连接的浏览器客户端。

请求:JSON body,包含 subscribers(PHID 数组)、type 及其他消息字段。

curl -X POST http://localhost:22281/ \
  -H 'Content-Type: application/json' \
  -d '{"type":"notification","key":"123","subscribers":["PHID-USER-aaa"]}'

响应

{"fingerprint": "<server-fingerprint>"}

GET /status/

返回服务器状态(运行时长、活跃客户端数、消息统计)。

{
  "instance": "default",
  "uptime": 86400000,
  "clients.active": 5,
  "clients.total": 42,
  "messages.in": 1000,
  "messages.out": 3500,
  "history.size": 128,
  "history.age": 45000,
  "version": 8
}

GET /healthz

健康检查端点。

Client WebSocket 协议

命令(浏览器 -> 服务器)

命令 数据格式 说明
subscribe ["PHID-USER-xxx"] 订阅指定 PHID 的通知
unsubscribe ["PHID-USER-xxx"] 取消订阅
replay {"age": 60000} 回放最近的消息(毫秒)
ping 任意 心跳保活,服务器返回 {"type":"pong"}

消息(服务器 -> 浏览器)

服务器推送的 JSON 对象与发布时的通知内容一致。

多实例支持

通过 WebSocket URL 路径指定实例:ws://host:22280/~instanceName/。未指定时使用 default 实例。

构建 & 运行

go build -o gorge-notification ./cmd/server
./gorge-notification

Docker

docker build -t gorge-notification .
docker run -p 22280:22280 -p 22281:22281 gorge-notification

测试

go test ./...

项目结构

gorge-notification/
├── cmd/server/
│   ├── main.go                     # 服务入口,双服务器启动与路由注册
│   └── main_test.go                # Admin/Client API 端到端测试
├── internal/
│   ├── config/
│   │   ├── config.go               # 配置加载(JSON 文件 / 环境变量)
│   │   └── config_test.go          # 配置解析测试
│   ├── hub/
│   │   ├── hub.go                  # 消息中枢:发布、历史管理、状态查询
│   │   ├── listener.go             # WebSocket 监听器:订阅管理、消息写入
│   │   └── hub_test.go             # Hub 单元测试
│   └── peer/
│       ├── peer.go                 # 集群对等节点:HTTP 转发、指纹去重
│       └── peer_test.go            # Peer 单元测试
├── Dockerfile                      # 多阶段 Docker 构建
├── go.mod
└── go.sum

技术栈

  • 语言:Go 1.26
  • WebSocketgorilla/websocket v1.5.3
  • 许可证:Apache License 2.0

About

A Go-based real-time notification microservice within the Gorge platform, replacing Phorge’s original Node.js Aphlict notification server.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors