-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
72 lines (59 loc) · 1.46 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
package main
import (
"fmt"
"log"
"net"
"os"
"gopkg.in/yaml.v2"
)
type LogPipe struct {
MetricLoggerQueueSize int `yaml:"metric_logger_queue_size"`
MetricFlushInterval int `yaml:"metric_flush_interval"`
ElasticServerHost string `yaml:"elastic_server_host"`
ElasticIndexName string `yaml:"elastic_index_name"`
ElasticUsername string `yaml:"elastic_username"`
ElasticPassword string `yaml:"elastic_password"`
}
func main() {
lp := LogPipe{}
fbytes, ferr := os.ReadFile("config.yaml")
if ferr == nil {
err := yaml.Unmarshal(fbytes, &lp)
if err != nil {
log.Println("Failed Unmarshal data", err)
}
MLogger.StartLogger(lp.MetricLoggerQueueSize, 1,
lp.ElasticServerHost, lp.ElasticIndexName,
lp.ElasticUsername, lp.ElasticPassword)
listenOnSocket("./sock/logPipe.sock")
} else {
log.Println("Could not read config.yaml")
}
}
func listenOnSocket(socketFile string) {
os.Remove(socketFile)
listener, err := net.Listen("unix", socketFile)
if err != nil {
fmt.Println("Error creating listener:", err)
return
}
for {
conn, err := listener.Accept()
if err != nil {
fmt.Println("Error accepting connection:", err)
continue
}
go handleConnection(conn)
}
}
func handleConnection(conn net.Conn) {
defer conn.Close()
buf := make([]byte, 2048)
_, err := conn.Read(buf)
if err != nil {
log.Println("Error reading from connection:", err)
return
}
logStr := string(buf)
MLogger.AddLogEntry(&logStr)
}