From 890c2862327e959411671001c8cc59b319ea57c0 Mon Sep 17 00:00:00 2001 From: Ollie Phillips Date: Fri, 29 Mar 2019 10:36:20 +0000 Subject: api before/after response hooks --- system/item/item.go | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) (limited to 'system/item') diff --git a/system/item/item.go b/system/item/item.go index 1108ae0..51ac2a9 100644 --- a/system/item/item.go +++ b/system/item/item.go @@ -24,13 +24,13 @@ func init() { // We store the compiled regex as the key // and assign the replacement as the map's value. rxList = map[*regexp.Regexp][]byte{ - regexp.MustCompile("`[-]+`"): []byte("-"), - regexp.MustCompile("[[:space:]]"): []byte("-"), - regexp.MustCompile("[[:blank:]]"): []byte(""), - regexp.MustCompile("`[^a-z0-9]`i"): []byte("-"), - regexp.MustCompile("[!/:-@[-`{-~]"): []byte(""), - regexp.MustCompile("/[^\x20-\x7F]/"): []byte(""), - regexp.MustCompile("`&(amp;)?#?[a-z0-9]+;`i"): []byte("-"), + regexp.MustCompile("`[-]+`"): []byte("-"), + regexp.MustCompile("[[:space:]]"): []byte("-"), + regexp.MustCompile("[[:blank:]]"): []byte(""), + regexp.MustCompile("`[^a-z0-9]`i"): []byte("-"), + regexp.MustCompile("[!/:-@[-`{-~]"): []byte(""), + regexp.MustCompile("/[^\x20-\x7F]/"): []byte(""), + regexp.MustCompile("`&(amp;)?#?[a-z0-9]+;`i"): []byte("-"), regexp.MustCompile("`&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);`i"): []byte("\\1"), } } @@ -65,6 +65,9 @@ type Sortable interface { // to the different lifecycles/events a struct may encounter. Item implements // Hookable with no-ops so our user can override only whichever ones necessary. type Hookable interface { + BeforeAPIResponse(http.ResponseWriter, *http.Request) error + AfterAPIResponse(http.ResponseWriter, *http.Request) error + BeforeAPICreate(http.ResponseWriter, *http.Request) error AfterAPICreate(http.ResponseWriter, *http.Request) error @@ -177,6 +180,16 @@ func (i Item) String() string { return fmt.Sprintf("Item ID: %s", i.UniqueID()) } +// BeforeAPIResponse is a no-op to ensure structs which embed Item implement Hookable +func (i Item) BeforeAPIResponse(res http.ResponseWriter, req *http.Request) error { + return nil +} + +// AfterAPIResponse is a no-op to ensure structs which embed Item implement Hookable +func (i Item) AfterAPIResponse(res http.ResponseWriter, req *http.Request) error { + return nil +} + // BeforeAPICreate is a no-op to ensure structs which embed Item implement Hookable func (i Item) BeforeAPICreate(res http.ResponseWriter, req *http.Request) error { return nil -- cgit v1.2.3 From 7063494d6c82e784878b6fca79e9442c43578954 Mon Sep 17 00:00:00 2001 From: Ollie Phillips Date: Fri, 29 Mar 2019 12:05:42 +0000 Subject: passing data to hooks --- system/item/item.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'system/item') diff --git a/system/item/item.go b/system/item/item.go index 51ac2a9..8be21ae 100644 --- a/system/item/item.go +++ b/system/item/item.go @@ -65,8 +65,8 @@ type Sortable interface { // to the different lifecycles/events a struct may encounter. Item implements // Hookable with no-ops so our user can override only whichever ones necessary. type Hookable interface { - BeforeAPIResponse(http.ResponseWriter, *http.Request) error - AfterAPIResponse(http.ResponseWriter, *http.Request) error + BeforeAPIResponse(http.ResponseWriter, *http.Request, []byte) ([]byte, error) + AfterAPIResponse(http.ResponseWriter, *http.Request, []byte) ([]byte, error) BeforeAPICreate(http.ResponseWriter, *http.Request) error AfterAPICreate(http.ResponseWriter, *http.Request) error @@ -181,13 +181,13 @@ func (i Item) String() string { } // BeforeAPIResponse is a no-op to ensure structs which embed Item implement Hookable -func (i Item) BeforeAPIResponse(res http.ResponseWriter, req *http.Request) error { - return nil +func (i Item) BeforeAPIResponse(res http.ResponseWriter, req *http.Request, data []byte) ([]byte, error) { + return data, nil } // AfterAPIResponse is a no-op to ensure structs which embed Item implement Hookable -func (i Item) AfterAPIResponse(res http.ResponseWriter, req *http.Request) error { - return nil +func (i Item) AfterAPIResponse(res http.ResponseWriter, req *http.Request, data []byte) ([]byte, error) { + return data, nil } // BeforeAPICreate is a no-op to ensure structs which embed Item implement Hookable -- cgit v1.2.3 From 878d593ef5da1435c83bf7bb229f8619c1cab9ff Mon Sep 17 00:00:00 2001 From: Ollie Phillips Date: Thu, 4 Apr 2019 15:30:15 +0100 Subject: removed []byte return on AfterAPIResponse, not needed) --- system/item/item.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'system/item') diff --git a/system/item/item.go b/system/item/item.go index 8be21ae..34ced58 100644 --- a/system/item/item.go +++ b/system/item/item.go @@ -66,7 +66,7 @@ type Sortable interface { // Hookable with no-ops so our user can override only whichever ones necessary. type Hookable interface { BeforeAPIResponse(http.ResponseWriter, *http.Request, []byte) ([]byte, error) - AfterAPIResponse(http.ResponseWriter, *http.Request, []byte) ([]byte, error) + AfterAPIResponse(http.ResponseWriter, *http.Request, []byte) error BeforeAPICreate(http.ResponseWriter, *http.Request) error AfterAPICreate(http.ResponseWriter, *http.Request) error @@ -186,8 +186,8 @@ func (i Item) BeforeAPIResponse(res http.ResponseWriter, req *http.Request, data } // AfterAPIResponse is a no-op to ensure structs which embed Item implement Hookable -func (i Item) AfterAPIResponse(res http.ResponseWriter, req *http.Request, data []byte) ([]byte, error) { - return data, nil +func (i Item) AfterAPIResponse(res http.ResponseWriter, req *http.Request, data []byte) error { + return nil } // BeforeAPICreate is a no-op to ensure structs which embed Item implement Hookable -- cgit v1.2.3