api: standardise marshal and unmarshal fn names

This commit is contained in:
Adhityaa Chandrasekar 2018-07-24 12:28:43 +05:30
parent 9974365b73
commit a8ecbd9717
23 changed files with 127 additions and 127 deletions

View File

@ -31,38 +31,38 @@ func commentApproveHandler(w http.ResponseWriter, r *http.Request) {
} }
var x request var x request
if err := unmarshalBody(r, &x); err != nil { if err := bodyUnmarshal(r, &x); err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
c, err := commenterGetByCommenterToken(*x.CommenterToken) c, err := commenterGetByCommenterToken(*x.CommenterToken)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
domain, err := commentDomainGet(*x.CommentHex) domain, err := commentDomainGet(*x.CommentHex)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
isModerator, err := isDomainModerator(domain, c.Email) isModerator, err := isDomainModerator(domain, c.Email)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
if !isModerator { if !isModerator {
writeBody(w, response{"success": false, "message": errorNotModerator.Error()}) bodyMarshal(w, response{"success": false, "message": errorNotModerator.Error()})
return return
} }
if err = commentApprove(*x.CommentHex); err != nil { if err = commentApprove(*x.CommentHex); err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
writeBody(w, response{"success": true}) bodyMarshal(w, response{"success": true})
} }

View File

@ -30,38 +30,38 @@ func commentDeleteHandler(w http.ResponseWriter, r *http.Request) {
} }
var x request var x request
if err := unmarshalBody(r, &x); err != nil { if err := bodyUnmarshal(r, &x); err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
c, err := commenterGetByCommenterToken(*x.CommenterToken) c, err := commenterGetByCommenterToken(*x.CommenterToken)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
domain, err := commentDomainGet(*x.CommentHex) domain, err := commentDomainGet(*x.CommentHex)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
isModerator, err := isDomainModerator(domain, c.Email) isModerator, err := isDomainModerator(domain, c.Email)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
if !isModerator { if !isModerator {
writeBody(w, response{"success": false, "message": errorNotModerator.Error()}) bodyMarshal(w, response{"success": false, "message": errorNotModerator.Error()})
return return
} }
if err = commentDelete(*x.CommentHex); err != nil { if err = commentDelete(*x.CommentHex); err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
writeBody(w, response{"success": true}) bodyMarshal(w, response{"success": true})
} }

View File

@ -97,8 +97,8 @@ func commentListHandler(w http.ResponseWriter, r *http.Request) {
} }
var x request var x request
if err := unmarshalBody(r, &x); err != nil { if err := bodyUnmarshal(r, &x); err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
@ -107,7 +107,7 @@ func commentListHandler(w http.ResponseWriter, r *http.Request) {
d, err := domainGet(domain) d, err := domainGet(domain)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
@ -119,7 +119,7 @@ func commentListHandler(w http.ResponseWriter, r *http.Request) {
if err == errorNoSuchToken { if err == errorNoSuchToken {
commenterHex = "anonymous" commenterHex = "anonymous"
} else { } else {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
} else { } else {
@ -138,11 +138,11 @@ func commentListHandler(w http.ResponseWriter, r *http.Request) {
comments, commenters, err := commentList(commenterHex, domain, path, isModerator) comments, commenters, err := commentList(commenterHex, domain, path, isModerator)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
writeBody(w, response{ bodyMarshal(w, response{
"success": true, "success": true,
"domain": domain, "domain": domain,
"comments": comments, "comments": comments,

View File

@ -44,8 +44,8 @@ func commentNewHandler(w http.ResponseWriter, r *http.Request) {
} }
var x request var x request
if err := unmarshalBody(r, &x); err != nil { if err := bodyUnmarshal(r, &x); err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
@ -54,12 +54,12 @@ func commentNewHandler(w http.ResponseWriter, r *http.Request) {
d, err := domainGet(domain) d, err := domainGet(domain)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
if d.State == "frozen" { if d.State == "frozen" {
writeBody(w, response{"success": false, "message": errorDomainFrozen.Error()}) bodyMarshal(w, response{"success": false, "message": errorDomainFrozen.Error()})
return return
} }
@ -80,7 +80,7 @@ func commentNewHandler(w http.ResponseWriter, r *http.Request) {
} else { } else {
c, err := commenterGetByCommenterToken(*x.CommenterToken) c, err := commenterGetByCommenterToken(*x.CommenterToken)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return 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()) commentHex, err := commentNew(commenterHex, domain, path, *x.ParentHex, *x.Markdown, state, time.Now().UTC())
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
writeBody(w, response{"success": true, "commentHex": commentHex, "approved": state == "approved"}) bodyMarshal(w, response{"success": true, "commentHex": commentHex, "approved": state == "approved"})
} }

View File

@ -51,19 +51,19 @@ func commentVoteHandler(w http.ResponseWriter, r *http.Request) {
} }
var x request var x request
if err := unmarshalBody(r, &x); err != nil { if err := bodyUnmarshal(r, &x); err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
if *x.CommenterToken == "anonymous" { if *x.CommenterToken == "anonymous" {
writeBody(w, response{"success": false, "message": errorUnauthorisedVote.Error()}) bodyMarshal(w, response{"success": false, "message": errorUnauthorisedVote.Error()})
return return
} }
c, err := commenterGetByCommenterToken(*x.CommenterToken) c, err := commenterGetByCommenterToken(*x.CommenterToken)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
@ -75,9 +75,9 @@ func commentVoteHandler(w http.ResponseWriter, r *http.Request) {
} }
if err := commentVote(c.CommenterHex, *x.CommentHex, direction); err != nil { 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 return
} }
writeBody(w, response{"success": true}) bodyMarshal(w, response{"success": true})
} }

View File

@ -56,16 +56,16 @@ func commenterLoginHandler(w http.ResponseWriter, r *http.Request) {
} }
var x request var x request
if err := unmarshalBody(r, &x); err != nil { if err := bodyUnmarshal(r, &x); err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
commenterToken, err := commenterLogin(*x.Email, *x.Password) commenterToken, err := commenterLogin(*x.Email, *x.Password)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
writeBody(w, response{"success": true, "commenterToken": commenterToken}) bodyMarshal(w, response{"success": true, "commenterToken": commenterToken})
} }

View File

@ -63,8 +63,8 @@ func commenterNewHandler(w http.ResponseWriter, r *http.Request) {
} }
var x request var x request
if err := unmarshalBody(r, &x); err != nil { if err := bodyUnmarshal(r, &x); err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return 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?
// 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 { 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 return
} }
writeBody(w, response{"success": true, "confirmEmail": smtpConfigured}) bodyMarshal(w, response{"success": true, "confirmEmail": smtpConfigured})
} }

View File

@ -10,16 +10,16 @@ func commenterSelfHandler(w http.ResponseWriter, r *http.Request) {
} }
var x request var x request
if err := unmarshalBody(r, &x); err != nil { if err := bodyUnmarshal(r, &x); err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
c, err := commenterGetByCommenterToken(*x.CommenterToken) c, err := commenterGetByCommenterToken(*x.CommenterToken)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
writeBody(w, response{"success": true, "commenter": c}) bodyMarshal(w, response{"success": true, "commenter": c})
} }

View File

@ -29,9 +29,9 @@ func commenterTokenNew() (string, error) {
func commenterTokenNewHandler(w http.ResponseWriter, r *http.Request) { func commenterTokenNewHandler(w http.ResponseWriter, r *http.Request) {
commenterToken, err := commenterTokenNew() commenterToken, err := commenterTokenNew()
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
writeBody(w, response{"success": true, "commenterToken": commenterToken}) bodyMarshal(w, response{"success": true, "commenterToken": commenterToken})
} }

View File

@ -70,33 +70,33 @@ func domainDeleteHandler(w http.ResponseWriter, r *http.Request) {
} }
var x request var x request
if err := unmarshalBody(r, &x); err != nil { if err := bodyUnmarshal(r, &x); err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
o, err := ownerGetByOwnerToken(*x.OwnerToken) o, err := ownerGetByOwnerToken(*x.OwnerToken)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
domain := stripDomain(*x.Domain) domain := stripDomain(*x.Domain)
isOwner, err := domainOwnershipVerify(o.OwnerHex, domain) isOwner, err := domainOwnershipVerify(o.OwnerHex, domain)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
if !isOwner { if !isOwner {
writeBody(w, response{"success": false, "message": errorNotAuthorised.Error()}) bodyMarshal(w, response{"success": false, "message": errorNotAuthorised.Error()})
return return
} }
if err = domainDelete(*x.Domain); err != nil { if err = domainDelete(*x.Domain); err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
writeBody(w, response{"success": true}) bodyMarshal(w, response{"success": true})
} }

View File

@ -174,34 +174,34 @@ func domainImportDisqusHandler(w http.ResponseWriter, r *http.Request) {
} }
var x request var x request
if err := unmarshalBody(r, &x); err != nil { if err := bodyUnmarshal(r, &x); err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
o, err := ownerGetByOwnerToken(*x.OwnerToken) o, err := ownerGetByOwnerToken(*x.OwnerToken)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
domain := stripDomain(*x.Domain) domain := stripDomain(*x.Domain)
isOwner, err := domainOwnershipVerify(o.OwnerHex, domain) isOwner, err := domainOwnershipVerify(o.OwnerHex, domain)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
if !isOwner { if !isOwner {
writeBody(w, response{"success": false, "message": errorNotAuthorised.Error()}) bodyMarshal(w, response{"success": false, "message": errorNotAuthorised.Error()})
return return
} }
numImported, err := domainImportDisqus(domain, *x.URL) numImported, err := domainImportDisqus(domain, *x.URL)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
writeBody(w, response{"success": true, "numImported": numImported}) bodyMarshal(w, response{"success": true, "numImported": numImported})
} }

View File

@ -46,22 +46,22 @@ func domainListHandler(w http.ResponseWriter, r *http.Request) {
} }
var x request var x request
if err := unmarshalBody(r, &x); err != nil { if err := bodyUnmarshal(r, &x); err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
o, err := ownerGetByOwnerToken(*x.OwnerToken) o, err := ownerGetByOwnerToken(*x.OwnerToken)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
domains, err := domainList(o.OwnerHex) domains, err := domainList(o.OwnerHex)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
writeBody(w, response{"success": true, "domains": domains}) bodyMarshal(w, response{"success": true, "domains": domains})
} }

View File

@ -30,33 +30,33 @@ func domainModeratorDeleteHandler(w http.ResponseWriter, r *http.Request) {
} }
var x request var x request
if err := unmarshalBody(r, &x); err != nil { if err := bodyUnmarshal(r, &x); err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
o, err := ownerGetByOwnerToken(*x.OwnerToken) o, err := ownerGetByOwnerToken(*x.OwnerToken)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
domain := stripDomain(*x.Domain) domain := stripDomain(*x.Domain)
authorised, err := domainOwnershipVerify(o.OwnerHex, domain) authorised, err := domainOwnershipVerify(o.OwnerHex, domain)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
if !authorised { if !authorised {
writeBody(w, response{"success": false, "message": errorNotAuthorised.Error()}) bodyMarshal(w, response{"success": false, "message": errorNotAuthorised.Error()})
return return
} }
if err = domainModeratorDelete(domain, *x.Email); err != nil { 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 return
} }
writeBody(w, response{"success": true}) bodyMarshal(w, response{"success": true})
} }

View File

@ -32,33 +32,33 @@ func domainModeratorNewHandler(w http.ResponseWriter, r *http.Request) {
} }
var x request var x request
if err := unmarshalBody(r, &x); err != nil { if err := bodyUnmarshal(r, &x); err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
o, err := ownerGetByOwnerToken(*x.OwnerToken) o, err := ownerGetByOwnerToken(*x.OwnerToken)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
domain := stripDomain(*x.Domain) domain := stripDomain(*x.Domain)
isOwner, err := domainOwnershipVerify(o.OwnerHex, domain) isOwner, err := domainOwnershipVerify(o.OwnerHex, domain)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
if !isOwner { if !isOwner {
writeBody(w, response{"success": false, "message": errorNotAuthorised.Error()}) bodyMarshal(w, response{"success": false, "message": errorNotAuthorised.Error()})
return return
} }
if err = domainModeratorNew(domain, *x.Email); err != nil { 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 return
} }
writeBody(w, response{"success": true}) bodyMarshal(w, response{"success": true})
} }

View File

@ -32,28 +32,28 @@ func domainNewHandler(w http.ResponseWriter, r *http.Request) {
} }
var x request var x request
if err := unmarshalBody(r, &x); err != nil { if err := bodyUnmarshal(r, &x); err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
o, err := ownerGetByOwnerToken(*x.OwnerToken) o, err := ownerGetByOwnerToken(*x.OwnerToken)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
domain := stripDomain(*x.Domain) domain := stripDomain(*x.Domain)
if err = domainNew(o.OwnerHex, *x.Name, domain); err != nil { 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 return
} }
if err = domainModeratorNew(domain, o.Email); err != nil { 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 return
} }
writeBody(w, response{"success": true, "domain": domain}) bodyMarshal(w, response{"success": true, "domain": domain})
} }

View File

@ -44,40 +44,40 @@ func domainStatisticsHandler(w http.ResponseWriter, r *http.Request) {
} }
var x request var x request
if err := unmarshalBody(r, &x); err != nil { if err := bodyUnmarshal(r, &x); err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
o, err := ownerGetByOwnerToken(*x.OwnerToken) o, err := ownerGetByOwnerToken(*x.OwnerToken)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
domain := stripDomain(*x.Domain) domain := stripDomain(*x.Domain)
isOwner, err := domainOwnershipVerify(o.OwnerHex, domain) isOwner, err := domainOwnershipVerify(o.OwnerHex, domain)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
if !isOwner { if !isOwner {
writeBody(w, response{"success": false, "message": errorNotAuthorised.Error()}) bodyMarshal(w, response{"success": false, "message": errorNotAuthorised.Error()})
return return
} }
viewsLast30Days, err := domainStatistics(domain) viewsLast30Days, err := domainStatistics(domain)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
commentsLast30Days, err := commentStatistics(domain) commentsLast30Days, err := commentStatistics(domain)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
writeBody(w, response{"success": true, "viewsLast30Days": viewsLast30Days, "commentsLast30Days": commentsLast30Days}) bodyMarshal(w, response{"success": true, "viewsLast30Days": viewsLast30Days, "commentsLast30Days": commentsLast30Days})
} }

View File

@ -27,33 +27,33 @@ func domainUpdateHandler(w http.ResponseWriter, r *http.Request) {
} }
var x request var x request
if err := unmarshalBody(r, &x); err != nil { if err := bodyUnmarshal(r, &x); err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
o, err := ownerGetByOwnerToken(*x.OwnerToken) o, err := ownerGetByOwnerToken(*x.OwnerToken)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
domain := stripDomain((*x.D).Domain) domain := stripDomain((*x.D).Domain)
isOwner, err := domainOwnershipVerify(o.OwnerHex, domain) isOwner, err := domainOwnershipVerify(o.OwnerHex, domain)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
if !isOwner { if !isOwner {
writeBody(w, response{"success": false, "message": errorNotAuthorised.Error()}) bodyMarshal(w, response{"success": false, "message": errorNotAuthorised.Error()})
return return
} }
if err = domainUpdate(*x.D); err != nil { if err = domainUpdate(*x.D); err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
writeBody(w, response{"success": true}) bodyMarshal(w, response{"success": true})
} }

View File

@ -61,16 +61,16 @@ func ownerLoginHandler(w http.ResponseWriter, r *http.Request) {
} }
var x request var x request
if err := unmarshalBody(r, &x); err != nil { if err := bodyUnmarshal(r, &x); err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
ownerToken, err := ownerLogin(*x.Email, *x.Password) ownerToken, err := ownerLogin(*x.Email, *x.Password)
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
writeBody(w, response{"success": true, "ownerToken": ownerToken}) bodyMarshal(w, response{"success": true, "ownerToken": ownerToken})
} }

View File

@ -69,20 +69,20 @@ func ownerNewHandler(w http.ResponseWriter, r *http.Request) {
} }
var x request var x request
if err := unmarshalBody(r, &x); err != nil { if err := bodyUnmarshal(r, &x); err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
if _, err := ownerNew(*x.Email, *x.Name, *x.Password); err != nil { 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 return
} }
if _, err := commenterNew(*x.Email, *x.Name, "undefined", "undefined", "commento", *x.Password); err != nil { 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 return
} }
writeBody(w, response{"success": true, "confirmEmail": smtpConfigured}) bodyMarshal(w, response{"success": true, "confirmEmail": smtpConfigured})
} }

View File

@ -56,15 +56,15 @@ func ownerSendResetHexHandler(w http.ResponseWriter, r *http.Request) {
} }
var x request var x request
if err := unmarshalBody(r, &x); err != nil { if err := bodyUnmarshal(r, &x); err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
if err := ownerSendResetHex(*x.Email); err != nil { if err := ownerSendResetHex(*x.Email); err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
writeBody(w, response{"success": true}) bodyMarshal(w, response{"success": true})
} }

View File

@ -58,15 +58,15 @@ func ownerResetPasswordHandler(w http.ResponseWriter, r *http.Request) {
} }
var x request var x request
if err := unmarshalBody(r, &x); err != nil { if err := bodyUnmarshal(r, &x); err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
if err := ownerResetPassword(*x.ResetHex, *x.Password); err != nil { 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 return
} }
writeBody(w, response{"success": true}) bodyMarshal(w, response{"success": true})
} }

View File

@ -10,21 +10,21 @@ func ownerSelfHandler(w http.ResponseWriter, r *http.Request) {
} }
var x request var x request
if err := unmarshalBody(r, &x); err != nil { if err := bodyUnmarshal(r, &x); err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
o, err := ownerGetByOwnerToken(*x.OwnerToken) o, err := ownerGetByOwnerToken(*x.OwnerToken)
if err == errorNoSuchToken { if err == errorNoSuchToken {
writeBody(w, response{"success": true, "loggedIn": false}) bodyMarshal(w, response{"success": true, "loggedIn": false})
return return
} }
if err != nil { if err != nil {
writeBody(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }
writeBody(w, response{"success": true, "loggedIn": true, "owner": o}) bodyMarshal(w, response{"success": true, "loggedIn": true, "owner": o})
} }

View File

@ -11,7 +11,7 @@ type response map[string]interface{}
// TODO: Add tests in utils_http_test.go // 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) b, err := ioutil.ReadAll(r.Body)
if err != nil { if err != nil {
logger.Errorf("cannot read POST body: %v\n", err) logger.Errorf("cannot read POST body: %v\n", err)
@ -32,7 +32,7 @@ func unmarshalBody(r *http.Request, x interface{}) error {
return nil 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) resp, err := json.Marshal(x)
if err != nil { if err != nil {
w.Write([]byte(`{"success":false,"message":"Some internal error occurred"}`)) w.Write([]byte(`{"success":false,"message":"Some internal error occurred"}`))