Files
Cyrene/backend/voice-service/internal/handler/tts_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

105 lines
2.8 KiB
Go

package handler
import (
"encoding/json"
"git.yeij.top/AskaEth/Cyrene/pkg/logger"
"net/http"
"git.yeij.top/AskaEth/Cyrene/voice-service/internal/service"
)
// TTSHandler TTS HTTP API 处理器
type TTSHandler struct {
svc *service.TTSService
}
// NewTTSHandler 创建 TTS 处理器
func NewTTSHandler(svc *service.TTSService) *TTSHandler {
return &TTSHandler{svc: svc}
}
// RegisterRoutes 注册 TTS 路由
func (h *TTSHandler) RegisterRoutes(mux *http.ServeMux) {
mux.HandleFunc("/api/v1/tts/synthesize", h.handleSynthesize)
mux.HandleFunc("/api/v1/tts/voices", h.handleVoices)
mux.HandleFunc("/api/v1/tts/status", h.handleStatus)
}
// TTSSynthesizeRequest TTS 合成请求体
type TTSSynthesizeRequest struct {
Text string `json:"text"`
Voice string `json:"voice"`
Rate string `json:"rate"`
}
// handleSynthesize POST /api/v1/tts/synthesize
func (h *TTSHandler) handleSynthesize(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
writeError(w, http.StatusMethodNotAllowed, "method not allowed")
return
}
// 解析 JSON 请求体
var req TTSSynthesizeRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, "请求体解析失败: "+err.Error())
return
}
if req.Text == "" {
writeError(w, http.StatusBadRequest, "text 字段不能为空")
return
}
// 调用合成 (Synthesize 内部已包含 fallback 链: edge-tts → espeak-ng → 静默 WAV)
audioData, format, err := h.svc.Synthesize(req.Text, req.Voice, req.Rate)
if err != nil {
logger.Printf("[tts-handler] TTS 合成失败: %v", err)
writeError(w, http.StatusInternalServerError, "TTS 合成失败: "+err.Error())
return
}
// 返回音频流
contentType := "audio/mpeg"
if format == "wav" {
contentType = "audio/wav"
}
w.Header().Set("Content-Type", contentType)
w.Header().Set("Content-Disposition", "inline; filename=synthesized."+format)
w.WriteHeader(http.StatusOK)
w.Write(audioData)
}
// handleVoices GET /api/v1/tts/voices
func (h *TTSHandler) handleVoices(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
writeError(w, http.StatusMethodNotAllowed, "method not allowed")
return
}
voices := h.svc.GetVoices()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]interface{}{
"voices": voices,
"count": len(voices),
})
}
// handleStatus GET /api/v1/tts/status
func (h *TTSHandler) handleStatus(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
writeError(w, http.StatusMethodNotAllowed, "method not allowed")
return
}
status := h.svc.GetEngineStatus()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]interface{}{
"service": "voice-service",
"tts": status,
})
}