Permalink
Browse files

Start a Server type to contain the running server state

  • Loading branch information...
abh committed Jul 10, 2016
1 parent 4d60839 commit dc6fbb83b08ce69aaf4305d44ae08529430cd2c6
Showing with 73 additions and 58 deletions.
  1. +8 −6 geodns.go
  2. +2 −1 monitor_test.go
  3. +0 −24 serve.go
  4. +6 −4 serve_test.go
  5. +46 −0 server.go
  6. +5 −19 zones.go
  7. +6 −4 zones_test.go
View
@@ -94,6 +94,8 @@ func main() {
os.Exit(0)
}
srv := Server{}
if len(*flagLogFile) > 0 {
logToFileOpen(*flagLogFile)
}
@@ -118,8 +120,8 @@ func main() {
}
Zones := make(Zones)
setupPgeodnsZone(Zones)
err = zonesReadDir(dirName, Zones)
srv.setupPgeodnsZone(Zones)
err = srv.zonesReadDir(dirName, Zones)
if err != nil {
log.Println("Errors reading zones", err)
os.Exit(2)
@@ -182,14 +184,14 @@ func main() {
go monitor(Zones)
go Zones.statHatPoster()
setupRootZone()
setupPgeodnsZone(Zones)
srv.setupRootZone()
srv.setupPgeodnsZone(Zones)
dirName := *flagconfig
go zonesReader(dirName, Zones)
go srv.zonesReader(dirName, Zones)
for _, host := range inter {
go listenAndServe(host)
go srv.listenAndServe(host)
}
terminate := make(chan os.Signal)
View
@@ -29,7 +29,8 @@ func (s *MonitorSuite) SetUpSuite(c *C) {
// TODO: use httptest
// https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/Jk785WB7F8I
zonesReadDir("dns", s.zones)
srv := Server{}
srv.zonesReadDir("dns", s.zones)
go httpHandler(s.zones)
time.Sleep(500 * time.Millisecond)
}
View
@@ -205,27 +205,3 @@ func statusRR(label string) []dns.RR {
return []dns.RR{&dns.TXT{Hdr: h, Txt: []string{string(js)}}}
}
func setupServerFunc(Zone *Zone) func(dns.ResponseWriter, *dns.Msg) {
return func(w dns.ResponseWriter, r *dns.Msg) {
serve(w, r, Zone)
}
}
func listenAndServe(ip string) {
prots := []string{"udp", "tcp"}
for _, prot := range prots {
go func(p string) {
server := &dns.Server{Addr: ip, Net: p}
log.Printf("Opening on %s %s", ip, p)
if err := server.ListenAndServe(); err != nil {
log.Fatalf("geodns: failed to setup %s %s: %s", ip, p, err)
}
log.Fatalf("geodns: ListenAndServe unexpectedly returned")
}(prot)
}
}
View
@@ -26,14 +26,16 @@ func (s *ServeSuite) SetUpSuite(c *C) {
metrics := NewMetrics()
go metrics.Updater()
srv := Server{}
Zones := make(Zones)
setupPgeodnsZone(Zones)
setupRootZone()
zonesReadDir("dns", Zones)
srv.setupPgeodnsZone(Zones)
srv.setupRootZone()
srv.zonesReadDir("dns", Zones)
// listenAndServe returns after listening on udp + tcp, so just
// wait for it before continuing
listenAndServe(PORT)
srv.listenAndServe(PORT)
// ensure service has properly started before we query it
time.Sleep(200 * time.Millisecond)
View
@@ -0,0 +1,46 @@
package main
import (
"log"
"time"
)
import "github.com/miekg/dns"
type Server struct{}
func (srv *Server) setupServerFunc(Zone *Zone) func(dns.ResponseWriter, *dns.Msg) {
return func(w dns.ResponseWriter, r *dns.Msg) {
serve(w, r, Zone)
}
}
func (srv *Server) listenAndServe(ip string) {
prots := []string{"udp", "tcp"}
for _, prot := range prots {
go func(p string) {
server := &dns.Server{Addr: ip, Net: p}
log.Printf("Opening on %s %s", ip, p)
if err := server.ListenAndServe(); err != nil {
log.Fatalf("geodns: failed to setup %s %s: %s", ip, p, err)
}
log.Fatalf("geodns: ListenAndServe unexpectedly returned")
}(prot)
}
}
func (srv *Server) addHandler(zones Zones, name string, config *Zone) {
oldZone := zones[name]
config.SetupMetrics(oldZone)
zones[name] = config
dns.HandleFunc(name, srv.setupServerFunc(config))
}
func (srv *Server) zonesReader(dirName string, zones Zones) {
for {
srv.zonesReadDir(dirName, zones)
time.Sleep(5 * time.Second)
}
}
View
@@ -30,21 +30,7 @@ type ZoneReadRecord struct {
var lastRead = map[string]*ZoneReadRecord{}
func zonesReader(dirName string, zones Zones) {
for {
zonesReadDir(dirName, zones)
time.Sleep(5 * time.Second)
}
}
func addHandler(zones Zones, name string, config *Zone) {
oldZone := zones[name]
config.SetupMetrics(oldZone)
zones[name] = config
dns.HandleFunc(name, setupServerFunc(config))
}
func zonesReadDir(dirName string, zones Zones) error {
func (srv *Server) zonesReadDir(dirName string, zones Zones) error {
dir, err := ioutil.ReadDir(dirName)
if err != nil {
log.Println("Could not read", dirName, ":", err)
@@ -115,7 +101,7 @@ func zonesReadDir(dirName string, zones Zones) error {
(lastRead[zoneName]).hash = sha256
addHandler(zones, zoneName, config)
srv.addHandler(zones, zoneName, config)
}
}
@@ -136,18 +122,18 @@ func zonesReadDir(dirName string, zones Zones) error {
return parseErr
}
func setupPgeodnsZone(zones Zones) {
func (srv *Server) setupPgeodnsZone(zones Zones) {
zoneName := "pgeodns"
Zone := NewZone(zoneName)
label := new(Label)
label.Records = make(map[uint16]Records)
label.Weight = make(map[uint16]int)
Zone.Labels[""] = label
setupSOA(Zone)
addHandler(zones, zoneName, Zone)
srv.addHandler(zones, zoneName, Zone)
}
func setupRootZone() {
func (srv *Server) setupRootZone() {
dns.HandleFunc(".", func(w dns.ResponseWriter, r *dns.Msg) {
m := new(dns.Msg)
m.SetRcode(r, dns.RcodeRefused)
View
@@ -14,6 +14,7 @@ import (
func Test(t *testing.T) { TestingT(t) }
type ConfigSuite struct {
srv *Server
zones Zones
}
@@ -22,7 +23,8 @@ var _ = Suite(&ConfigSuite{})
func (s *ConfigSuite) SetUpSuite(c *C) {
s.zones = make(Zones)
lastRead = map[string]*ZoneReadRecord{}
zonesReadDir("dns", s.zones)
s.srv = &Server{}
s.srv.zonesReadDir("dns", s.zones)
}
func (s *ConfigSuite) TestReadConfigs(c *C) {
@@ -70,7 +72,7 @@ func (s *ConfigSuite) TestReadConfigs(c *C) {
func (s *ConfigSuite) TestRemoveConfig(c *C) {
// restore the dns.Mux
defer zonesReadDir("dns", s.zones)
defer s.srv.zonesReadDir("dns", s.zones)
dir, err := ioutil.TempDir("", "geodns-test.")
if err != nil {
@@ -95,14 +97,14 @@ func (s *ConfigSuite) TestRemoveConfig(c *C) {
c.Fail()
}
zonesReadDir(dir, s.zones)
s.srv.zonesReadDir(dir, s.zones)
c.Check(s.zones["test.example.org"].Origin, Equals, "test.example.org")
c.Check(s.zones["test2.example.org"].Origin, Equals, "test2.example.org")
os.Remove(dir + "/test2.example.org.json")
os.Remove(dir + "/invalid.example.org.json")
zonesReadDir(dir, s.zones)
s.srv.zonesReadDir(dir, s.zones)
c.Check(s.zones["test.example.org"].Origin, Equals, "test.example.org")
_, ok := s.zones["test2.example.org"]
c.Check(ok, Equals, false)

0 comments on commit dc6fbb8

Please sign in to comment.