1
package main
2

3
//go:generate go run gen.go
4

5
import (
6
	"flag"
7
	"fmt"
8
	"os"
9
	"os/signal"
10
	"runtime"
11

12
	"github.com/semihalev/log"
13
	"github.com/semihalev/sdns/api"
14
	"github.com/semihalev/sdns/config"
15
	"github.com/semihalev/sdns/middleware"
16
	"github.com/semihalev/sdns/server"
17
)
18

19
const version = "1.1.6"
20

21
var (
22
	flagcfgpath  = flag.String("config", "sdns.conf", "location of the config file, if config file not found, a config will generate")
23
	flagprintver = flag.Bool("v", false, "show version information")
24

25
	cfg *config.Config
26
)
27

28
func init() {
29 1
	runtime.GOMAXPROCS(runtime.NumCPU())
30

31
	flag.Usage = func() {
32 1
		fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS]\n\n", os.Args[0])
33 1
		fmt.Fprintln(os.Stderr, "Options:")
34 1
		flag.PrintDefaults()
35 1
		fmt.Fprintln(os.Stderr, "")
36 1
		fmt.Fprintln(os.Stderr, "Example:")
37 1
		fmt.Fprintf(os.Stderr, "%s -config=sdns.conf\n", os.Args[0])
38 1
		fmt.Fprintln(os.Stderr, "")
39
	}
40
}
41

42
func setup() {
43 1
	var err error
44

45 1
	if cfg, err = config.Load(*flagcfgpath, version); err != nil {
46 0
		log.Crit("Config loading failed", "error", err.Error())
47
	}
48

49 1
	if cfg.LogLevel == "" {
50 0
		cfg.LogLevel = "info"
51
	}
52

53 1
	lvl, err := log.LvlFromString(cfg.LogLevel)
54 1
	if err != nil {
55 0
		log.Crit("Log verbosity level unknown")
56
	}
57

58 1
	log.Root().SetLevel(lvl)
59 1
	log.Root().SetHandler(log.LvlFilterHandler(lvl, log.StdoutHandler))
60

61 1
	middleware.Setup(cfg)
62
}
63

64
func run() {
65 1
	server := server.New(cfg)
66 1
	server.Run()
67

68 1
	api := api.New(cfg)
69 1
	api.Run()
70
}
71

72
func main() {
73 0
	flag.Parse()
74

75 0
	if *flagprintver {
76 0
		println("SDNS v" + version)
77 0
		os.Exit(0)
78
	}
79

80 0
	log.Info("Starting sdns...", "version", version)
81

82 0
	setup()
83 0
	run()
84

85 0
	c := make(chan os.Signal, 1)
86 0
	signal.Notify(c, os.Interrupt)
87

88 0
	<-c
89

90 0
	log.Info("Stopping sdns...")
91
}

Read our documentation on viewing source code .

Loading