Browse Source

Cleanup/usability updates

master
Tyler 3 years ago
parent
commit
288c2de7eb
  1. 32
      main.go
  2. 5
      stack.yml

32
main.go

@ -94,8 +94,7 @@ func getRecords(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
hosts := make(map[string]string)
if err := c.Hgetall(key, hosts); err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(&response{Success: false, Message: "Unable to retrieve records: " + err.Error()})
errorResponse(w, http.StatusInternalServerError, "Unable to retrieve records: " + err.Error())
return
}
@ -166,29 +165,31 @@ type domainRequest struct {
}
func updateRecord(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
if r.Header.Get("Content-Type") != "application/json" {
errorResponse(w, http.StatusBadRequest, "Invalid body type.")
return
}
var req domainRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(&response{Success: false, Message: "Unable to decode body: " + err.Error()})
errorResponse(w, http.StatusBadRequest, "Unable to decode body: " + err.Error())
return
}
req.Domain = strings.ToLower(req.Domain)
if !govalidator.IsDNSName(req.Domain) || !govalidator.IsIP(req.IP) {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(&response{Success: false, Message: "Invalid domain or IP"})
errorResponse(w, http.StatusBadRequest, "Invalid domain or IP")
return
}
if _, err := c.Hset(key, req.Domain, []byte(req.IP)); err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(&response{Success: false, Message: "Unable to save record: " + err.Error()})
errorResponse(w, http.StatusInternalServerError, "Unable to save record: " + err.Error())
return
}
c.Publish("godns:update_record", []byte(strings.ToLower(req.Domain)))
c.Publish("godns:update_record", []byte(req.Domain))
json.NewEncoder(w).Encode(&response{Success: true})
}
@ -211,8 +212,7 @@ func removeRecord(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
hosts := make(map[string]string)
if err := c.Hgetall(key, hosts); err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(&response{Success: false, Message: "Unable to fetch records: " + err.Error()})
errorResponse(w, http.StatusInternalServerError, "Unable to fetch records: " + err.Error())
return
}
@ -229,11 +229,15 @@ func removeRecord(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
}
} else {
if _, err := c.Hdel(key, req.Domain); err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(&response{Success: false, Message: "Unable to delete record: " + err.Error()})
errorResponse(w, http.StatusInternalServerError, "Unable to delete record: " + err.Error())
return
}
c.Publish("godns:remove_record", []byte(strings.ToLower(req.Domain)))
c.Publish("godns:remove_record", []byte(req.Domain))
}
}
func errorResponse(w http.ResponseWriter, status int, message string) {
w.WriteHeader(status)
json.NewEncoder(w).Encode(&response{Success: false, Message: message})
}

5
stack.yml

@ -21,11 +21,6 @@ services:
- REDIS_ADDR=redis:6379
networks:
- godns
deploy:
mode: replicated
replicas: 1
placement:
constraints: [node.role == manager]
redis:
image: registry.git.meow.tf/sopine-docker/services/redis:latest
volumes:

Loading…
Cancel
Save