Moved main to top, libs to subpackage
This commit is contained in:
1 parent
1ff9f7e9b7
commit
3976e5ceaf
4 files changed
No files matched your search
@@ -0,0 +1,60 @@
|
||||
package weavedns
|
||||
|
||||
import (
|
||||
"github.com/miekg/dns"
|
||||
"log"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type Zone interface {
|
||||
AddRecord(name string, ip string, weave_ip string) error
|
||||
MatchLocal(name string) (string, error)
|
||||
MatchGlobal(name string) (string, error)
|
||||
}
|
||||
|
||||
type Record struct {
|
||||
Name string
|
||||
Ip string
|
||||
WeaveIp string
|
||||
}
|
||||
|
||||
type ZoneDb struct {
|
||||
mx sync.RWMutex
|
||||
recs []Record
|
||||
}
|
||||
|
||||
type LookupError string
|
||||
|
||||
func (ops LookupError) Error() string {
|
||||
return string(ops)
|
||||
}
|
||||
|
||||
// Stop gap.
|
||||
func (zone *ZoneDb) match(name string) (string, error) {
|
||||
for _, r := range zone.recs {
|
||||
log.Printf("%s == %s ?", r.Name, name)
|
||||
if r.Name == name {
|
||||
return r.WeaveIp, nil
|
||||
}
|
||||
}
|
||||
return "", LookupError(name)
|
||||
}
|
||||
|
||||
func (zone *ZoneDb) MatchLocal(name string) (string, error) {
|
||||
zone.mx.Lock()
|
||||
defer zone.mx.Unlock()
|
||||
return zone.match(name)
|
||||
}
|
||||
|
||||
func (zone *ZoneDb) MatchGlobal(name string) (string, error) {
|
||||
zone.mx.Lock()
|
||||
defer zone.mx.Unlock()
|
||||
return zone.match(name)
|
||||
}
|
||||
|
||||
func (zone *ZoneDb) AddRecord(name string, ip string, weave_ip string) error {
|
||||
zone.mx.Lock()
|
||||
defer zone.mx.Unlock()
|
||||
zone.recs = append(zone.recs, Record{dns.Fqdn(name), ip, weave_ip})
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user