-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdaemon.go
50 lines (43 loc) · 1.2 KB
/
daemon.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 (
"fmt"
"github.com/fever-ch/go-google-sites-proxy/common/config"
"github.com/fever-ch/go-google-sites-proxy/proxy"
log "github.com/sirupsen/logrus"
"os"
"os/signal"
"path/filepath"
"syscall"
)
func startDaemonFromFile(confFile string) {
os.Chdir(filepath.Dir(confFile))
startDaemon(config.NewYamlConfigLoader(confFile))
}
func startDaemon(cl config.ConfigLoader) {
if cfg, err := cl(); err != nil {
log.WithError(err).Fatal("Unable to load configuration")
} else {
proxy := proxy.NewSmartProxy(cfg.Port())
proxy.SetConfiguration(cfg)
startUp := func() {
if err = proxy.Start(); err != nil {
log.WithError(err).Fatal("Unable to start proxy")
}
}
go startUp()
for true {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGUSR1)
<-c
if cfg, err := cl(); err != nil {
log.WithError(err).Warn("Unable to load config")
} else if cfg.Port() != proxy.Port() {
log.Warning(fmt.Sprintf("Server currently running on port %d but config specifies %d. This change will "+
"not be taken in account. Please restart daemon.", proxy.Port(), cfg.Port()))
} else {
proxy.SetConfiguration(cfg)
log.Info("Configuration reloaded")
}
}
}
}