feat: 重构项目以符合 Go 规范,添加 OpenAI 接口适配,优化长文本朗读功能(切割后合并)
This commit is contained in:
83
internal/http/server/app.go
Normal file
83
internal/http/server/app.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
"tts/internal/config"
|
||||
)
|
||||
|
||||
// App 表示整个TTS应用程序
|
||||
type App struct {
|
||||
server *Server
|
||||
cfg *config.Config
|
||||
}
|
||||
|
||||
// NewApp 创建一个新的应用程序实例
|
||||
func NewApp(configPath string) (*App, error) {
|
||||
// 加载配置
|
||||
cfg, err := config.Load(configPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("加载配置失败: %w", err)
|
||||
}
|
||||
|
||||
// 初始化服务
|
||||
ttsService, err := InitializeServices(cfg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("初始化服务失败: %w", err)
|
||||
}
|
||||
|
||||
// 设置路由
|
||||
handler, err := SetupRoutes(cfg, ttsService)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("设置路由失败: %w", err)
|
||||
}
|
||||
|
||||
// 创建HTTP服务器
|
||||
server := New(cfg, handler)
|
||||
|
||||
return &App{
|
||||
server: server,
|
||||
cfg: cfg,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Start 启动应用程序
|
||||
func (a *App) Start() error {
|
||||
// 创建一个错误通道
|
||||
errChan := make(chan error, 1)
|
||||
|
||||
// 创建一个退出信号通道
|
||||
quit := make(chan os.Signal, 1)
|
||||
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
// 在一个goroutine中启动服务器
|
||||
go func() {
|
||||
log.Printf("启动TTS服务,监听端口 %d...\n", a.cfg.Server.Port)
|
||||
errChan <- a.server.Start()
|
||||
}()
|
||||
|
||||
// 等待退出信号或错误
|
||||
select {
|
||||
case err := <-errChan:
|
||||
return err
|
||||
case <-quit:
|
||||
log.Println("接收到退出信号,正在优雅关闭...")
|
||||
|
||||
// 创建一个超时上下文用于优雅关闭
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// 尝试优雅关闭服务器
|
||||
if err := a.server.Shutdown(ctx); err != nil {
|
||||
return fmt.Errorf("服务器关闭出错: %w", err)
|
||||
}
|
||||
|
||||
log.Println("服务器已优雅关闭")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
65
internal/http/server/routes.go
Normal file
65
internal/http/server/routes.go
Normal file
@@ -0,0 +1,65 @@
|
||||
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路由
|
||||
mux.HandleFunc("/tts", ttsHandler.HandleTTS)
|
||||
|
||||
// 设置语音列表API路由
|
||||
mux.HandleFunc("/voices", voicesHandler.HandleVoices)
|
||||
|
||||
mux.HandleFunc("/v1/audio/speech", ttsHandler.HandleOpenAITTS)
|
||||
mux.HandleFunc("/audio/speech", ttsHandler.HandleOpenAITTS)
|
||||
|
||||
// 设置静态文件服务
|
||||
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
|
||||
}
|
||||
45
internal/http/server/server.go
Normal file
45
internal/http/server/server.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"tts/internal/config"
|
||||
)
|
||||
|
||||
// Server 封装HTTP服务器
|
||||
type Server struct {
|
||||
server *http.Server
|
||||
basePath string
|
||||
}
|
||||
|
||||
// New 创建新的HTTP服务器
|
||||
func New(cfg *config.Config, handler http.Handler) *Server {
|
||||
// 创建HTTP服务器
|
||||
httpServer := &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", cfg.Server.Port),
|
||||
Handler: handler,
|
||||
ReadTimeout: time.Duration(cfg.Server.ReadTimeout) * time.Second,
|
||||
WriteTimeout: time.Duration(cfg.Server.WriteTimeout) * time.Second,
|
||||
IdleTimeout: 120 * time.Second,
|
||||
}
|
||||
|
||||
return &Server{
|
||||
server: httpServer,
|
||||
basePath: cfg.Server.BasePath,
|
||||
}
|
||||
}
|
||||
|
||||
// Start 启动HTTP服务器
|
||||
func (s *Server) Start() error {
|
||||
fmt.Printf("服务启动在 %s\n", s.server.Addr)
|
||||
return s.server.ListenAndServe()
|
||||
}
|
||||
|
||||
// Shutdown 优雅关闭服务器
|
||||
func (s *Server) Shutdown(ctx context.Context) error {
|
||||
fmt.Println("正在关闭HTTP服务器...")
|
||||
return s.server.Shutdown(ctx)
|
||||
}
|
||||
Reference in New Issue
Block a user