feat: refactor routing structure by moving routes to a dedicated package and updating service initialization

This commit is contained in:
王锦强
2025-03-14 20:47:37 +08:00
parent 39c776998e
commit cab289dabf
2 changed files with 4 additions and 3 deletions

View File

@@ -9,6 +9,7 @@ import (
"syscall"
"time"
"tts/internal/config"
"tts/internal/http/routes"
)
// App 表示整个TTS应用程序
@@ -26,13 +27,13 @@ func NewApp(configPath string) (*App, error) {
}
// 初始化服务
ttsService, err := InitializeServices(cfg)
ttsService, err := routes.InitializeServices(cfg)
if err != nil {
return nil, fmt.Errorf("初始化服务失败: %w", err)
}
// 设置路由
handler, err := SetupRoutes(cfg, ttsService)
handler, err := routes.SetupRoutes(cfg, ttsService)
if err != nil {
return nil, fmt.Errorf("设置路由失败: %w", err)
}

View File

@@ -1,72 +0,0 @@
package server
import (
"net/http"
"tts/internal/config"
"tts/internal/http/handlers"
"tts/internal/http/middleware"
"tts/internal/tts"
"tts/internal/tts/microsoft"
)
// SetupRoutes 配置所有API路由
func SetupRoutes(cfg *config.Config, ttsService tts.Service) (http.Handler, error) {
// 创建一个新的路由多路复用器
mux := http.NewServeMux()
// 创建处理器
ttsHandler := handlers.NewTTSHandler(ttsService, cfg)
voicesHandler := handlers.NewVoicesHandler(ttsService)
// 创建页面处理器
pagesHandler, err := handlers.NewPagesHandler("./web/templates", cfg)
if err != nil {
return nil, err
}
// 设置主页路由
mux.HandleFunc("/", pagesHandler.HandleIndex)
// 设置API文档路由
mux.HandleFunc("/api-doc", pagesHandler.HandleAPIDoc)
// 设置TTS API路由 - 添加认证中间件
ttsHandlerFunc := http.HandlerFunc(ttsHandler.HandleTTS)
authenticatedTTSHandler := middleware.TTSAuth(cfg.TTS.ApiKey, ttsHandlerFunc)
mux.Handle("/tts", authenticatedTTSHandler)
// 设置语音列表API路由
mux.HandleFunc("/voices", voicesHandler.HandleVoices)
// 创建OpenAI兼容接口的处理器添加验证中间件
openAIHandler := http.HandlerFunc(ttsHandler.HandleOpenAITTS)
authenticatedHandler := middleware.OpenAIAuth(cfg.OpenAI.ApiKey, openAIHandler)
// 应用OpenAI兼容的路由
mux.Handle("/v1/audio/speech", authenticatedHandler)
mux.Handle("/audio/speech", authenticatedHandler)
// 设置静态文件服务
fs := http.FileServer(http.Dir("./web/static"))
mux.Handle("/static/", http.StripPrefix("/static/", fs))
// 应用基础路径前缀
var handler http.Handler = mux
if cfg.Server.BasePath != "" {
handler = http.StripPrefix(cfg.Server.BasePath, mux)
}
// 应用中间件
handler = middleware.Logger(handler) // 日志中间件
handler = middleware.CORS(handler) // CORS中间件
return handler, nil
}
// InitializeServices 初始化所有服务
func InitializeServices(cfg *config.Config) (tts.Service, error) {
// 创建Microsoft TTS客户端
ttsClient := microsoft.NewClient(cfg)
return ttsClient, nil
}