-
Notifications
You must be signed in to change notification settings - Fork 11
/
domains.handler.go
130 lines (105 loc) · 2.98 KB
/
domains.handler.go
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package handlers
import (
"errors"
"log"
"strings"
"github.com/labstack/echo/v4"
"github.com/nwesterhausen/domain-monitor/configuration"
"github.com/nwesterhausen/domain-monitor/views/domains"
)
type DomainHandler struct {
DomainService ApiDomainService
}
func NewDomainHandler(ds ApiDomainService) *DomainHandler {
return &DomainHandler{
DomainService: ds,
}
}
// Get the HTML for the domain card inner content from 'fqdn' parameter
func (h *DomainHandler) GetCard(c echo.Context) error {
fqdn := c.Param("fqdn")
if len(fqdn) == 0 {
return errors.New("Invalid domain to fetch (FQDN required)")
}
domain, err := h.DomainService.GetDomain(fqdn)
if err != nil {
return err
}
card := domains.DomainCard(domain)
return View(c, card)
}
// Get the HTML for all the domain cards
func (h *DomainHandler) GetCards(c echo.Context) error {
domainList, err := h.DomainService.GetDomains()
if err != nil {
return err
}
cards := domains.DomainCards(domainList)
return View(c, cards)
}
// Get HTML for domain list as tbody
func (h *DomainHandler) GetListTbody(c echo.Context) error {
domainList, err := h.DomainService.GetDomains()
if err != nil {
return err
}
list := domains.DomainListingTbody(domainList)
return View(c, list)
}
// Add a domain and return an updated tbody
func (h *DomainHandler) PostNewDomain(c echo.Context) error {
var domain configuration.Domain
if err := c.Bind(&domain); err != nil {
return err
}
log.Printf("🆕 Adding domain: %+v\n", domain)
_, err := h.DomainService.CreateDomain(domain)
if err != nil {
return err
}
return h.GetListTbody(c)
}
// Delete a domain and return an updated tbody
func (h *DomainHandler) DeleteDomain(c echo.Context) error {
fqdn := c.Param("fqdn")
if len(fqdn) == 0 {
return errors.New("invalid domain to delete (FQDN required)")
}
log.Printf("🙅 Deleting domain: %s\n", fqdn)
err := h.DomainService.DeleteDomain(fqdn)
if err != nil {
return err
}
return h.GetListTbody(c)
}
// Get the HTML for the domain edit form
func (h *DomainHandler) GetEditDomain(c echo.Context) error {
fqdn := c.Param("fqdn")
if len(fqdn) == 0 {
return errors.New("invalid domain to edit (FQDN required)")
}
domain, err := h.DomainService.GetDomain(fqdn)
if err != nil {
return err
}
log.Printf("🛰️ Editing domain: %+v\n", domain)
return View(c, domains.DomainTableRowInput(strings.ReplaceAll(domain.FQDN, ".", "_"), domain))
}
// Update a domain and return an updated tbody
func (h *DomainHandler) PostUpdateDomain(c echo.Context) error {
var domain configuration.Domain
if err := (&echo.DefaultBinder{}).BindBody(c, &domain); err != nil {
return err
}
log.Printf("🛰️ Updating domain: %+v\n", domain)
err := h.DomainService.UpdateDomain(domain)
if err != nil {
return err
}
return h.GetListDomainRow(domain, c)
}
// Get the HTML for a single domain row
func (h *DomainHandler) GetListDomainRow(domain configuration.Domain, c echo.Context) error {
row := domains.DomainTableRow(domain)
return View(c, row)
}