-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (66 loc) · 1.83 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// main.go
package main
import (
"encoding/json"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/BurntSushi/toml"
"github.com/devops-fj/collectDataUpload/config"
"github.com/devops-fj/collectDataUpload/logger"
"github.com/devops-fj/collectDataUpload/metrics"
"github.com/devops-fj/collectDataUpload/plugin"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func main() {
// 初始化配置
cfg := initConfig()
// 初始化日志管理器
logger.InitLogger(cfg.Log.Level, cfg.Log.Output)
// 初始化 Metrics
metric := metrics.NewMetrics()
metric.Register()
// 初始化 ReporterMetrics
reporterMetrics := metrics.NewReporterMetrics()
reporterMetrics.Register()
// 启动 Metrics HTTP 服务器
go func() {
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":2112", nil)
}()
// 创建插件管理器
pm := plugin.NewPluginManager(cfg, metric, reporterMetrics)
// 注册所有插件
pm.RegisterPlugins()
// 启动插件
pm.StartPlugins()
// 注册插件管理器的 HTTP 处理器
http.HandleFunc("/plugins", func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
// 获取所有插件的数据
data := pm.GetPluginsData()
// 将数据转换为 JSON 格式并写入响应
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(data)
} else {
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte("Method not allowed"))
}
})
// 等待中断信号以优雅地关闭插件
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
<-sigCh
// 停止插件
pm.StopPlugins()
}
// 初始化配置文件
func initConfig() *config.Config {
var cfg config.Config
// 读取配置文件
if _, err := toml.DecodeFile("config/config.toml", &cfg); err != nil {
panic(err)
}
return &cfg
}