move dnsapi from frontend to backend
This commit is contained in:
parent
887600f964
commit
b7c07b86a0
4 changed files with 80 additions and 42 deletions
45
backend/backend.go
Normal file
45
backend/backend.go
Normal file
|
@ -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/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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
|
@ -5,6 +5,7 @@ type Config struct {
|
|||
Domain string
|
||||
SOAFqdn string
|
||||
HostExpirationDays int
|
||||
Listen string
|
||||
FrontendListen string
|
||||
BackendListen string
|
||||
RedisHost string
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue