forked from bbeyondllove/asr_server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
81 lines (70 loc) · 2.52 KB
/
main.go
File metadata and controls
81 lines (70 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"asr_server/config"
"asr_server/internal/bootstrap"
"asr_server/internal/logger"
"asr_server/internal/router"
)
func main() {
// 加载配置
if err := config.InitConfig("config.json"); err != nil {
logger.Errorf("Failed to load configuration:%v", err)
os.Exit(1)
}
// 设置日志级别
logger.InitLoggerFromConfig(logger.LoggingConfig{
Level: config.GlobalConfig.Logging.Level,
Format: config.GlobalConfig.Logging.Format,
Output: config.GlobalConfig.Logging.Output,
FilePath: config.GlobalConfig.Logging.FilePath,
MaxSize: config.GlobalConfig.Logging.MaxSize,
MaxBackups: config.GlobalConfig.Logging.MaxBackups,
MaxAge: config.GlobalConfig.Logging.MaxAge,
Compress: config.GlobalConfig.Logging.Compress,
})
logger.Infof("✅ Configuration loaded")
config.PrintConfig()
// 初始化所有依赖
deps, err := bootstrap.InitApp(&config.GlobalConfig)
if err != nil {
logger.Errorf("Failed to initialize app dependencies:%v", err)
os.Exit(1)
}
// 统一注册所有路由
r := router.NewRouter(deps)
// 创建HTTP服务器
server := &http.Server{
Addr: fmt.Sprintf("%s:%d", config.GlobalConfig.Server.Host, config.GlobalConfig.Server.Port),
Handler: deps.RateLimiter.Middleware(r),
ReadTimeout: time.Duration(config.GlobalConfig.Server.ReadTimeout) * time.Second,
}
// 优雅关闭
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-quit
logger.Infof("🛑 Shutting down server...")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
logger.Errorf("Server forced to shutdown:%v", err)
}
logger.Infof("✅ Server shutdown complete")
}()
logger.Infof("🌐 Listening on %s:%d", config.GlobalConfig.Server.Host, config.GlobalConfig.Server.Port)
logger.Infof("🔗 WebSocket: ws://%s:%d/ws", config.GlobalConfig.Server.Host, config.GlobalConfig.Server.Port)
logger.Infof("📊 Health check: http://%s:%d/health", config.GlobalConfig.Server.Host, config.GlobalConfig.Server.Port)
logger.Infof("📈 Statistics: http://%s:%d/stats", config.GlobalConfig.Server.Host, config.GlobalConfig.Server.Port)
logger.Infof("🧪 Test page: http://%s:%d/", config.GlobalConfig.Server.Host, config.GlobalConfig.Server.Port)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Errorf("Server error:%v", err)
os.Exit(1)
}
}