refactor: upgrade to Go 1.26 and modernize code idioms
- Replace `interface{}` with `any` type alias across all packages
- Use built-in `min()`/`max()` for parameter clamping
- Use `slices.SortFunc`, `slices.Min`, `slices.Max` for cleaner code
- Use `strings.Cut()` for simpler string parsing in auth middleware
- Use `errors.Is()` for proper error comparison in handlers
- Update dependencies: golang.org/x/image 0.37.0 -> 0.38.0
- Add Wire code generation guidelines to ARCHITECTURE.md
- Disable Go cache in CI build workflow
This commit is contained in:
@@ -86,7 +86,7 @@ func (e *MessageEncryptor) DecryptMessageSegments(ciphertext string) ([]byte, er
|
||||
|
||||
// TryParseMessageSegments 尝试解析消息段
|
||||
// 先尝试解密,如果失败则尝试直接解析(兼容未加密的旧数据)
|
||||
func TryParseMessageSegments(ciphertext string, segments interface{}) error {
|
||||
func TryParseMessageSegments(ciphertext string, segments any) error {
|
||||
if ciphertext == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -93,14 +93,14 @@ func (r *PageRequest) IsBackward() bool {
|
||||
|
||||
// PageResponse 游标分页响应结构
|
||||
type PageResponse struct {
|
||||
Items interface{} `json:"items"`
|
||||
NextCursor string `json:"next_cursor"`
|
||||
PrevCursor string `json:"prev_cursor,omitempty"`
|
||||
HasMore bool `json:"has_more"`
|
||||
Items any `json:"items"`
|
||||
NextCursor string `json:"next_cursor"`
|
||||
PrevCursor string `json:"prev_cursor,omitempty"`
|
||||
HasMore bool `json:"has_more"`
|
||||
}
|
||||
|
||||
// NewPageResponse 创建分页响应
|
||||
func NewPageResponse(items interface{}, nextCursor, prevCursor string, hasMore bool) *PageResponse {
|
||||
func NewPageResponse(items any, nextCursor, prevCursor string, hasMore bool) *PageResponse {
|
||||
return &PageResponse{
|
||||
Items: items,
|
||||
NextCursor: nextCursor,
|
||||
|
||||
@@ -103,7 +103,7 @@ func (m *Metrics) GetHookMetrics() []MetricSnapshot {
|
||||
}
|
||||
|
||||
func parseKey(key string) (HookType, string) {
|
||||
for i := 0; i < len(key); i++ {
|
||||
for i := range len(key) {
|
||||
if key[i] == ':' {
|
||||
return HookType(key[:i]), key[i+1:]
|
||||
}
|
||||
|
||||
@@ -210,8 +210,8 @@ type responseFormatConfig struct {
|
||||
}
|
||||
|
||||
type chatMessage struct {
|
||||
Role string `json:"role"`
|
||||
Content interface{} `json:"content"`
|
||||
Role string `json:"role"`
|
||||
Content any `json:"content"`
|
||||
}
|
||||
|
||||
type contentPart struct {
|
||||
|
||||
@@ -73,7 +73,7 @@ func (c *Client) Get(ctx context.Context, key string) (string, error) {
|
||||
}
|
||||
|
||||
// Set 设置值
|
||||
func (c *Client) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error {
|
||||
func (c *Client) Set(ctx context.Context, key string, value any, expiration time.Duration) error {
|
||||
return c.rdb.Set(ctx, key, value, expiration).Err()
|
||||
}
|
||||
|
||||
@@ -121,12 +121,12 @@ func (c *Client) IsMiniRedis() bool {
|
||||
// ==================== Hash 操作 ====================
|
||||
|
||||
// HSet 设置 Hash 字段
|
||||
func (c *Client) HSet(ctx context.Context, key string, field string, value interface{}) error {
|
||||
func (c *Client) HSet(ctx context.Context, key string, field string, value any) error {
|
||||
return c.rdb.HSet(ctx, key, field, value).Err()
|
||||
}
|
||||
|
||||
// HMSet 批量设置 Hash 字段
|
||||
func (c *Client) HMSet(ctx context.Context, key string, values map[string]interface{}) error {
|
||||
func (c *Client) HMSet(ctx context.Context, key string, values map[string]any) error {
|
||||
return c.rdb.HMSet(ctx, key, values).Err()
|
||||
}
|
||||
|
||||
@@ -136,7 +136,7 @@ func (c *Client) HGet(ctx context.Context, key string, field string) (string, er
|
||||
}
|
||||
|
||||
// HMGet 批量获取 Hash 字段值
|
||||
func (c *Client) HMGet(ctx context.Context, key string, fields ...string) ([]interface{}, error) {
|
||||
func (c *Client) HMGet(ctx context.Context, key string, fields ...string) ([]any, error) {
|
||||
return c.rdb.HMGet(ctx, key, fields...).Result()
|
||||
}
|
||||
|
||||
@@ -203,7 +203,7 @@ func (c *Client) ZRevRange(ctx context.Context, key string, start, stop int64) (
|
||||
}
|
||||
|
||||
// ZRem 删除 Sorted Set 成员
|
||||
func (c *Client) ZRem(ctx context.Context, key string, members ...interface{}) error {
|
||||
func (c *Client) ZRem(ctx context.Context, key string, members ...any) error {
|
||||
return c.rdb.ZRem(ctx, key, members...).Err()
|
||||
}
|
||||
|
||||
|
||||
@@ -10,20 +10,20 @@ import (
|
||||
|
||||
// Response 统一响应结构
|
||||
type Response struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data any `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
// ResponseSnakeCase 统一响应结构(snake_case)
|
||||
type ResponseSnakeCase struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data any `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
// Success 成功响应
|
||||
func Success(c *gin.Context, data interface{}) {
|
||||
func Success(c *gin.Context, data any) {
|
||||
c.JSON(http.StatusOK, Response{
|
||||
Code: 0,
|
||||
Message: "success",
|
||||
@@ -32,7 +32,7 @@ func Success(c *gin.Context, data interface{}) {
|
||||
}
|
||||
|
||||
// SuccessWithMessage 成功响应带消息
|
||||
func SuccessWithMessage(c *gin.Context, message string, data interface{}) {
|
||||
func SuccessWithMessage(c *gin.Context, message string, data any) {
|
||||
c.JSON(http.StatusOK, Response{
|
||||
Code: 0,
|
||||
Message: message,
|
||||
@@ -95,15 +95,15 @@ func InternalServerError(c *gin.Context, message string) {
|
||||
|
||||
// PaginatedResponse 分页响应
|
||||
type PaginatedResponse struct {
|
||||
List interface{} `json:"list"`
|
||||
Total int64 `json:"total"`
|
||||
Page int `json:"page"`
|
||||
PageSize int `json:"page_size"`
|
||||
TotalPages int `json:"total_pages"`
|
||||
List any `json:"list"`
|
||||
Total int64 `json:"total"`
|
||||
Page int `json:"page"`
|
||||
PageSize int `json:"page_size"`
|
||||
TotalPages int `json:"total_pages"`
|
||||
}
|
||||
|
||||
// Paginated 分页成功响应
|
||||
func Paginated(c *gin.Context, list interface{}, total int64, page, pageSize int) {
|
||||
func Paginated(c *gin.Context, list any, total int64, page, pageSize int) {
|
||||
totalPages := int(total) / pageSize
|
||||
if int(total)%pageSize > 0 {
|
||||
totalPages++
|
||||
|
||||
@@ -16,11 +16,11 @@ const (
|
||||
|
||||
// Event 表示一个WebSocket事件
|
||||
type Event struct {
|
||||
ID uint64 `json:"event_id"`
|
||||
Type string `json:"type"`
|
||||
Event string `json:"event"` // 事件名称(兼容多种协议)
|
||||
TS int64 `json:"ts"`
|
||||
Payload interface{} `json:"payload"`
|
||||
ID uint64 `json:"event_id"`
|
||||
Type string `json:"type"`
|
||||
Event string `json:"event"` // 事件名称(兼容多种协议)
|
||||
TS int64 `json:"ts"`
|
||||
Payload any `json:"payload"`
|
||||
}
|
||||
|
||||
// Client 表示一个WebSocket客户端连接
|
||||
@@ -39,10 +39,10 @@ type Message struct {
|
||||
|
||||
// ResponseMessage 表示发送给客户端的消息
|
||||
type ResponseMessage struct {
|
||||
EventID uint64 `json:"event_id"`
|
||||
Type string `json:"type"`
|
||||
TS int64 `json:"ts"`
|
||||
Payload interface{} `json:"payload,omitempty"`
|
||||
EventID uint64 `json:"event_id"`
|
||||
Type string `json:"type"`
|
||||
TS int64 `json:"ts"`
|
||||
Payload any `json:"payload,omitempty"`
|
||||
}
|
||||
|
||||
// ErrorMessage 表示错误消息
|
||||
@@ -76,11 +76,11 @@ type Hub struct {
|
||||
seq uint64
|
||||
|
||||
mu sync.RWMutex
|
||||
clients map[string]map[uint64]*Client // userID -> clientID -> Client
|
||||
history map[string][]Event // userID -> []Event (用于断线重连历史回放)
|
||||
clients map[string]map[uint64]*Client // userID -> clientID -> Client
|
||||
history map[string][]Event // userID -> []Event (用于断线重连历史回放)
|
||||
subscribers map[string]map[uint64]*subscriber // userID -> subscriberID -> subscriber
|
||||
disconnectHandlers []DisconnectHandler // 断开连接事件处理器列表
|
||||
connectHandlers []ConnectHandler // 连接事件处理器列表
|
||||
disconnectHandlers []DisconnectHandler // 断开连接事件处理器列表
|
||||
connectHandlers []ConnectHandler // 连接事件处理器列表
|
||||
}
|
||||
|
||||
// NewHub 创建WebSocket Hub
|
||||
@@ -213,7 +213,7 @@ func (h *Hub) GetClientCount(userID string) int {
|
||||
}
|
||||
|
||||
// PublishToUser 向指定用户发送事件
|
||||
func (h *Hub) PublishToUser(userID string, eventType string, payload interface{}) Event {
|
||||
func (h *Hub) PublishToUser(userID string, eventType string, payload any) Event {
|
||||
ev := Event{
|
||||
ID: h.NextID(),
|
||||
Type: eventType,
|
||||
@@ -225,7 +225,7 @@ func (h *Hub) PublishToUser(userID string, eventType string, payload interface{}
|
||||
}
|
||||
|
||||
// PublishToUsers 向多个用户发送事件
|
||||
func (h *Hub) PublishToUsers(userIDs []string, eventType string, payload interface{}) {
|
||||
func (h *Hub) PublishToUsers(userIDs []string, eventType string, payload any) {
|
||||
for _, uid := range userIDs {
|
||||
h.PublishToUser(uid, eventType, payload)
|
||||
}
|
||||
@@ -233,7 +233,7 @@ func (h *Hub) PublishToUsers(userIDs []string, eventType string, payload interfa
|
||||
|
||||
// PublishToUserOnline 仅在用户在线时发送事件,不存入历史回放
|
||||
// 适用于通话信令等实时性消息,避免断线重连时重放过期消息
|
||||
func (h *Hub) PublishToUserOnline(userID string, eventType string, payload interface{}) bool {
|
||||
func (h *Hub) PublishToUserOnline(userID string, eventType string, payload any) bool {
|
||||
h.mu.RLock()
|
||||
targets := make([]*Client, 0, len(h.clients[userID]))
|
||||
for _, c := range h.clients[userID] {
|
||||
@@ -269,7 +269,7 @@ func (h *Hub) PublishToUserOnline(userID string, eventType string, payload inter
|
||||
// PublishToUserOnlineReliable 可靠地发送事件给在线用户(阻塞发送)
|
||||
// 用于重要的信令消息(如 SDP, ICE candidate),确保消息送达
|
||||
// 如果用户不在线,返回 false
|
||||
func (h *Hub) PublishToUserOnlineReliable(userID string, eventType string, payload interface{}) bool {
|
||||
func (h *Hub) PublishToUserOnlineReliable(userID string, eventType string, payload any) bool {
|
||||
h.mu.RLock()
|
||||
targets := make([]*Client, 0, len(h.clients[userID]))
|
||||
for _, c := range h.clients[userID] {
|
||||
@@ -376,7 +376,7 @@ func (h *Hub) SendError(client *Client, code string, message string) {
|
||||
}
|
||||
|
||||
// Broadcast 广播消息给所有连接
|
||||
func (h *Hub) Broadcast(eventType string, payload interface{}) {
|
||||
func (h *Hub) Broadcast(eventType string, payload any) {
|
||||
h.mu.RLock()
|
||||
userIDs := make([]string, 0, len(h.clients))
|
||||
for uid := range h.clients {
|
||||
@@ -454,4 +454,4 @@ func EncodeData(ev Event) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
return string(body), nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user