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 {
|
2018-01-28 01:49:41 +01:00
|
|
|
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 {
|
2018-01-28 01:49:41 +01:00
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-28 01:50:07 +01:00
|
|
|
r.GET("/dnsapi/getDomainMetadata/:name/:kind", func(c *gin.Context) {
|
|
|
|
c.JSON(200, gin.H{
|
|
|
|
"result": false,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-12-03 23:18:47 +01:00
|
|
|
return r.Run(b.config.ListenBackend)
|
2017-12-03 23:07:13 +01:00
|
|
|
}
|