refactor(server): decouple services and improve architecture
All checks were successful
Build Backend / build (push) Successful in 4m55s
Build Backend / build-docker (push) Successful in 10m34s

- Introduce interfaces for all major services (JWT, PostAI, Comment, Message, Notification, QRCodeLogin, Upload, Vote, etc.) to support dependency inversion.
- Move query parameters from `internal/dto` to a new `internal/query` package to separate request payloads from data transfer objects.
- Refactor `internal/router` to use embedded `RouterDeps` for cleaner dependency management.
- Decouple handlers from repositories by injecting services instead of direct repository access, ensuring proper layering.
- Improve database initialization by moving it from `internal/model` to `internal/database`.
- Optimize message decryption by implementing a more efficient `BatchDecrypt` method in `MessageEncryptor` using a worker pool.
- Enhance error handling and security by implementing fail-fast checks for encryption key length during startup.
- Clean up unused code, including the `avatar` package and several unused DTOs.
This commit is contained in:
2026-06-15 03:41:59 +08:00
parent 9951043034
commit d9aa4b46c3
78 changed files with 2156 additions and 1640 deletions

View File

@@ -5,7 +5,6 @@ import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"time"
@@ -126,73 +125,3 @@ func (s *liveKitService) verifyWebhookSignature(body []byte, authHeader string)
return nil
}
// WebhookRoomFinished 处理 room_finished 事件的负载
type WebhookRoomFinished struct {
RoomName string `json:"room_name"`
Duration int64 `json:"duration"`
StartedAt int64 `json:"started_at"`
EndedAt int64 `json:"ended_at"`
}
// ParseRoomFinished 从 webhook event 中解析 room_finished 数据
func ParseRoomFinished(event *livekit.WebhookEvent) (*WebhookRoomFinished, error) {
if event.Event != "room_finished" {
return nil, fmt.Errorf("not a room_finished event: %s", event.Event)
}
room := event.Room
if room == nil {
return nil, fmt.Errorf("room is nil in webhook event")
}
duration := int64(0)
startedAt := int64(0)
endedAt := int64(0)
if room.CreationTime > 0 {
startedAt = room.CreationTime
}
endedAt = time.Now().Unix()
if startedAt > 0 {
duration = endedAt - startedAt
}
return &WebhookRoomFinished{
RoomName: room.Name,
Duration: duration,
StartedAt: startedAt,
EndedAt: endedAt,
}, nil
}
// WebhookEventJSON 用于 JSON 序列化的 webhook 事件
type WebhookEventJSON struct {
Event string `json:"event"`
Room *WebhookRoomJSON `json:"room,omitempty"`
RoomName string `json:"room_name,omitempty"`
}
// WebhookRoomJSON webhook 房间信息 JSON
type WebhookRoomJSON struct {
Sid string `json:"sid,omitempty"`
Name string `json:"name,omitempty"`
CreationTime int64 `json:"creation_time,omitempty"`
NumParticipants uint32 `json:"num_participants,omitempty"`
}
// WebhookEventToJSON 将 webhook event 转为自定义 JSON 结构(避免 protobuf 内部字段)
func WebhookEventToJSON(event *livekit.WebhookEvent) (*WebhookEventJSON, error) {
data, err := protojson.Marshal(event)
if err != nil {
return nil, err
}
var result WebhookEventJSON
if err := json.Unmarshal(data, &result); err != nil {
return nil, err
}
return &result, nil
}