Skip to content

Commit

Permalink
refactor: split main on func
Browse files Browse the repository at this point in the history
  • Loading branch information
grishy committed Jul 27, 2023
1 parent 679b538 commit fc5877f
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,14 @@ import (
"github.com/miekg/dns"
)

const TTL = uint32(60 * 10) // seconds

func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()

log.Println("Creating publisher")
publisher, err := publisher.NewPublisher()
if err != nil {
log.Fatalf("Can't create publisher: %v", err)
}
const TTL = uint32(60 * 10) // in seconds

func formatCname(hostnameFqdn string, cnames []string) []string {
log.Println("Formating CNAMEs:")
cnames := os.Args[1:]

for i, cname := range cnames {
if !dns.IsFqdn(cname) {
cnames[i] = dns.Fqdn(cname + "." + publisher.Fqdn())
cnames[i] = dns.Fqdn(cname + "." + hostnameFqdn)

log.Printf(" > '%s' (added current FQDN)", cnames[i])
continue
Expand All @@ -36,17 +27,25 @@ func main() {
log.Printf(" > '%s'", cname)
}

return cnames
}

func publishing(ctx context.Context, publisher *publisher.Publisher, cnames []string) {
resendDuration := time.Duration(TTL/2) * time.Second
log.Printf("Publishing every %v and CNAME TTL=%ds.", resendDuration, TTL)

// To start publishing immediately
// https://github.com/golang/go/issues/17601
publisher.PublishCNAMES(os.Args[1:], TTL)
if err := publisher.PublishCNAMES(cnames, TTL); err != nil {
log.Fatalf("can't publish CNAMEs: %v", err)
}

for {
select {
case <-time.Tick(resendDuration):
publisher.PublishCNAMES(os.Args[1:], TTL)
if err := publisher.PublishCNAMES(cnames, TTL); err != nil {
log.Fatalf("can't publish CNAMEs: %v", err)
}
case <-ctx.Done():
log.Println("Closing publisher...")
if err := publisher.Close(); err != nil {
Expand All @@ -56,3 +55,17 @@ func main() {
}
}
}

func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()

log.Println("Creating publisher")
publisher, err := publisher.NewPublisher()
if err != nil {
log.Fatalf("Can't create publisher: %v", err)
}

cnames := formatCname(publisher.Fqdn(), os.Args[1:])
publishing(ctx, publisher, cnames)
}

0 comments on commit fc5877f

Please sign in to comment.