-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (64 loc) · 1.93 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"context"
"flag"
"log"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"github.com/jiripetrlik/handy-dns-manager/internal/app/dnszone"
"github.com/jiripetrlik/handy-dns-manager/internal/app/rest"
)
func main() {
ipPtr := flag.String("i", "127.0.0.1", "IP of primary nameserver")
originPtr := flag.String("o", "example-domain.", "Domain origin")
primaryNameServerPtr := flag.String("p", "ns1", "Primary name server")
emailPtr := flag.String("e", "email.example-domain.", "Hostmaster email")
dnszonePtr := flag.String("f", "example-domain.hosts", "Zone file")
zoneDataPtr := flag.String("d", "example-domain.json", "Data about zone")
htpasswdPtr := flag.String("s", "", "Htpasswd file")
certFilePtr := flag.String("certfile", "", "Cert file")
keyFilePtr := flag.String("keyfile", "", "Key file")
flag.Parse()
log.Printf(
"Starting handy-dns-manager for domain %v. dnszone=%v and zone data=%v",
*originPtr,
*dnszonePtr,
*zoneDataPtr,
)
dnsZone := dnszone.DNSZone{
*dnszonePtr,
*zoneDataPtr,
&sync.Mutex{},
}
dnsZone.Initialize(*ipPtr, *originPtr, *primaryNameServerPtr, *emailPtr)
restServer := rest.NewHandyDNSRestServer(&dnsZone, *htpasswdPtr)
restServer.HandleRestAPI()
var srv http.Server
idleConnsClosed := make(chan struct{})
go func() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
log.Print("Registering handler for graceful termination")
<-sigs
log.Print("Closing server")
if err := srv.Shutdown(context.Background()); err != nil {
log.Fatal("HTTP server Shutdown: " + err.Error())
}
close(idleConnsClosed)
}()
var err error
if len(*certFilePtr) > 0 && len(*keyFilePtr) > 0 {
srv.Addr = ":8443"
err = srv.ListenAndServeTLS(*certFilePtr, *keyFilePtr)
} else {
srv.Addr = ":8080"
err = srv.ListenAndServe()
}
if err != http.ErrServerClosed {
log.Fatal("HTTP server ListenAndServe: " + err.Error())
}
<-idleConnsClosed
}