-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
79 lines (69 loc) · 1.96 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
package main
import (
"fmt"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"github.com/pyke369/golang-support/uconfig"
"github.com/pyke369/golang-support/ulog"
)
const PROGNAME = "tcpsplice"
const VERSION = "1.4.1"
var (
Config *uconfig.UConfig
Logger *ulog.ULog
Started time.Time
Unbind bool
)
func main() {
var err error
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "usage: %s <configuration file>\n", filepath.Base(os.Args[0]))
os.Exit(1)
}
if Config, err = uconfig.New(os.Args[1]); err != nil {
fmt.Fprintf(os.Stderr, "configuration file syntax error: %s - aborting\n", err)
os.Exit(2)
}
Logger = ulog.New(Config.GetString(PROGNAME+".log", "console(output=stdout)"))
Logger.Info(map[string]any{
"scope": "main", "event": "start", "version": VERSION, "config": os.Args[1],
"pid": os.Getpid(), "services": len(Config.GetPaths(PROGNAME + ".service")),
})
Started = time.Now()
go ServiceRun()
go MonitorRun()
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGHUP, syscall.SIGUSR1)
for {
signal := <-signals
switch {
case signal == syscall.SIGHUP:
if _, err = uconfig.New(os.Args[1]); err == nil {
Config.Load(os.Args[1])
Logger.Load(Config.GetString(PROGNAME+".log", "console(output=stdout)"))
Logger.Info(map[string]any{
"scope": "main", "event": "reload", "version": VERSION, "config": os.Args[1],
"pid": os.Getpid(), "services": len(Config.GetPaths(PROGNAME + ".service")),
})
} else {
Logger.Warn(map[string]any{
"scope": "main", "event": "reload", "config": os.Args[1], "error": fmt.Sprintf("%v", err),
})
}
case signal == syscall.SIGUSR1:
Unbind = !Unbind
if Unbind {
Logger.Info(map[string]any{
"scope": "main", "event": "unbind", "services": len(Config.GetPaths(PROGNAME + ".service")),
})
} else {
Logger.Info(map[string]any{
"scope": "main", "event": "rebind", "services": len(Config.GetPaths(PROGNAME + ".service")),
})
}
}
}
}