api: standardise marshal and unmarshal fn names
This commit is contained in:
parent
9974365b73
commit
a8ecbd9717
@ -31,38 +31,38 @@ func commentApproveHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var x request
|
||||
if err := unmarshalBody(r, &x); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
if err := bodyUnmarshal(r, &x); err != nil {
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c, err := commenterGetByCommenterToken(*x.CommenterToken)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
domain, err := commentDomainGet(*x.CommentHex)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
isModerator, err := isDomainModerator(domain, c.Email)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if !isModerator {
|
||||
writeBody(w, response{"success": false, "message": errorNotModerator.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": errorNotModerator.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err = commentApprove(*x.CommentHex); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
writeBody(w, response{"success": true})
|
||||
bodyMarshal(w, response{"success": true})
|
||||
}
|
||||
|
@ -30,38 +30,38 @@ func commentDeleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var x request
|
||||
if err := unmarshalBody(r, &x); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
if err := bodyUnmarshal(r, &x); err != nil {
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c, err := commenterGetByCommenterToken(*x.CommenterToken)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
domain, err := commentDomainGet(*x.CommentHex)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
isModerator, err := isDomainModerator(domain, c.Email)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if !isModerator {
|
||||
writeBody(w, response{"success": false, "message": errorNotModerator.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": errorNotModerator.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err = commentDelete(*x.CommentHex); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
writeBody(w, response{"success": true})
|
||||
bodyMarshal(w, response{"success": true})
|
||||
}
|
||||
|
@ -97,8 +97,8 @@ func commentListHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var x request
|
||||
if err := unmarshalBody(r, &x); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
if err := bodyUnmarshal(r, &x); err != nil {
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
@ -107,7 +107,7 @@ func commentListHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
d, err := domainGet(domain)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
@ -119,7 +119,7 @@ func commentListHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if err == errorNoSuchToken {
|
||||
commenterHex = "anonymous"
|
||||
} else {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
} else {
|
||||
@ -138,11 +138,11 @@ func commentListHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
comments, commenters, err := commentList(commenterHex, domain, path, isModerator)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
writeBody(w, response{
|
||||
bodyMarshal(w, response{
|
||||
"success": true,
|
||||
"domain": domain,
|
||||
"comments": comments,
|
||||
|
@ -44,8 +44,8 @@ func commentNewHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var x request
|
||||
if err := unmarshalBody(r, &x); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
if err := bodyUnmarshal(r, &x); err != nil {
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
@ -54,12 +54,12 @@ func commentNewHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
d, err := domainGet(domain)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if d.State == "frozen" {
|
||||
writeBody(w, response{"success": false, "message": errorDomainFrozen.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": errorDomainFrozen.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
@ -80,7 +80,7 @@ func commentNewHandler(w http.ResponseWriter, r *http.Request) {
|
||||
} else {
|
||||
c, err := commenterGetByCommenterToken(*x.CommenterToken)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
@ -108,9 +108,9 @@ func commentNewHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
commentHex, err := commentNew(commenterHex, domain, path, *x.ParentHex, *x.Markdown, state, time.Now().UTC())
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
writeBody(w, response{"success": true, "commentHex": commentHex, "approved": state == "approved"})
|
||||
bodyMarshal(w, response{"success": true, "commentHex": commentHex, "approved": state == "approved"})
|
||||
}
|
||||
|
@ -51,19 +51,19 @@ func commentVoteHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var x request
|
||||
if err := unmarshalBody(r, &x); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
if err := bodyUnmarshal(r, &x); err != nil {
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if *x.CommenterToken == "anonymous" {
|
||||
writeBody(w, response{"success": false, "message": errorUnauthorisedVote.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": errorUnauthorisedVote.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c, err := commenterGetByCommenterToken(*x.CommenterToken)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
@ -75,9 +75,9 @@ func commentVoteHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
if err := commentVote(c.CommenterHex, *x.CommentHex, direction); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
writeBody(w, response{"success": true})
|
||||
bodyMarshal(w, response{"success": true})
|
||||
}
|
||||
|
@ -56,16 +56,16 @@ func commenterLoginHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var x request
|
||||
if err := unmarshalBody(r, &x); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
if err := bodyUnmarshal(r, &x); err != nil {
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
commenterToken, err := commenterLogin(*x.Email, *x.Password)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
writeBody(w, response{"success": true, "commenterToken": commenterToken})
|
||||
bodyMarshal(w, response{"success": true, "commenterToken": commenterToken})
|
||||
}
|
||||
|
@ -63,8 +63,8 @@ func commenterNewHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var x request
|
||||
if err := unmarshalBody(r, &x); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
if err := bodyUnmarshal(r, &x); err != nil {
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
@ -72,9 +72,9 @@ func commenterNewHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// TODO: email confirmation if provider = commento?
|
||||
// TODO: email confirmation if provider = commento?
|
||||
if _, err := commenterNew(*x.Email, *x.Name, *x.Website, "undefined", "commento", *x.Password); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
writeBody(w, response{"success": true, "confirmEmail": smtpConfigured})
|
||||
bodyMarshal(w, response{"success": true, "confirmEmail": smtpConfigured})
|
||||
}
|
||||
|
@ -10,16 +10,16 @@ func commenterSelfHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var x request
|
||||
if err := unmarshalBody(r, &x); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
if err := bodyUnmarshal(r, &x); err != nil {
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c, err := commenterGetByCommenterToken(*x.CommenterToken)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
writeBody(w, response{"success": true, "commenter": c})
|
||||
bodyMarshal(w, response{"success": true, "commenter": c})
|
||||
}
|
||||
|
@ -29,9 +29,9 @@ func commenterTokenNew() (string, error) {
|
||||
func commenterTokenNewHandler(w http.ResponseWriter, r *http.Request) {
|
||||
commenterToken, err := commenterTokenNew()
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
writeBody(w, response{"success": true, "commenterToken": commenterToken})
|
||||
bodyMarshal(w, response{"success": true, "commenterToken": commenterToken})
|
||||
}
|
||||
|
@ -70,33 +70,33 @@ func domainDeleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var x request
|
||||
if err := unmarshalBody(r, &x); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
if err := bodyUnmarshal(r, &x); err != nil {
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
o, err := ownerGetByOwnerToken(*x.OwnerToken)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
domain := stripDomain(*x.Domain)
|
||||
isOwner, err := domainOwnershipVerify(o.OwnerHex, domain)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if !isOwner {
|
||||
writeBody(w, response{"success": false, "message": errorNotAuthorised.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": errorNotAuthorised.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err = domainDelete(*x.Domain); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
writeBody(w, response{"success": true})
|
||||
bodyMarshal(w, response{"success": true})
|
||||
}
|
||||
|
@ -174,34 +174,34 @@ func domainImportDisqusHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var x request
|
||||
if err := unmarshalBody(r, &x); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
if err := bodyUnmarshal(r, &x); err != nil {
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
o, err := ownerGetByOwnerToken(*x.OwnerToken)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
domain := stripDomain(*x.Domain)
|
||||
isOwner, err := domainOwnershipVerify(o.OwnerHex, domain)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if !isOwner {
|
||||
writeBody(w, response{"success": false, "message": errorNotAuthorised.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": errorNotAuthorised.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
numImported, err := domainImportDisqus(domain, *x.URL)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
writeBody(w, response{"success": true, "numImported": numImported})
|
||||
bodyMarshal(w, response{"success": true, "numImported": numImported})
|
||||
}
|
||||
|
@ -46,22 +46,22 @@ func domainListHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var x request
|
||||
if err := unmarshalBody(r, &x); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
if err := bodyUnmarshal(r, &x); err != nil {
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
o, err := ownerGetByOwnerToken(*x.OwnerToken)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
domains, err := domainList(o.OwnerHex)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
writeBody(w, response{"success": true, "domains": domains})
|
||||
bodyMarshal(w, response{"success": true, "domains": domains})
|
||||
}
|
||||
|
@ -30,33 +30,33 @@ func domainModeratorDeleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var x request
|
||||
if err := unmarshalBody(r, &x); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
if err := bodyUnmarshal(r, &x); err != nil {
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
o, err := ownerGetByOwnerToken(*x.OwnerToken)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
domain := stripDomain(*x.Domain)
|
||||
authorised, err := domainOwnershipVerify(o.OwnerHex, domain)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if !authorised {
|
||||
writeBody(w, response{"success": false, "message": errorNotAuthorised.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": errorNotAuthorised.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err = domainModeratorDelete(domain, *x.Email); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
writeBody(w, response{"success": true})
|
||||
bodyMarshal(w, response{"success": true})
|
||||
}
|
||||
|
@ -32,33 +32,33 @@ func domainModeratorNewHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var x request
|
||||
if err := unmarshalBody(r, &x); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
if err := bodyUnmarshal(r, &x); err != nil {
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
o, err := ownerGetByOwnerToken(*x.OwnerToken)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
domain := stripDomain(*x.Domain)
|
||||
isOwner, err := domainOwnershipVerify(o.OwnerHex, domain)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if !isOwner {
|
||||
writeBody(w, response{"success": false, "message": errorNotAuthorised.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": errorNotAuthorised.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err = domainModeratorNew(domain, *x.Email); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
writeBody(w, response{"success": true})
|
||||
bodyMarshal(w, response{"success": true})
|
||||
}
|
||||
|
@ -32,28 +32,28 @@ func domainNewHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var x request
|
||||
if err := unmarshalBody(r, &x); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
if err := bodyUnmarshal(r, &x); err != nil {
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
o, err := ownerGetByOwnerToken(*x.OwnerToken)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
domain := stripDomain(*x.Domain)
|
||||
|
||||
if err = domainNew(o.OwnerHex, *x.Name, domain); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err = domainModeratorNew(domain, o.Email); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
writeBody(w, response{"success": true, "domain": domain})
|
||||
bodyMarshal(w, response{"success": true, "domain": domain})
|
||||
}
|
||||
|
@ -44,40 +44,40 @@ func domainStatisticsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var x request
|
||||
if err := unmarshalBody(r, &x); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
if err := bodyUnmarshal(r, &x); err != nil {
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
o, err := ownerGetByOwnerToken(*x.OwnerToken)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
domain := stripDomain(*x.Domain)
|
||||
isOwner, err := domainOwnershipVerify(o.OwnerHex, domain)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if !isOwner {
|
||||
writeBody(w, response{"success": false, "message": errorNotAuthorised.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": errorNotAuthorised.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
viewsLast30Days, err := domainStatistics(domain)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
commentsLast30Days, err := commentStatistics(domain)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
writeBody(w, response{"success": true, "viewsLast30Days": viewsLast30Days, "commentsLast30Days": commentsLast30Days})
|
||||
bodyMarshal(w, response{"success": true, "viewsLast30Days": viewsLast30Days, "commentsLast30Days": commentsLast30Days})
|
||||
}
|
||||
|
@ -27,33 +27,33 @@ func domainUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var x request
|
||||
if err := unmarshalBody(r, &x); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
if err := bodyUnmarshal(r, &x); err != nil {
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
o, err := ownerGetByOwnerToken(*x.OwnerToken)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
domain := stripDomain((*x.D).Domain)
|
||||
isOwner, err := domainOwnershipVerify(o.OwnerHex, domain)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if !isOwner {
|
||||
writeBody(w, response{"success": false, "message": errorNotAuthorised.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": errorNotAuthorised.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err = domainUpdate(*x.D); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
writeBody(w, response{"success": true})
|
||||
bodyMarshal(w, response{"success": true})
|
||||
}
|
||||
|
@ -61,16 +61,16 @@ func ownerLoginHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var x request
|
||||
if err := unmarshalBody(r, &x); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
if err := bodyUnmarshal(r, &x); err != nil {
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
ownerToken, err := ownerLogin(*x.Email, *x.Password)
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
writeBody(w, response{"success": true, "ownerToken": ownerToken})
|
||||
bodyMarshal(w, response{"success": true, "ownerToken": ownerToken})
|
||||
}
|
||||
|
@ -69,20 +69,20 @@ func ownerNewHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var x request
|
||||
if err := unmarshalBody(r, &x); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
if err := bodyUnmarshal(r, &x); err != nil {
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := ownerNew(*x.Email, *x.Name, *x.Password); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := commenterNew(*x.Email, *x.Name, "undefined", "undefined", "commento", *x.Password); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
writeBody(w, response{"success": true, "confirmEmail": smtpConfigured})
|
||||
bodyMarshal(w, response{"success": true, "confirmEmail": smtpConfigured})
|
||||
}
|
||||
|
@ -56,15 +56,15 @@ func ownerSendResetHexHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var x request
|
||||
if err := unmarshalBody(r, &x); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
if err := bodyUnmarshal(r, &x); err != nil {
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err := ownerSendResetHex(*x.Email); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
writeBody(w, response{"success": true})
|
||||
bodyMarshal(w, response{"success": true})
|
||||
}
|
||||
|
@ -58,15 +58,15 @@ func ownerResetPasswordHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var x request
|
||||
if err := unmarshalBody(r, &x); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
if err := bodyUnmarshal(r, &x); err != nil {
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err := ownerResetPassword(*x.ResetHex, *x.Password); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
writeBody(w, response{"success": true})
|
||||
bodyMarshal(w, response{"success": true})
|
||||
}
|
||||
|
@ -10,21 +10,21 @@ func ownerSelfHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var x request
|
||||
if err := unmarshalBody(r, &x); err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
if err := bodyUnmarshal(r, &x); err != nil {
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
o, err := ownerGetByOwnerToken(*x.OwnerToken)
|
||||
if err == errorNoSuchToken {
|
||||
writeBody(w, response{"success": true, "loggedIn": false})
|
||||
bodyMarshal(w, response{"success": true, "loggedIn": false})
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
writeBody(w, response{"success": false, "message": err.Error()})
|
||||
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
writeBody(w, response{"success": true, "loggedIn": true, "owner": o})
|
||||
bodyMarshal(w, response{"success": true, "loggedIn": true, "owner": o})
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ type response map[string]interface{}
|
||||
|
||||
// TODO: Add tests in utils_http_test.go
|
||||
|
||||
func unmarshalBody(r *http.Request, x interface{}) error {
|
||||
func bodyUnmarshal(r *http.Request, x interface{}) error {
|
||||
b, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
logger.Errorf("cannot read POST body: %v\n", err)
|
||||
@ -32,7 +32,7 @@ func unmarshalBody(r *http.Request, x interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeBody(w http.ResponseWriter, x map[string]interface{}) error {
|
||||
func bodyMarshal(w http.ResponseWriter, x map[string]interface{}) error {
|
||||
resp, err := json.Marshal(x)
|
||||
if err != nil {
|
||||
w.Write([]byte(`{"success":false,"message":"Some internal error occurred"}`))
|
||||
|
Loading…
Reference in New Issue
Block a user