move dnsapi from frontend to backend
This commit is contained in:
parent
887600f964
commit
b7c07b86a0
|
@ -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)
|
||||||
|
}
|
29
ddns.go
29
ddns.go
|
@ -5,6 +5,7 @@ import (
|
||||||
"github.com/pboehm/ddns/backend"
|
"github.com/pboehm/ddns/backend"
|
||||||
"github.com/pboehm/ddns/frontend"
|
"github.com/pboehm/ddns/frontend"
|
||||||
"github.com/pboehm/ddns/shared"
|
"github.com/pboehm/ddns/shared"
|
||||||
|
"golang.org/x/sync/errgroup"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
@ -15,15 +16,18 @@ func init() {
|
||||||
flag.StringVar(&serviceConfig.Domain, "domain", "",
|
flag.StringVar(&serviceConfig.Domain, "domain", "",
|
||||||
"The subdomain which should be handled by DDNS")
|
"The subdomain which should be handled by DDNS")
|
||||||
|
|
||||||
flag.StringVar(&serviceConfig.Listen, "listen", ":8080",
|
flag.StringVar(&serviceConfig.SOAFqdn, "soa_fqdn", "",
|
||||||
"Which socket should the web service use to bind itself")
|
"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",
|
flag.StringVar(&serviceConfig.RedisHost, "redis", ":6379",
|
||||||
"The Redis socket that should be used")
|
"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,
|
flag.IntVar(&serviceConfig.HostExpirationDays, "expiration-days", 10,
|
||||||
"The number of days after a host is released when it is not updated")
|
"The number of days after a host is released when it is not updated")
|
||||||
|
|
||||||
|
@ -51,7 +55,18 @@ func main() {
|
||||||
redis := shared.NewRedisBackend(serviceConfig)
|
redis := shared.NewRedisBackend(serviceConfig)
|
||||||
defer redis.Close()
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package frontend
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/pboehm/ddns/backend"
|
|
||||||
"github.com/pboehm/ddns/shared"
|
"github.com/pboehm/ddns/shared"
|
||||||
"gopkg.in/gin-gonic/gin.v1"
|
"gopkg.in/gin-gonic/gin.v1"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
@ -10,36 +9,33 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type WebService struct {
|
type Frontend struct {
|
||||||
config *shared.Config
|
config *shared.Config
|
||||||
hosts shared.HostBackend
|
hosts shared.HostBackend
|
||||||
lookup *backend.HostLookup
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWebService(config *shared.Config, hosts shared.HostBackend, lookup *backend.HostLookup) *WebService {
|
func NewFrontend(config *shared.Config, hosts shared.HostBackend) *Frontend {
|
||||||
return &WebService{
|
return &Frontend{
|
||||||
config: config,
|
config: config,
|
||||||
hosts: hosts,
|
hosts: hosts,
|
||||||
lookup: lookup,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WebService) Run() {
|
func (f *Frontend) Run() error {
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
r.SetHTMLTemplate(buildTemplate())
|
r.SetHTMLTemplate(buildTemplate())
|
||||||
|
|
||||||
r.GET("/", func(g *gin.Context) {
|
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) {
|
r.GET("/available/:hostname", func(c *gin.Context) {
|
||||||
hostname, valid := isValidHostname(c.Params.ByName("hostname"))
|
hostname, valid := isValidHostname(c.Params.ByName("hostname"))
|
||||||
|
|
||||||
if valid {
|
if valid {
|
||||||
_, err := w.hosts.GetHost(hostname)
|
_, err := f.hosts.GetHost(hostname)
|
||||||
valid = err != nil
|
valid = err != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,7 +54,7 @@ func (w *WebService) Run() {
|
||||||
|
|
||||||
var err error
|
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{
|
c.JSON(403, gin.H{
|
||||||
"error": fmt.Sprintf("This hostname has already been registered. %v", 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 := &shared.Host{Hostname: hostname, Ip: "127.0.0.1"}
|
||||||
host.GenerateAndSetToken()
|
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."})
|
c.JSON(400, gin.H{"error": "Could not register host."})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -89,7 +85,7 @@ func (w *WebService) Run() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
host, err := w.hosts.GetHost(hostname)
|
host, err := f.hosts.GetHost(hostname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(404, gin.H{
|
c.JSON(404, gin.H{
|
||||||
"error": "This hostname has not been registered or is expired.",
|
"error": "This hostname has not been registered or is expired.",
|
||||||
|
@ -113,7 +109,7 @@ func (w *WebService) Run() {
|
||||||
}
|
}
|
||||||
|
|
||||||
host.Ip = ip
|
host.Ip = ip
|
||||||
if err = w.hosts.SetHost(host); err != nil {
|
if err = f.hosts.SetHost(host); err != nil {
|
||||||
c.JSON(400, gin.H{
|
c.JSON(400, gin.H{
|
||||||
"error": "Could not update registered IP address",
|
"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) {
|
return r.Run(f.config.FrontendListen)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the Remote Address of the client. At First we try to get the
|
// Get the Remote Address of the client. At First we try to get the
|
|
@ -5,6 +5,7 @@ type Config struct {
|
||||||
Domain string
|
Domain string
|
||||||
SOAFqdn string
|
SOAFqdn string
|
||||||
HostExpirationDays int
|
HostExpirationDays int
|
||||||
Listen string
|
FrontendListen string
|
||||||
|
BackendListen string
|
||||||
RedisHost string
|
RedisHost string
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue