-
Notifications
You must be signed in to change notification settings - Fork 22
/
main.go
69 lines (56 loc) · 1.51 KB
/
main.go
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
package main
import (
"context"
"net/http"
"github.com/GoSimplicity/LinkMe/internal/domain/events"
"github.com/GoSimplicity/LinkMe/config"
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
)
func main() {
Init()
}
func Init() {
// 初始化配置
config.InitViper()
// 初始化 Web 服务器和其他组件
cmd := InitWebServer()
// 设置请求头打印路由
server := cmd.server
server.GET("/headers", printHeaders)
// 启动 Prometheus 监控
go func() {
if err := startMetricsServer(); err != nil {
zap.L().Error("Failed to start metrics server", zap.Error(err))
}
}()
// 启动消费者
for _, s := range cmd.consumer {
go func(consumer events.Consumer) { // 将每个消费者启动放入goroutine中并发执行
if err := consumer.Start(context.Background()); err != nil {
zap.L().Error("Failed to start consumer", zap.Error(err))
}
}(s)
}
// 启动定时任务
cmd.Cron.Start()
// 启动 Web 服务器
if err := server.Run(":9999"); err != nil {
zap.L().Fatal("Failed to start web server", zap.Error(err))
}
}
// printHeaders 打印请求头信息
func printHeaders(c *gin.Context) {
headers := c.Request.Header
for key, values := range headers {
for _, value := range values {
c.String(http.StatusOK, "%s: %s\n", key, value)
}
}
}
// startMetricsServer 启动 Prometheus 监控服务器
func startMetricsServer() error {
http.Handle("/metrics", promhttp.Handler())
return http.ListenAndServe(":9090", nil)
}