This repository has been archived by the owner on Nov 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller_admin.go
93 lines (83 loc) · 2.5 KB
/
controller_admin.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
package clickcounteriframe
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
hivdomainstatus "github.com/dothiv/hiv-domain-status"
)
type AdminController struct {
domainRepo DomainRepositoryInterface
}
func NewAdminController(d DomainRepositoryInterface) (c *AdminController) {
c = new(AdminController)
c.domainRepo = d
return
}
func (c *AdminController) DomainHandler(w http.ResponseWriter, r *http.Request, matches []string) {
w.Header().Add("X-Click-Counter-Iframe-Version", VERSION)
if r.Method == "DELETE" {
c.deleteDomain(w, r, matches)
return
}
if r.Method != "PUT" {
w.WriteHeader(http.StatusBadRequest)
return
}
if r.Header.Get("Content-Type") != "application/json" {
hivdomainstatus.HttpProblem(w, http.StatusBadRequest, "Expected application/json")
return
}
b, bodyErr := ioutil.ReadAll(r.Body)
if bodyErr != nil {
hivdomainstatus.HttpProblem(w, http.StatusInternalServerError, fmt.Sprintf("failed to read request body:", bodyErr.Error()))
return
}
var data struct {
Redirect string
LandingPage *LandingPage `json:"landingPage"`
}
unmarshalErr := json.Unmarshal(b, &data)
if unmarshalErr != nil {
hivdomainstatus.HttpProblem(w, http.StatusBadRequest, fmt.Sprintf("failed to read json: %s", unmarshalErr.Error()))
return
}
redirect, urlErr := url.Parse(data.Redirect)
if urlErr != nil {
hivdomainstatus.HttpProblem(w, http.StatusBadRequest, fmt.Sprintf("Invalid redirect url provided: %s", data.Redirect))
return
}
domain, domainErr := c.domainRepo.FindByName(matches[1])
created := false
if domainErr != nil {
domain = new(Domain)
domain.Name = matches[1]
created = true
}
if len(redirect.String()) > 0 {
domain.Redirect.String = redirect.String()
domain.Redirect.Valid = true
}
domain.LandingPage = data.LandingPage
c.domainRepo.Persist(domain)
if created {
w.Header().Add("Location", r.URL.String())
w.WriteHeader(http.StatusCreated)
} else {
w.WriteHeader(http.StatusNoContent)
}
}
func (c *AdminController) deleteDomain(w http.ResponseWriter, r *http.Request, matches []string) {
domain, domainErr := c.domainRepo.FindByName(matches[1])
if domainErr != nil {
hivdomainstatus.HttpProblem(w, http.StatusNotFound, fmt.Sprintf("domain not found: %s", matches[1]))
return
}
deleteErr := c.domainRepo.Remove(domain)
if deleteErr != nil {
hivdomainstatus.HttpProblem(w, http.StatusInternalServerError, fmt.Sprintf("Failed to delete domain: %s! %s", matches[1], deleteErr.Error()))
return
}
w.WriteHeader(http.StatusNoContent)
}