godns/main.go

112 lines
1.7 KiB
Go
Raw Normal View History

2013-07-23 11:10:38 +00:00
package main
import (
2020-01-25 17:43:02 +00:00
"flag"
"fmt"
"github.com/spf13/viper"
"meow.tf/joker/godns/log"
"meow.tf/joker/godns/settings"
2013-07-23 11:10:38 +00:00
"os"
"os/signal"
2013-07-25 04:32:57 +00:00
"runtime"
2015-10-14 07:21:21 +00:00
"runtime/pprof"
2013-07-23 11:10:38 +00:00
"time"
)
2020-01-25 17:43:02 +00:00
const (
Version = "0.3.0"
)
2013-07-23 11:10:38 +00:00
var (
2020-01-25 17:43:02 +00:00
cfgFile string
2013-07-23 11:10:38 +00:00
)
2020-01-25 17:43:02 +00:00
func init() {
flag.StringVar(&cfgFile, "config", "/etc/godns/godns.conf", "")
}
2013-07-23 11:10:38 +00:00
2020-01-25 17:43:02 +00:00
func main() {
initLogger()
2013-07-23 11:10:38 +00:00
2020-01-25 17:43:02 +00:00
viper.SetConfigFile(cfgFile)
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
serverSettings := settings.Server()
2013-07-23 11:10:38 +00:00
server := &Server{
2020-01-25 17:43:02 +00:00
host: serverSettings.Host,
port: serverSettings.Port,
2013-07-23 11:10:38 +00:00
rTimeout: 5 * time.Second,
wTimeout: 5 * time.Second,
}
server.Run()
2020-01-25 17:43:02 +00:00
log.Info("godns %s (%s) start", Version, runtime.Version())
2013-07-25 04:32:57 +00:00
2020-01-25 17:43:02 +00:00
if settings.Debug() {
2015-10-14 07:21:21 +00:00
go profileCPU()
go profileMEM()
}
2013-07-23 11:10:38 +00:00
sig := make(chan os.Signal)
signal.Notify(sig, os.Interrupt)
2020-01-25 17:43:02 +00:00
<- sig
log.Info("signal received, stopping")
2013-07-23 11:10:38 +00:00
}
2015-10-14 07:21:21 +00:00
func profileCPU() {
f, err := os.Create("godns.cprof")
if err != nil {
2020-01-25 17:43:02 +00:00
log.Error("%s", err)
2015-10-14 07:21:21 +00:00
return
}
pprof.StartCPUProfile(f)
time.AfterFunc(6*time.Minute, func() {
pprof.StopCPUProfile()
f.Close()
})
}
func profileMEM() {
f, err := os.Create("godns.mprof")
2020-01-25 17:43:02 +00:00
2015-10-14 07:21:21 +00:00
if err != nil {
2020-01-25 17:43:02 +00:00
log.Error("%s", err)
2015-10-14 07:21:21 +00:00
return
}
time.AfterFunc(5*time.Minute, func() {
pprof.WriteHeapProfile(f)
f.Close()
})
}
func initLogger() {
2020-01-25 17:43:02 +00:00
logSettings := settings.Log()
2020-01-25 17:43:02 +00:00
if viper.GetBool("log.stdout") {
log.SetLogger("console", nil)
}
2020-01-25 17:43:02 +00:00
if file := viper.GetString("log.file"); file != "" {
log.SetLogger("file", map[string]interface{}{"file": file})
2013-07-23 11:10:38 +00:00
}
2020-01-25 17:43:02 +00:00
log.SetLevel(logSettings.LogLevel())
2013-07-23 11:10:38 +00:00
}
2013-07-25 04:32:57 +00:00
func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
}