2
0
mirror of https://github.com/namecoin/ncdns synced 2024-11-16 00:13:01 +00:00

Switch to using service management code.

This commit is contained in:
Hugo Landau 2014-11-14 08:51:57 +00:00
parent f05af9f321
commit 24c4850099
3 changed files with 48 additions and 17 deletions

View File

@ -68,7 +68,7 @@ func New(cfg *Config) (backend *Backend, err error) {
return return
} }
// Keep domains in DNS format. // Keep domains in parsed format.
type domain struct { type domain struct {
ncv *ncValue ncv *ncValue
} }

31
main.go
View File

@ -1,8 +1,10 @@
package main package main
import "github.com/hlandau/degoutils/config" import "github.com/hlandau/degoutils/config"
import "github.com/hlandau/degoutils/log"
import "github.com/hlandau/degoutils/daemon" //import "github.com/hlandau/degoutils/log"
//import "github.com/hlandau/degoutils/daemon"
import "github.com/hlandau/degoutils/service"
import "github.com/hlandau/ncdns/server" import "github.com/hlandau/ncdns/server"
func main() { func main() {
@ -13,7 +15,28 @@ func main() {
} }
config.ParseFatal(&cfg) config.ParseFatal(&cfg)
err := daemon.Init() service.Main(&service.Info{
Name: "ncdns",
Description: "Namecoin to DNS Daemon",
RunFunc: func(smgr service.Manager) error {
s, err := server.NewServer(&cfg)
if err != nil {
return err
}
err = s.Start()
if err != nil {
return err
}
smgr.SetStarted()
<-smgr.StopChan()
return nil
},
})
/* err := daemon.Init()
log.Fatale(err) log.Fatale(err)
if cfg.Daemonize { if cfg.Daemonize {
@ -27,7 +50,7 @@ func main() {
s, err := server.NewServer(&cfg) s, err := server.NewServer(&cfg)
log.Fatale(err) log.Fatale(err)
s.Run() s.Run()*/
} }
// © 2014 Hugo Landau <hlandau@devever.net> GPLv3 or later // © 2014 Hugo Landau <hlandau@devever.net> GPLv3 or later

View File

@ -9,6 +9,8 @@ import "fmt"
import "os/signal" import "os/signal"
import "syscall" import "syscall"
const version = "1.0"
type Server struct { type Server struct {
cfg ServerConfig cfg ServerConfig
@ -33,9 +35,9 @@ type ServerConfig struct {
SelfName string `default:"" usage:"Canonical name for this nameserver (default: autogenerated psuedo-hostname resolving to SelfIP; SelfIP is not used if this is set)"` SelfName string `default:"" usage:"Canonical name for this nameserver (default: autogenerated psuedo-hostname resolving to SelfIP; SelfIP is not used if this is set)"`
SelfIP string `default:"127.127.127.127" usage:"The canonical IP address for this service"` SelfIP string `default:"127.127.127.127" usage:"The canonical IP address for this service"`
Daemonize bool `default:"false" usage:"Daemonize (doesn't fork)"` //Daemonize bool `default:"false" usage:"Daemonize (doesn't fork)"`
UID int `default:"0" usage:"UID to drop privileges to if run as root"` //UID int `default:"0" usage:"UID to drop privileges to if run as root"`
GID int `default:"0" usage:"GID to drop privileges to if run as root"` //GID int `default:"0" usage:"GID to drop privileges to if run as root"`
} }
func NewServer(cfg *ServerConfig) (s *Server, err error) { func NewServer(cfg *ServerConfig) (s *Server, err error) {
@ -58,6 +60,7 @@ func NewServer(cfg *ServerConfig) (s *Server, err error) {
ecfg := &madns.EngineConfig{ ecfg := &madns.EngineConfig{
Backend: b, Backend: b,
VersionString: "ncdns/" + version,
} }
// key setup // key setup
@ -122,13 +125,18 @@ func (s *Server) loadKey(fn, privateFn string) (k *dns.DNSKEY, privatek dns.Priv
return return
} }
func (s *Server) Run() { func (s *Server) Start() error {
s.mux = dns.NewServeMux() s.mux = dns.NewServeMux()
s.mux.Handle(".", s.engine) s.mux.Handle(".", s.engine)
s.udpListener = s.runListener("udp") s.udpListener = s.runListener("udp")
s.tcpListener = s.runListener("tcp") s.tcpListener = s.runListener("tcp")
return nil
}
func (s *Server) Run() {
s.Start()
log.Info("Ready.") log.Info("Ready.")
// wait // wait