-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathynetd.go
89 lines (71 loc) · 1.51 KB
/
ynetd.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
85
86
87
88
89
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"github.com/rwstauner/ynetd/config"
"github.com/rwstauner/ynetd/procman"
"github.com/rwstauner/ynetd/service"
)
// Version is the program version, filled in from git during build process.
var Version string
var logger = log.New(os.Stdout, "ynetd ", log.Ldate|log.Ltime|log.Lmicroseconds)
func setupSignals(pm *procman.ProcessManager) {
channel := make(chan os.Signal, 1)
signals := []os.Signal{syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM}
for _, sig := range signals {
if !signal.Ignored(sig) {
signal.Notify(channel, sig)
}
}
pm.Signal(<-channel)
}
var printVersion = false
func init() {
flag.BoolVar(&printVersion, "version", printVersion, "Print version")
procman.SetLogger(logger)
service.SetLogger(logger)
}
func main() {
os.Exit(frd())
}
func frd() int {
flag.Parse()
if printVersion {
fmt.Println("ynetd", Version)
return 0
}
cfg, err := config.Load(flag.Args())
if err != nil {
fmt.Println(err)
return 1
}
pm := procman.New()
var services []*service.Service
for _, sc := range cfg.Services {
service, err := service.New(sc, pm)
if err != nil {
fmt.Println(err)
return 1
}
services = append(services, service)
}
if len(services) == 0 {
fmt.Println("no services configured!")
return 1
}
for _, svc := range services {
err := svc.Listen()
if err != nil {
fmt.Printf("error starting listener: %s\n", err)
return 1
}
}
go setupSignals(pm)
// block
pm.Manage()
return 0
}