From b7c07b86a09b4378e35b72b47e91c15a3c2d2aaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20B=C3=B6hm?= Date: Sun, 3 Dec 2017 23:07:13 +0100 Subject: [PATCH] move dnsapi from frontend to backend --- backend/backend.go | 45 ++++++++++++++++++++++++++++++++ ddns.go | 29 +++++++++++++++----- frontend/{web.go => frontend.go} | 45 ++++++++------------------------ shared/config.go | 3 ++- 4 files changed, 80 insertions(+), 42 deletions(-) create mode 100644 backend/backend.go rename frontend/{web.go => frontend.go} (73%) diff --git a/backend/backend.go b/backend/backend.go new file mode 100644 index 0000000..dbf9f73 --- /dev/null +++ b/backend/backend.go @@ -0,0 +1,45 @@ +package backend + +import ( + "github.com/pboehm/ddns/shared" + "gopkg.in/gin-gonic/gin.v1" + "log" + "strings" +) + +type Backend struct { + config *shared.Config + lookup *HostLookup +} + +func NewBackend(config *shared.Config, lookup *HostLookup) *Backend { + return &Backend{ + config: config, + lookup: lookup, + } +} + +func (b *Backend) Run() error { + r := gin.Default() + + r.GET("/dnsapi/lookup/:qname/:qtype", func(c *gin.Context) { + request := &Request{ + QName: strings.TrimRight(c.Param("qname"), "."), + QType: c.Param("qtype"), + } + + response, err := b.lookup.Lookup(request) + if err == nil { + c.JSON(200, gin.H{ + "result": []*Response{response}, + }) + } else { + log.Printf("Error during lookup: %v", err) + c.JSON(200, gin.H{ + "result": false, + }) + } + }) + + return r.Run(b.config.BackendListen) +} diff --git a/ddns.go b/ddns.go index 8d5969c..15983aa 100644 --- a/ddns.go +++ b/ddns.go @@ -5,6 +5,7 @@ import ( "github.com/pboehm/ddns/backend" "github.com/pboehm/ddns/frontend" "github.com/pboehm/ddns/shared" + "golang.org/x/sync/errgroup" "log" "strings" ) @@ -15,15 +16,18 @@ func init() { flag.StringVar(&serviceConfig.Domain, "domain", "", "The subdomain which should be handled by DDNS") - flag.StringVar(&serviceConfig.Listen, "listen", ":8080", - "Which socket should the web service use to bind itself") + flag.StringVar(&serviceConfig.SOAFqdn, "soa_fqdn", "", + "The FQDN of the DNS server which is returned as a SOA record") + + flag.StringVar(&serviceConfig.BackendListen, "listen-backend", ":8057", + "Which socket should the backend web service use to bind itself") + + flag.StringVar(&serviceConfig.FrontendListen, "listen-frontend", ":8080", + "Which socket should the frontend web service use to bind itself") flag.StringVar(&serviceConfig.RedisHost, "redis", ":6379", "The Redis socket that should be used") - flag.StringVar(&serviceConfig.SOAFqdn, "soa_fqdn", "", - "The FQDN of the DNS server which is returned as a SOA record") - flag.IntVar(&serviceConfig.HostExpirationDays, "expiration-days", 10, "The number of days after a host is released when it is not updated") @@ -51,7 +55,18 @@ func main() { redis := shared.NewRedisBackend(serviceConfig) defer redis.Close() - lookup := backend.NewHostLookup(serviceConfig, redis) + var group errgroup.Group - frontend.NewWebService(serviceConfig, redis, lookup).Run() + group.Go(func() error { + lookup := backend.NewHostLookup(serviceConfig, redis) + return backend.NewBackend(serviceConfig, lookup).Run() + }) + + group.Go(func() error { + return frontend.NewFrontend(serviceConfig, redis).Run() + }) + + if err := group.Wait(); err != nil { + log.Fatal(err) + } } diff --git a/frontend/web.go b/frontend/frontend.go similarity index 73% rename from frontend/web.go rename to frontend/frontend.go index 65c9303..976948e 100644 --- a/frontend/web.go +++ b/frontend/frontend.go @@ -2,7 +2,6 @@ package frontend import ( "fmt" - "github.com/pboehm/ddns/backend" "github.com/pboehm/ddns/shared" "gopkg.in/gin-gonic/gin.v1" "html/template" @@ -10,36 +9,33 @@ import ( "net" "net/http" "regexp" - "strings" ) -type WebService struct { +type Frontend struct { config *shared.Config hosts shared.HostBackend - lookup *backend.HostLookup } -func NewWebService(config *shared.Config, hosts shared.HostBackend, lookup *backend.HostLookup) *WebService { - return &WebService{ +func NewFrontend(config *shared.Config, hosts shared.HostBackend) *Frontend { + return &Frontend{ config: config, hosts: hosts, - lookup: lookup, } } -func (w *WebService) Run() { +func (f *Frontend) Run() error { r := gin.Default() r.SetHTMLTemplate(buildTemplate()) r.GET("/", func(g *gin.Context) { - g.HTML(200, "index.html", gin.H{"domain": w.config.Domain}) + g.HTML(200, "index.html", gin.H{"domain": f.config.Domain}) }) r.GET("/available/:hostname", func(c *gin.Context) { hostname, valid := isValidHostname(c.Params.ByName("hostname")) if valid { - _, err := w.hosts.GetHost(hostname) + _, err := f.hosts.GetHost(hostname) valid = err != nil } @@ -58,7 +54,7 @@ func (w *WebService) Run() { var err error - if h, err := w.hosts.GetHost(hostname); err == nil { + if h, err := f.hosts.GetHost(hostname); err == nil { c.JSON(403, gin.H{ "error": fmt.Sprintf("This hostname has already been registered. %v", h), }) @@ -68,7 +64,7 @@ func (w *WebService) Run() { host := &shared.Host{Hostname: hostname, Ip: "127.0.0.1"} host.GenerateAndSetToken() - if err = w.hosts.SetHost(host); err != nil { + if err = f.hosts.SetHost(host); err != nil { c.JSON(400, gin.H{"error": "Could not register host."}) return } @@ -89,7 +85,7 @@ func (w *WebService) Run() { return } - host, err := w.hosts.GetHost(hostname) + host, err := f.hosts.GetHost(hostname) if err != nil { c.JSON(404, gin.H{ "error": "This hostname has not been registered or is expired.", @@ -113,7 +109,7 @@ func (w *WebService) Run() { } host.Ip = ip - if err = w.hosts.SetHost(host); err != nil { + if err = f.hosts.SetHost(host); err != nil { c.JSON(400, gin.H{ "error": "Could not update registered IP address", }) @@ -125,26 +121,7 @@ func (w *WebService) Run() { }) }) - r.GET("/dnsapi/lookup/:qname/:qtype", func(c *gin.Context) { - request := &backend.Request{ - QName: strings.TrimRight(c.Param("qname"), "."), - QType: c.Param("qtype"), - } - - response, err := w.lookup.Lookup(request) - if err == nil { - c.JSON(200, gin.H{ - "result": []*backend.Response{response}, - }) - } else { - log.Printf("Error during lookup: %v", err) - c.JSON(200, gin.H{ - "result": false, - }) - } - }) - - r.Run(w.config.Listen) + return r.Run(f.config.FrontendListen) } // Get the Remote Address of the client. At First we try to get the diff --git a/shared/config.go b/shared/config.go index 664829a..42326d6 100644 --- a/shared/config.go +++ b/shared/config.go @@ -5,6 +5,7 @@ type Config struct { Domain string SOAFqdn string HostExpirationDays int - Listen string + FrontendListen string + BackendListen string RedisHost string }