This repository has been archived by the owner on Oct 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathrest.go
145 lines (123 loc) · 3.07 KB
/
rest.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2018 The Rind Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package rind
import (
"encoding/json"
"net/http"
)
// RestServer will do CRUD on DNS records
type RestServer interface {
Create() http.HandlerFunc
Read() http.HandlerFunc
Update() http.HandlerFunc
Delete() http.HandlerFunc
}
// RestService is an implementation of RestServer interface.
type RestService struct {
Dn *DNSService
}
type request struct {
Host string
TTL uint32
Type string
Data string
OldData string
SOA requestSOA
OldSOA requestSOA
MX requestMX
OldMX requestMX
SRV requestSRV
OldSRV requestSRV
}
type requestSOA struct {
NS string
MBox string
Serial uint32
Refresh uint32
Retry uint32
Expire uint32
MinTTL uint32
}
type requestMX struct {
Pref uint16
MX string
}
type requestSRV struct {
Priority uint16
Weight uint16
Port uint16
Target string
}
type get struct {
Host string
TTL uint32
Type string
Data string
}
// Create is HTTP handler of POST request.
// Use for adding new record to DNS server.
func (s *RestService) Create(w http.ResponseWriter, r *http.Request) {
var req request
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
resource, err := toResource(req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
s.Dn.save(ntString(resource.Header.Name, resource.Header.Type), resource, nil)
w.WriteHeader(http.StatusCreated)
}
// Read is HTTP handler of GET request.
// Use for reading existed records on DNS server.
func (s *RestService) Read(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(s.Dn.all())
}
// Update is HTTP handler of PUT request.
// Use for updating existed records on DNS server.
func (s *RestService) Update(w http.ResponseWriter, r *http.Request) {
var req request
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
oldReq := request{Host: req.Host, Type: req.Type, Data: req.OldData}
old, err := toResource(oldReq)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
resource, err := toResource(req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
ok := s.Dn.save(ntString(resource.Header.Name, resource.Header.Type), resource, &old)
if ok {
w.WriteHeader(http.StatusOK)
return
}
http.Error(w, "", http.StatusNotFound)
}
// Delete is HTTP handler of DELETE request.
// Use for removing records on DNS server.
func (s *RestService) Delete(w http.ResponseWriter, r *http.Request) {
var req request
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
ok := false
h, err := toResourceHeader(req.Host, req.Type)
if err == nil {
ok = s.Dn.remove(ntString(h.Name, h.Type), nil)
}
if ok {
w.WriteHeader(http.StatusOK)
return
}
http.Error(w, "", http.StatusNotFound)
}