mirror of https://github.com/gophish/gophish
Moved JSON encoding to helper function because DRY.
parent
e1520e6742
commit
3dd22e8d7c
|
@ -59,11 +59,7 @@ func API_Campaigns(w http.ResponseWriter, r *http.Request) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
cj, err := json.MarshalIndent(cs, "", " ")
|
writeJSON(w, cs)
|
||||||
if checkError(err, w, "Error looking up campaigns", http.StatusInternalServerError) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
writeJSON(w, cj)
|
|
||||||
//POST: Create a new campaign and return it as JSON
|
//POST: Create a new campaign and return it as JSON
|
||||||
case r.Method == "POST":
|
case r.Method == "POST":
|
||||||
c := models.Campaign{}
|
c := models.Campaign{}
|
||||||
|
@ -86,11 +82,7 @@ func API_Campaigns(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
Worker.Queue <- &c
|
Worker.Queue <- &c
|
||||||
cj, err := json.MarshalIndent(c, "", " ")
|
writeJSON(w, c)
|
||||||
if checkError(err, w, "Error creating JSON response", http.StatusInternalServerError) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
writeJSON(w, cj)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,11 +98,7 @@ func API_Campaigns_Id(w http.ResponseWriter, r *http.Request) {
|
||||||
if checkError(err, w, "No campaign found", http.StatusNotFound) {
|
if checkError(err, w, "No campaign found", http.StatusNotFound) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
cj, err := json.MarshalIndent(c, "", " ")
|
writeJSON(w, c)
|
||||||
if checkError(err, w, "Error creating JSON response", http.StatusInternalServerError) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
writeJSON(w, cj)
|
|
||||||
case r.Method == "DELETE":
|
case r.Method == "DELETE":
|
||||||
_, err := models.GetCampaign(id, ctx.Get(r, "user_id").(int64))
|
_, err := models.GetCampaign(id, ctx.Get(r, "user_id").(int64))
|
||||||
if checkError(err, w, "No campaign found", http.StatusNotFound) {
|
if checkError(err, w, "No campaign found", http.StatusNotFound) {
|
||||||
|
@ -154,11 +142,7 @@ func API_Groups(w http.ResponseWriter, r *http.Request) {
|
||||||
if checkError(err, w, "Groups not found", http.StatusNotFound) {
|
if checkError(err, w, "Groups not found", http.StatusNotFound) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
gj, err := json.MarshalIndent(gs, "", " ")
|
writeJSON(w, gs)
|
||||||
if checkError(err, w, "Error marshaling group information", http.StatusInternalServerError) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
writeJSON(w, gj)
|
|
||||||
//POST: Create a new group and return it as JSON
|
//POST: Create a new group and return it as JSON
|
||||||
case r.Method == "POST":
|
case r.Method == "POST":
|
||||||
g := models.Group{}
|
g := models.Group{}
|
||||||
|
@ -178,11 +162,7 @@ func API_Groups(w http.ResponseWriter, r *http.Request) {
|
||||||
if checkError(err, w, "Error inserting group", http.StatusInternalServerError) {
|
if checkError(err, w, "Error inserting group", http.StatusInternalServerError) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
gj, err := json.MarshalIndent(g, "", " ")
|
writeJSON(w, g)
|
||||||
if checkError(err, w, "Error creating JSON response", http.StatusInternalServerError) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
writeJSON(w, gj)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -197,11 +177,7 @@ func API_Groups_Id(w http.ResponseWriter, r *http.Request) {
|
||||||
if checkError(err, w, "No group found", http.StatusNotFound) {
|
if checkError(err, w, "No group found", http.StatusNotFound) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
gj, err := json.MarshalIndent(g, "", " ")
|
writeJSON(w, g)
|
||||||
if checkError(err, w, "Error creating JSON response", http.StatusInternalServerError) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
writeJSON(w, gj)
|
|
||||||
case r.Method == "DELETE":
|
case r.Method == "DELETE":
|
||||||
g, err := models.GetGroup(id, ctx.Get(r, "user_id").(int64))
|
g, err := models.GetGroup(id, ctx.Get(r, "user_id").(int64))
|
||||||
if checkError(err, w, "No group found", http.StatusNotFound) {
|
if checkError(err, w, "No group found", http.StatusNotFound) {
|
||||||
|
@ -234,11 +210,7 @@ func API_Groups_Id(w http.ResponseWriter, r *http.Request) {
|
||||||
if checkError(err, w, "Error updating group", http.StatusInternalServerError) {
|
if checkError(err, w, "Error updating group", http.StatusInternalServerError) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
gj, err := json.MarshalIndent(g, "", " ")
|
writeJSON(w, g)
|
||||||
if checkError(err, w, "Error creating JSON response", http.StatusInternalServerError) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
writeJSON(w, gj)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -249,11 +221,7 @@ func API_Templates(w http.ResponseWriter, r *http.Request) {
|
||||||
if checkError(err, w, "Templates not found", http.StatusNotFound) {
|
if checkError(err, w, "Templates not found", http.StatusNotFound) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
tj, err := json.MarshalIndent(ts, "", " ")
|
writeJSON(w, ts)
|
||||||
if checkError(err, w, "Error marshaling template information", http.StatusInternalServerError) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
writeJSON(w, tj)
|
|
||||||
//POST: Create a new template and return it as JSON
|
//POST: Create a new template and return it as JSON
|
||||||
case r.Method == "POST":
|
case r.Method == "POST":
|
||||||
t := models.Template{}
|
t := models.Template{}
|
||||||
|
@ -268,11 +236,7 @@ func API_Templates(w http.ResponseWriter, r *http.Request) {
|
||||||
if checkError(err, w, "Error inserting template", http.StatusInternalServerError) {
|
if checkError(err, w, "Error inserting template", http.StatusInternalServerError) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
tj, err := json.MarshalIndent(t, "", " ")
|
writeJSON(w, t)
|
||||||
if checkError(err, w, "Error creating JSON response", http.StatusInternalServerError) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
writeJSON(w, tj)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -285,11 +249,7 @@ func API_Templates_Id(w http.ResponseWriter, r *http.Request) {
|
||||||
if checkError(err, w, "No template found", http.StatusNotFound) {
|
if checkError(err, w, "No template found", http.StatusNotFound) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
tj, err := json.MarshalIndent(t, "", " ")
|
writeJSON(w, t)
|
||||||
if checkError(err, w, "Error creating JSON response", http.StatusInternalServerError) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
writeJSON(w, tj)
|
|
||||||
case r.Method == "DELETE":
|
case r.Method == "DELETE":
|
||||||
err := models.DeleteTemplate(id, ctx.Get(r, "user_id").(int64))
|
err := models.DeleteTemplate(id, ctx.Get(r, "user_id").(int64))
|
||||||
if checkError(err, w, "Error deleting group", http.StatusInternalServerError) {
|
if checkError(err, w, "Error deleting group", http.StatusInternalServerError) {
|
||||||
|
@ -312,15 +272,15 @@ func API_Templates_Id(w http.ResponseWriter, r *http.Request) {
|
||||||
if checkError(err, w, "Error updating group", http.StatusInternalServerError) {
|
if checkError(err, w, "Error updating group", http.StatusInternalServerError) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
tj, err := json.MarshalIndent(t, "", " ")
|
writeJSON(w, t)
|
||||||
if checkError(err, w, "Error creating JSON response", http.StatusInternalServerError) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
writeJSON(w, tj)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeJSON(w http.ResponseWriter, c []byte) {
|
func writeJSON(w http.ResponseWriter, c interface{}) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
cj, err := json.MarshalIndent(c, "", " ")
|
||||||
fmt.Fprintf(w, "%s", c)
|
if checkError(err, w, "Error creating JSON response", http.StatusInternalServerError) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
fmt.Fprintf(w, "%s", cj)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue