Browse Source

Removed AfterResponse and replaced with signal code.

signals-integration
Benjamin Shelton 1 month ago
parent
commit
014a52aedb
7 changed files with 19 additions and 78 deletions
  1. +0
    -14
      api.go
  2. +11
    -29
      route.go
  3. +0
    -14
      router.go
  4. +4
    -15
      router_bind.go
  5. +0
    -3
      router_private.go
  6. +0
    -3
      routergroup.go
  7. +4
    -0
      signals.go

+ 0
- 14
api.go View File

@ -193,12 +193,6 @@ type BaseController struct {
// controller or may be set globally via a helper.
Renderer render.Renderer
// BeforeResponse bindings specifically used for this controller.
BeforeResponse []func(Context, *Route) error
// AfterResponse bindings specifically used for this controller.
AfterResponse []func(Context, error) error
// Index path override. This may contain a path component, a single slash
// indicating the path terminates with this index, a slash followed by a
// question mark which indicates the slash is optional, or a slash followed
@ -273,14 +267,6 @@ func (bc BaseController) renderer() render.Renderer {
return bc.Renderer
}
func (bc BaseController) beforeResponseHandlers() []func(Context, *Route) error {
return bc.BeforeResponse
}
func (bc BaseController) afterResponseHandlers() []func(Context, error) error {
return bc.AfterResponse
}
func (bc BaseController) webSocketPath() string {
return bc.WebSocketPath
}


+ 11
- 29
route.go View File

@ -146,16 +146,6 @@ type Route struct {
// Upgrader for websocket connections.
Upgrader *websocket.Upgrader
// AfterResponse call. If defined by the router, this function will be
// called after every response made to this route. If `error` contains a
// 400- or 500-level error, its value is returned instead of the value
// returned by the route's handler.
//
// AfterResponse will also receive the error returned by Route.Endpoint.
//
// This will be nil unless defined by the controller.
AfterResponse []func(Context, error) error
// Renderer defines a controller-specific renderer to use for this handler.
// If this is nil, the global renderer will be used instead.
Renderer render.Renderer
@ -191,7 +181,6 @@ func (rt *Route) Copy() *Route {
Controller: rt.Controller,
Middleware: rt.Middleware,
Endpoint: rt.Endpoint,
AfterResponse: rt.AfterResponse,
Renderer: rt.Renderer,
}
}
@ -316,25 +305,18 @@ func (rt *Route) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Handle endpoint.
err = rt.Endpoint(ctx)
// AfterResponse handlers act akin to a post-request middleware that can
// perform additional processing. AfterResponse handlers CANNOT change the
// request body being sent to the client, but they can interrupt the control
// flow by returning redirect codes.
//
// AfterResponse handlers are intended to perform any post-processing
// required immediately following a response. Though it may be possible,
// they are not intended to manipulate the result itself.
if err == nil && len(rt.AfterResponse) > 0 {
for _, fn := range rt.AfterResponse {
err = fn(ctx, err)
switch err.(type) {
case *ce.Redirect, *ce.Internal:
continue
case error:
goto ERRTYPES
// Signal: OnBeforeResponse.
signals.EmitFuncContext(SigAfterResponse,
func(fn interface{}) signals.Context {
var e signals.Context
if f, ok := fn.(OnAfterResponse); ok {
e = f(ctx, err)
if e != nil && e.Error() != nil {
err = e.Error()
}
}
}
}
return e
})
ERRTYPES:
logger := logging.MustInheritLogger("web.errors", "web")


+ 0
- 14
router.go View File

@ -122,12 +122,6 @@ type Router struct {
// TCP, TLS, and/or Unix sockets. See the Router.Has* functions.
namedListeners *namedListeners
// Slice containing all afterResponse callbacks.
//
// If this router is assigned to a RouteGroup, this will receive
// afterResponse targets from the parent Router.
afterResponse []func(Context, error) error
// Middleware assigned to this Router.
//
// If this router is assigned to a RouteGroup, this will be populated with
@ -311,14 +305,6 @@ func (r *Router) SetRenderer(renderer render.Renderer) {
r.renderer = renderer
}
// AfterResponseFuncc configures a function to call after the response has been
// handled. Response data will have already been dispatched by the time this
// function is called; consequently, care should be taken not to manipulate the
// response.
func (r *Router) AfterResponseFunc(fn func(Context, error) error) {
r.afterResponse = append(r.afterResponse, fn)
}
// Group creates and returns a new router group.
func (r *Router) Group(path string) *RouterGroup {
if _, ok := r.routerGroups[path]; ok {


+ 4
- 15
router_bind.go View File

@ -251,11 +251,6 @@ func (r *Router) Bind(endpoint Controller) error {
rt.Renderer.GlobalContext("_route", rt)
}
// Assign before/after response handlers.
if e, ok := endpoint.(BaseController); ok {
rt.AfterResponse = append(rt.AfterResponse, e.afterResponseHandlers()...)
}
if middleware != nil {
rt.Middleware = append(rt.Middleware, middleware...)
}
@ -298,11 +293,10 @@ func (r *Router) ManualBind(method, path, suffix string, controller Controller,
Method: method,
// FIXME: needs to be []MiddlewareFunc
//Middleware: controller.middleware(),
Controller: controller,
Renderer: controller.renderer(),
Suffix: suffix,
Endpoint: endpoint,
AfterResponse: make([]func(Context, error) error, len(r.afterResponse)),
Controller: controller,
Renderer: controller.renderer(),
Suffix: suffix,
Endpoint: endpoint,
}
r.cnames[route.Name] = struct{}{}
@ -333,11 +327,6 @@ func (r *Router) ManualBind(method, path, suffix string, controller Controller,
route.Renderer.GlobalContext("_route", route)
}
// Assign before/after response handlers.
if e, ok := controller.(BaseController); ok {
route.AfterResponse = append(route.AfterResponse, e.afterResponseHandlers()...)
}
if middleware != nil {
route.Middleware = append(route.Middleware, middleware...)
}


+ 0
- 3
router_private.go View File

@ -56,7 +56,6 @@ func (r *Router) toRoute(controller Controller) (*Route, *routeProps) {
Middleware: controller.middleware(),
Controller: controller,
Renderer: controller.renderer(),
AfterResponse: make([]func(Context, error) error, len(r.afterResponse)),
}
// Set route name based on either the controller, as provided, or the
@ -82,7 +81,5 @@ func (r *Router) toRoute(controller Controller) (*Route, *routeProps) {
}
}
copy(route.AfterResponse, r.afterResponse)
return route, props
}

+ 0
- 3
routergroup.go View File

@ -41,11 +41,8 @@ func NewRouterGroup(path string, parent *Router) *RouterGroup {
router.routes = rg.parent.routes
router.name = rg.name
router.renderer = rg.parent.renderer
router.afterResponse = make([]func(Context, error) error,
len(rg.parent.afterResponse))
router.reserved = rg.parent.reserved
copy(router.afterResponse, parent.afterResponse)
rg.router = router
rg.basePath = pth.Join(parent.basePath, path)


+ 4
- 0
signals.go View File

@ -31,6 +31,10 @@ var SigStartRouteHandler = signals.New((OnStartRouteHandler)(nil))
type OnStartRouteHandler func(*Route, http.ResponseWriter, *http.Request)
var SigAfterResponse = signals.New((OnAfterResponse)(nil))
type OnAfterResponse func(Context, error) signals.Context
var SigBeforeResponse = signals.New((OnBeforeResponse)(nil))
type OnBeforeResponse func(Context, *Route) signals.Context


Loading…
Cancel
Save