Skip to content

Commit

Permalink
[core] Embedded and implement http.Pusher into http.ResponseWriter+gz…
Browse files Browse the repository at this point in the history
…ip Writer (#47)

* added http Pusher interface to gzip response writer

* implement Pusher on gzipResponseWriter and pass encoding header to pusher options

* providing a generic error view fuction for code that calls a interface method which will have access to the response

* fix []byte -> string type for fmt string

* adding the res, req pattern for method arguments in interfaces and their method calls

* fix for spacing in generic error message

* remove default error views displayed in lifecycle hooks - will rely on custom views or redirects inside hook now that user has ResponseWriter. Otherwise, multiple WriteHeader calls would be warned

* removing WriteHeader calls before return in external handlers

* bump version 0.8.1
  • Loading branch information
nilslice authored Jan 26, 2017
1 parent 3dea30f commit 16a159a
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 139 deletions.
2 changes: 1 addition & 1 deletion cmd/ponzu/ponzu.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "0.8.0"
"version": "0.8.1"
}
2 changes: 1 addition & 1 deletion management/editor/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Editable interface {
type Mergeable interface {
// Approve copies an external post to the internal collection and triggers
// a re-sort of its content type posts
Approve(req *http.Request) error
Approve(http.ResponseWriter, *http.Request) error
}

// Editor is a view containing fields to manage content
Expand Down
20 changes: 20 additions & 0 deletions system/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package admin
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"net/http"

Expand Down Expand Up @@ -607,3 +608,22 @@ var err500HTML = []byte(`
func Error500() ([]byte, error) {
return Admin(err500HTML)
}

var errMessageHTML = `
<div class="error-page eMsg col s6">
<div class="card">
<div class="card-content">
<div class="card-title"><b>Error:&nbsp;</b>%s</div>
<blockquote>%s</blockquote>
</div>
</div>
</div>
`

// ErrorMessage is a generic error message container, similar to Error500() and
// others in this package, ecxept it expects the caller to provide a title and
// message to describe to a view why the error is being shown
func ErrorMessage(title, message string) ([]byte, error) {
eHTML := fmt.Sprintf(errMessageHTML, title, message)
return Admin([]byte(eHTML))
}
122 changes: 19 additions & 103 deletions system/admin/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -1357,56 +1357,28 @@ func approveContentHandler(res http.ResponseWriter, req *http.Request) {
return
}

err = hook.BeforeApprove(req)
err = hook.BeforeApprove(res, req)
if err != nil {
log.Println("Error running BeforeApprove hook in approveContentHandler for:", t, err)
res.WriteHeader(http.StatusInternalServerError)
errView, err := Error500()
if err != nil {
return
}

res.Write(errView)
return
}

// call its Approve method
err = m.Approve(req)
err = m.Approve(res, req)
if err != nil {
log.Println("Error running Approve method in approveContentHandler for:", t, err)
res.WriteHeader(http.StatusInternalServerError)
errView, err := Error500()
if err != nil {
return
}

res.Write(errView)
return
}

err = hook.AfterApprove(req)
err = hook.AfterApprove(res, req)
if err != nil {
log.Println("Error running AfterApprove hook in approveContentHandler for:", t, err)
res.WriteHeader(http.StatusInternalServerError)
errView, err := Error500()
if err != nil {
return
}

res.Write(errView)
return
}

err = hook.BeforeSave(req)
err = hook.BeforeSave(res, req)
if err != nil {
log.Println("Error running BeforeSave hook in approveContentHandler for:", t, err)
res.WriteHeader(http.StatusInternalServerError)
errView, err := Error500()
if err != nil {
return
}

res.Write(errView)
return
}

Expand All @@ -1428,16 +1400,9 @@ func approveContentHandler(res http.ResponseWriter, req *http.Request) {
ctx := context.WithValue(req.Context(), "target", fmt.Sprintf("%s:%d", t, id))
req = req.WithContext(ctx)

err = hook.AfterSave(req)
err = hook.AfterSave(res, req)
if err != nil {
log.Println("Error running AfterSave hook in approveContentHandler for:", t, err)
res.WriteHeader(http.StatusInternalServerError)
errView, err := Error500()
if err != nil {
return
}

res.Write(errView)
return
}

Expand Down Expand Up @@ -1669,16 +1634,9 @@ func editHandler(res http.ResponseWriter, req *http.Request) {
return
}

err = hook.BeforeSave(req)
err = hook.BeforeSave(res, req)
if err != nil {
log.Println(err)
res.WriteHeader(http.StatusInternalServerError)
errView, err := Error500()
if err != nil {
return
}

res.Write(errView)
log.Println("Error running BeforeSave method in editHandler for:", t, err)
return
}

Expand All @@ -1699,16 +1657,9 @@ func editHandler(res http.ResponseWriter, req *http.Request) {
ctx := context.WithValue(req.Context(), "target", fmt.Sprintf("%s:%d", t, id))
req = req.WithContext(ctx)

err = hook.AfterSave(req)
err = hook.AfterSave(res, req)
if err != nil {
log.Println(err)
res.WriteHeader(http.StatusInternalServerError)
errView, err := Error500()
if err != nil {
return
}

res.Write(errView)
log.Println("Error running AfterSave method in editHandler for:", t, err)
return
}

Expand Down Expand Up @@ -1792,30 +1743,16 @@ func deleteHandler(res http.ResponseWriter, req *http.Request) {

reject := req.URL.Query().Get("reject")
if reject == "true" {
err = hook.BeforeReject(req)
err = hook.BeforeReject(res, req)
if err != nil {
log.Println(err)
res.WriteHeader(http.StatusInternalServerError)
errView, err := Error500()
if err != nil {
return
}

res.Write(errView)
log.Println("Error running BeforeReject method in deleteHandler for:", t, err)
return
}
}

err = hook.BeforeDelete(req)
err = hook.BeforeDelete(res, req)
if err != nil {
log.Println(err)
res.WriteHeader(http.StatusInternalServerError)
errView, err := Error500()
if err != nil {
return
}

res.Write(errView)
log.Println("Error running BeforeDelete method in deleteHandler for:", t, err)
return
}

Expand All @@ -1826,30 +1763,16 @@ func deleteHandler(res http.ResponseWriter, req *http.Request) {
return
}

err = hook.AfterDelete(req)
err = hook.AfterDelete(res, req)
if err != nil {
log.Println(err)
res.WriteHeader(http.StatusInternalServerError)
errView, err := Error500()
if err != nil {
return
}

res.Write(errView)
log.Println("Error running AfterDelete method in deleteHandler for:", t, err)
return
}

if reject == "true" {
err = hook.AfterReject(req)
err = hook.AfterReject(res, req)
if err != nil {
log.Println(err)
res.WriteHeader(http.StatusInternalServerError)
errView, err := Error500()
if err != nil {
return
}

res.Write(errView)
log.Println("Error running AfterReject method in deleteHandler for:", t, err)
return
}
}
Expand Down Expand Up @@ -2272,16 +2195,9 @@ func addonHandler(res http.ResponseWriter, req *http.Request) {
// if Hookable, call BeforeSave prior to saving
h, ok := at().(item.Hookable)
if ok {
err := h.BeforeSave(req)
err := h.BeforeSave(res, req)
if err != nil {
log.Println(err)
res.WriteHeader(http.StatusInternalServerError)
errView, err := Error500()
if err != nil {
return
}

res.Write(errView)
log.Println("Error running BeforeSave method in addonHandler for:", id, err)
return
}
}
Expand Down
27 changes: 11 additions & 16 deletions system/api/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import (
// /external/content?type=Review
type Externalable interface {
// Accept allows external content submissions of a specific type
Accept(req *http.Request) error
Accept(http.ResponseWriter, *http.Request) error
}

// Trustable allows external content to be auto-approved, meaning content sent
// as an Externalable will be stored in the public content bucket
type Trustable interface {
AutoApprove(req *http.Request) error
AutoApprove(http.ResponseWriter, *http.Request) error
}

func externalContentHandler(res http.ResponseWriter, req *http.Request) {
Expand Down Expand Up @@ -125,10 +125,9 @@ func externalContentHandler(res http.ResponseWriter, req *http.Request) {

// call Accept with the request, enabling developer to add or chack data
// before saving to DB
err = ext.Accept(req)
err = ext.Accept(res, req)
if err != nil {
log.Println(err)
res.WriteHeader(http.StatusInternalServerError)
log.Println("[External} error calling Accept:", err)
return
}

Expand All @@ -139,10 +138,9 @@ func externalContentHandler(res http.ResponseWriter, req *http.Request) {
return
}

err = hook.BeforeSave(req)
err = hook.BeforeSave(res, req)
if err != nil {
log.Println("[External] error:", err)
res.WriteHeader(http.StatusInternalServerError)
log.Println("[External] error calling BeforeSave:", err)
return
}

Expand All @@ -152,10 +150,9 @@ func externalContentHandler(res http.ResponseWriter, req *http.Request) {
// check if the content is Trustable should be auto-approved
trusted, ok := post.(Trustable)
if ok {
err := trusted.AutoApprove(req)
err := trusted.AutoApprove(res, req)
if err != nil {
log.Println("[External] error:", err)
res.WriteHeader(http.StatusInternalServerError)
log.Println("[External] error calling AutoApprove:", err)
return
}
} else {
Expand All @@ -164,19 +161,17 @@ func externalContentHandler(res http.ResponseWriter, req *http.Request) {

id, err := db.SetContent(t+spec+":-1", req.PostForm)
if err != nil {
log.Println("[External] error:", err)
res.WriteHeader(http.StatusInternalServerError)
log.Println("[External] error calling SetContent:", err)
return
}

// set the target in the context so user can get saved value from db in hook
ctx := context.WithValue(req.Context(), "target", fmt.Sprintf("%s:%d", t, id))
req = req.WithContext(ctx)

err = hook.AfterSave(req)
err = hook.AfterSave(res, req)
if err != nil {
log.Println("[External] error:", err)
res.WriteHeader(http.StatusInternalServerError)
log.Println("[External] error calling AfterSave:", err)
return
}

Expand Down
2 changes: 1 addition & 1 deletion system/api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func contentHandlerBySlug(res http.ResponseWriter, req *http.Request) {
func hide(it interface{}, res http.ResponseWriter, req *http.Request) bool {
// check if should be hidden
if h, ok := it.(item.Hideable); ok {
err := h.Hide(req)
err := h.Hide(res, req)
if err == item.ErrAllowHiddenItem {
return false
}
Expand Down
Loading

0 comments on commit 16a159a

Please sign in to comment.