Files
Cyrene/backend/gateway/internal/handler/notification_handler.go
T
AskaEth 71f0a1abdb feat: Go模块路径迁移 + Docker生产部署适配 + ethend Docker兼容
- 所有Go模块路径从 github.com/yourname/cyrene-ai 迁移到 git.yeij.top/AskaEth/Cyrene
- 5个Go Dockerfile添加 GOPROXY=https://goproxy.cn,direct 解决国内构建问题
- ai-core go.mod 添加 pkg/plugins replace 指令
- Caddyfile 简化为 http:// 通配 + handle 保留 /api 前缀
- ethend Dockerfile 适配 (npm install + 仅 COPY package.json)
- ethend 新增 RUNNING_IN_DOCKER 环境变量,健康检查改用Docker服务名
- ethend 数据库状态检查支持Docker hostname (postgres/redis/qdrant/minio)
- process-manager 新增 CONTAINER_SVC_MAP + Docker模式自动检测
- 统一 docker-compose.dev.db.yml 卷名 (pg_data/redis_data/qdrant_data/minio_data)
- docker-compose.yml ethend服务挂载docker.sock + 端口变量化
- 清理 .env 统一后的残留文件与提示信息

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-30 13:43:22 +08:00

163 lines
4.3 KiB
Go

package handler
import (
"encoding/json"
"git.yeij.top/AskaEth/Cyrene/pkg/logger"
"net/http"
"time"
"github.com/gin-gonic/gin"
"git.yeij.top/AskaEth/Cyrene/gateway/internal/config"
"git.yeij.top/AskaEth/Cyrene/gateway/internal/ws"
)
// NotificationHandler 通知推送处理器
type NotificationHandler struct {
cfg *config.Config
hub *ws.Hub
}
// NewNotificationHandler 创建通知处理器
func NewNotificationHandler(cfg *config.Config, hub *ws.Hub) *NotificationHandler {
return &NotificationHandler{cfg: cfg, hub: hub}
}
// PushNotificationRequest 推送通知请求体
type PushNotificationRequest struct {
UserID string `json:"user_id" binding:"required"`
Type string `json:"type" binding:"required,oneof=info warning success thinking reminder"`
Title string `json:"title" binding:"required"`
Body string `json:"body" binding:"required"`
Data map[string]interface{} `json:"data,omitempty"`
}
// Push 推送通知到指定用户 (需要 JWT 认证)
// POST /api/v1/notifications/push
func (h *NotificationHandler) Push(c *gin.Context) {
var req PushNotificationRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数无效: " + err.Error()})
return
}
// 生成通知
notif := h.buildNotification(req)
// 序列化 WS 消息
msg := ws.ServerMessage{
Type: "notification",
MessageID: "notif_" + generateID(),
Timestamp: time.Now().UnixMilli(),
Notification: notif,
}
data, err := json.Marshal(msg)
if err != nil {
logger.Printf("[notification] 序列化通知失败: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "内部错误"})
return
}
// 通过 Hub 推送给指定用户
h.hub.SendToUser(req.UserID, data)
logger.Printf("[notification] 通知已推送: user=%s type=%s title=%s", req.UserID, req.Type, req.Title)
c.JSON(http.StatusOK, gin.H{
"success": true,
"notification": gin.H{
"id": notif.ID,
"type": notif.Type,
"title": notif.Title,
"user_id": req.UserID,
"timestamp": notif.Timestamp,
"delivered": h.hub.UserClientCount(req.UserID) > 0,
},
})
}
// InternalNotify 内部服务推送通知 (使用内部 service token)
// POST /api/v1/internal/notify
func (h *NotificationHandler) InternalNotify(c *gin.Context) {
var req PushNotificationRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数无效: " + err.Error()})
return
}
// 生成通知
notif := h.buildNotification(req)
// 序列化 WS 消息
msg := ws.ServerMessage{
Type: "notification",
MessageID: "notif_" + generateID(),
Timestamp: time.Now().UnixMilli(),
Notification: notif,
}
data, err := json.Marshal(msg)
if err != nil {
logger.Printf("[notification] 序列化通知失败: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "内部错误"})
return
}
// 通过 Hub 推送给指定用户
h.hub.SendToUser(req.UserID, data)
logger.Printf("[notification] 内部通知已推送: user=%s type=%s title=%s", req.UserID, req.Type, req.Title)
c.JSON(http.StatusOK, gin.H{
"success": true,
"notification": gin.H{
"id": notif.ID,
"type": notif.Type,
"title": notif.Title,
"user_id": req.UserID,
"timestamp": notif.Timestamp,
"delivered": h.hub.UserClientCount(req.UserID) > 0,
},
})
}
// InternalNotifyAuth 内部服务认证中间件
func (h *NotificationHandler) InternalNotifyAuth() gin.HandlerFunc {
return func(c *gin.Context) {
token := c.GetHeader("X-Internal-Token")
if token == "" {
token = c.GetHeader("Authorization")
if len(token) > 7 && token[:7] == "Bearer " {
token = token[7:]
}
}
if token != h.cfg.InternalServiceToken {
c.JSON(http.StatusUnauthorized, gin.H{"error": "内部认证失败"})
c.Abort()
return
}
c.Next()
}
}
// buildNotification 构建 NotificationInfo
func (h *NotificationHandler) buildNotification(req PushNotificationRequest) *ws.NotificationInfo {
notifID := "notif_" + generateID()
now := time.Now().UTC().Format(time.RFC3339)
if req.Data == nil {
req.Data = make(map[string]interface{})
}
return &ws.NotificationInfo{
ID: notifID,
Type: req.Type,
Title: req.Title,
Body: req.Body,
Timestamp: now,
Data: req.Data,
}
}