-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
64 lines (48 loc) · 1.16 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
package main
import (
"flag"
"fmt"
"os"
"udp-web-logger/pkg/server"
)
var usage = `
Usage: udp-web-logger [options].
Options:
--udp-read-buffer-size size of buffer to read incoming UDP packet into. Default: 4096.
--max-messages maximum amount of messages to keep. Default: 50.
--help prints this message.
Env:
UDP_LISTEN - address to listen UDP on. Default: 127.0.0.1:9010.
WEB_LISTEN - address to listen HTTP on. Default: 127.0.0.1:9000.
`
func main() {
udpReadBufferSize := flag.Int("udp-read-buffer-size", 4096, "")
maxMessages := flag.Int("max-messages", 50, "")
udpListen := os.Getenv("UDP_LISTEN")
webListen := os.Getenv("WEB_LISTEN")
flag.Usage = func() {
fmt.Print(usage)
}
flag.Parse()
if udpListen == "" {
udpListen = "127.0.0.1:9010"
}
if webListen == "" {
webListen = "127.0.0.1:9000"
}
udp := server.NewUDPServer(udpListen, nil)
web := server.NewHTTPServer(webListen, *maxMessages, nil)
defer udp.Shutdown()
defer web.Shutdown()
go web.Serve()
go func() {
for {
message, err := udp.ReadMessage(*udpReadBufferSize)
if err != nil {
continue
}
web.AddMessage(message)
}
}()
select {}
}