-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
50 lines (46 loc) · 1.06 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
package main
import (
"context"
"fmt"
zabbix "github.com/0xdeface/zabbix/sender"
"log"
"os"
"os/signal"
"zabbix-http/config"
"zabbix-http/internal/http"
)
func main() {
cfg := config.GetConfig()
errCh := make(chan error, 10)
zabbixSender := zabbix.NewZabbixSender(cfg.ServerAddr, cfg.ZabbixPort)
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
//ctx, cancel := context.WithTimeout(ctx, time.Second * 1)
go startZabbixSender(ctx, zabbixSender)
go startHttpServer(ctx, cfg.HttpPort, zabbixSender.MsgCh, errCh)
go func() {
for {
select {
case e := <-errCh:
fmt.Println(e)
case e := <-zabbixSender.ErrCh:
fmt.Println(e)
case <-ctx.Done():
return
}
}
}()
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
<-stop
cancel()
}
func startZabbixSender(ctx context.Context, s *zabbix.Sender) {
err := s.Start(ctx)
if err != nil {
log.Fatalln(err)
}
}
func startHttpServer(ctx context.Context, port string, msgCh chan zabbix.Message, errCh chan error) {
http.RunServer(ctx, port, msgCh, errCh)
}