ddns/backend/backend.go

60 lines
1 KiB
Go
Raw Normal View History

2017-12-03 23:07:13 +01:00
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.New()
r.Use(gin.Recovery())
if b.config.Verbose {
r.Use(gin.Logger())
}
2017-12-03 23:07:13 +01:00
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 {
if b.config.Verbose {
log.Printf("Error during lookup: %v", err)
}
2017-12-03 23:07:13 +01:00
c.JSON(200, gin.H{
"result": false,
})
}
})
r.GET("/dnsapi/getDomainMetadata/:name/:kind", func(c *gin.Context) {
c.JSON(200, gin.H{
"result": false,
})
})
return r.Run(b.config.ListenBackend)
2017-12-03 23:07:13 +01:00
}