-
Notifications
You must be signed in to change notification settings - Fork 57
/
sdns.go
126 lines (94 loc) · 2.71 KB
/
sdns.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
//go:generate go run gen.go
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"runtime/debug"
"syscall"
"time"
"github.com/semihalev/log"
"github.com/semihalev/sdns/api"
"github.com/semihalev/sdns/config"
"github.com/semihalev/sdns/middleware"
"github.com/semihalev/sdns/server"
)
const version = "1.3.7"
var (
flagcfgpath string
flagprintver bool
cfg *config.Config
)
func init() {
flag.StringVar(&flagcfgpath, "config", "sdns.conf", "Location of the config file. If it doesn't exist, a new one will be generated.")
flag.StringVar(&flagcfgpath, "c", "sdns.conf", "Location of the config file. If it doesn't exist, a new one will be generated.")
flag.BoolVar(&flagprintver, "version", false, "Show the version of the sdns.")
flag.BoolVar(&flagprintver, "v", false, "Show the version of the sdns.")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage:\n sdns [OPTIONS]\n\n")
fmt.Fprintf(os.Stderr, "Options:\n")
fmt.Fprintf(os.Stderr, " -c, --config PATH\tLocation of the config file. If it doesn't exist, a new one will be generated.\n")
fmt.Fprintf(os.Stderr, " -v, --version\t\tShow the version of the sdns and exit.\n")
fmt.Fprintf(os.Stderr, " -h, --help\t\tShow this help and exit.\n\n")
fmt.Fprintf(os.Stderr, "Example:\n")
fmt.Fprintf(os.Stderr, " sdns -c sdns.conf\n\n")
}
}
func setup() {
var err error
if cfg, err = config.Load(flagcfgpath, version); err != nil {
log.Crit("Config loading failed", "error", err.Error())
}
if cfg.LogLevel == "" {
cfg.LogLevel = "info"
}
lvl, err := log.LvlFromString(cfg.LogLevel)
if err != nil {
log.Crit("Log verbosity level unknown")
}
log.Root().SetLevel(lvl)
log.Root().SetHandler(log.LvlFilterHandler(lvl, log.StdoutHandler))
middleware.Setup(cfg)
}
func run(ctx context.Context) *server.Server {
srv := server.New(cfg)
srv.Run(ctx)
api := api.New(cfg)
api.Run(ctx)
return srv
}
func printver() {
buildInfo, _ := debug.ReadBuildInfo()
settings := make(map[string]string)
for _, s := range buildInfo.Settings {
settings[s.Key] = s.Value
}
fmt.Fprintf(os.Stderr, "sdns v%s rev %.7s\nbuilt by %s (%s %s)\n", version,
settings["vcs.revision"], buildInfo.GoVersion, settings["GOOS"], settings["GOARCH"])
os.Exit(0)
}
func main() {
flag.Parse()
if flagprintver {
printver()
}
log.Info("Starting sdns...", "version", version)
setup()
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
srv := run(ctx)
<-ctx.Done()
log.Info("Stopping sdns...")
stop()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
for !srv.Stopped() {
select {
case <-time.After(100 * time.Millisecond):
continue
case <-ctx.Done():
return
}
}
}