2013-07-23 11:10:38 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2013-07-25 04:32:57 +00:00
|
|
|
"runtime"
|
2013-07-23 11:10:38 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
logger *log.Logger
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
logger = initLogger(settings.Log.File)
|
|
|
|
|
|
|
|
server := &Server{
|
|
|
|
host: settings.Server.Host,
|
|
|
|
port: settings.Server.Port,
|
|
|
|
rTimeout: 5 * time.Second,
|
|
|
|
wTimeout: 5 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
server.Run()
|
|
|
|
|
2013-07-25 04:32:57 +00:00
|
|
|
logger.Printf("godns %s start", settings.Version)
|
|
|
|
|
2013-07-23 11:10:38 +00:00
|
|
|
sig := make(chan os.Signal)
|
|
|
|
signal.Notify(sig, os.Interrupt)
|
|
|
|
|
|
|
|
forever:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-sig:
|
|
|
|
logger.Printf("signal received, stopping")
|
|
|
|
break forever
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func Debug(format string, v ...interface{}) {
|
|
|
|
if settings.Debug {
|
|
|
|
logger.Printf(format, v...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func initLogger(log_file string) (logger *log.Logger) {
|
|
|
|
if log_file != "" {
|
|
|
|
f, err := os.Create(log_file)
|
|
|
|
if err != nil {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
logger = log.New(f, "[godns]", log.Ldate|log.Ltime)
|
|
|
|
} else {
|
|
|
|
logger = log.New(os.Stdout, "[godns]", log.Ldate|log.Ltime)
|
|
|
|
}
|
|
|
|
return logger
|
|
|
|
|
|
|
|
}
|
2013-07-25 04:32:57 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
|
|
|
}
|