summaryrefslogtreecommitdiff
path: root/web/controllers/manage.go
blob: 53a38a659fcf70a800eda567b09c2d40893c3534 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package controllers

import (
	"net/http"

	"github.com/gin-gonic/gin"

	"code.crute.me/mcrute/frame"
	"code.crute.me/mcrute/go_ddns_manager/dns"
	"code.crute.me/mcrute/go_ddns_manager/web/middleware"
)

type UpdateZone struct {
	Create []dns.RR
	Update []dns.RR
	Delete []dns.RR
}

func ManageRoot(c *gin.Context) {
	c.JSON(http.StatusOK, map[string]string{
		"views_url": frame.MakeURL(c.Request, "/manage/views").String(),
	})
}

func ListViews(c *gin.Context) {
	cfg := middleware.GetServerConfig(c)

	out := map[string]string{}

	for _, vn := range cfg.BindConfig.Views() {
		out[vn] = frame.MakeURL(c.Request, "/manage/views/%s", vn).String()
	}

	c.JSON(http.StatusOK, out)
}

func ListView(c *gin.Context) {
	cfg := middleware.GetServerConfig(c)
	view := c.Param("view")

	out := map[string]string{}

	for _, z := range cfg.BindConfig.ZonesInView(view) {
		out[z.Name] = frame.MakeURL(c.Request, "/manage/views/%s/%s", view, z.Name).String()
	}

	c.JSON(http.StatusOK, out)
}

func ListZone(c *gin.Context) {
	cfg := middleware.GetServerConfig(c)
	zone := cfg.BindConfig.Zone(c.Param("view"), c.Param("zone"))

	rrs, err := cfg.DNSClient.ReadRemoteZone(zone)
	if err != nil {
		c.JSON(http.StatusInternalServerError, err)
		return
	}

	c.JSON(http.StatusOK, rrs)
}