-
Notifications
You must be signed in to change notification settings - Fork 3
/
302.go
104 lines (91 loc) · 2.93 KB
/
302.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"flag"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/juju/loggo"
"github.com/mirrorz-org/mirrorz-302/pkg/geo"
"github.com/mirrorz-org/mirrorz-302/pkg/server"
"sigs.k8s.io/yaml"
)
var logger = loggo.GetLogger("<root>")
func LoadConfig(path string) (config server.Config, err error) {
file, err := os.ReadFile(path)
if err != nil {
logger.Errorf("LoadConfig ReadFile failed: %v\n", err)
return
}
err = yaml.Unmarshal(file, &config)
if err != nil {
logger.Errorf("LoadConfig json Unmarshal failed: %v\n", err)
return
}
logger.Debugf("LoadConfig InfluxDB URL: %s\n", config.InfluxDB.URL)
logger.Debugf("LoadConfig InfluxDB Org: %s\n", config.InfluxDB.Org)
logger.Debugf("LoadConfig InfluxDB Bucket: %s\n", config.InfluxDB.Bucket)
logger.Debugf("LoadConfig IPDB File: %s\n", config.IPDBFile)
logger.Debugf("LoadConfig HTTP Bind Address: %s\n", config.HTTPBindAddress)
logger.Debugf("LoadConfig MirrorZ D Directory: %s\n", config.MirrorZDDirectory)
logger.Debugf("LoadConfig Homepage: %s\n", config.Homepage)
logger.Debugf("LoadConfig Domain Length: %d\n", config.DomainLength)
logger.Debugf("LoadConfig Cache Time: %d\n", config.CacheTime)
logger.Debugf("LoadConfig Log Directory: %s\n", config.LogDirectory)
return
}
func main() {
configPtr := flag.String("config", "config.yml", "path to config file")
debugPtr := flag.Bool("debug", false, "debug mode")
flag.Parse()
if *debugPtr {
loggo.ConfigureLoggers("<root>=DEBUG")
} else {
loggo.ConfigureLoggers("<root>=INFO")
}
config, err := LoadConfig(*configPtr)
if err != nil {
logger.Errorf("Cannot open config file: %v\n", err)
os.Exit(1)
}
if config.IPDBFile != "" {
geo.LoadIPDB(config.IPDBFile)
}
s := server.NewServer(config)
if err := s.LoadMirrorZD(); err != nil {
logger.Errorf("Cannot load mirrorz.d.json: %v\n", err)
os.Exit(1)
}
// Logfile (or its directory) must be unprivilegd
err = s.InitLoggers()
if err != nil {
logger.Errorf("Cannot open log file: %v\n", err)
os.Exit(1)
}
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel, syscall.SIGHUP, syscall.SIGUSR1, syscall.SIGUSR2, syscall.SIGWINCH)
go func() {
for sig := range signalChannel {
switch sig {
case syscall.SIGHUP:
logger.Infof("Got A HUP Signal! Now Reloading mirrorz.d.json....\n")
s.LoadMirrorZD()
case syscall.SIGUSR1:
logger.Infof("Got A USR1 Signal! Now Reloading config.json....\n")
LoadConfig(*configPtr)
case syscall.SIGUSR2:
logger.Infof("Got A USR2 Signal! Now Reopen log file....\n")
err := s.InitLoggers()
if err != nil {
logger.Errorf("Error reopening log file: %v\n", err)
}
case syscall.SIGWINCH:
logger.Infof("Got A WINCH Signal! Now Flush Resolved....\n")
s.CachePurge()
}
}
}()
s.StartResolvedTicker()
logger.Infof("Starting HTTP server on %s\n", config.HTTPBindAddress)
logger.Errorf("HTTP Server error: %v\n", http.ListenAndServe(config.HTTPBindAddress, s))
}