Browse Source

Changed signal method signatures to include context passing.

master
Benjamin Shelton 3 weeks ago
parent
commit
aee85ed483
3 changed files with 29 additions and 36 deletions
  1. +2
    -2
      extension/csrf.go
  2. +19
    -27
      route.go
  3. +8
    -7
      signals.go

+ 2
- 2
extension/csrf.go View File

@ -467,13 +467,13 @@ func (c *CSRF) Init(server capstan.Application) {
c.hostname = server.Config().Server.CalculatedHostname()
signals.Connect(capstan.SigBeforeResponse,
capstan.OnBeforeResponse(func(cctx capstan.Context, r *capstan.Route) signals.Context {
capstan.OnBeforeResponse(func(cctx capstan.Context, r *capstan.Route,
sctx signals.Context) {
var cx signals.Context
if err := c.beforeResponse(cctx, r); err != nil {
cx = signals.NewContext()
cx.SetError(err)
}
return cx
}))
server.Dependencies().Register(c.options.InjectedName, c)


+ 19
- 27
route.go View File

@ -219,23 +219,20 @@ func (rt *Route) FullPath() string {
// handling logic. Refactoring is welcome.
func (rt *Route) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var err error
var sctx signals.Context
sctx := signals.NewContext()
ctx := rt.contextMaker(w, r, rt)
// Signal: OnStartRouteHandler.
c := signals.EmitFuncContext(SigStartRouteHandler, func(fn interface{}) signals.Context {
var ct signals.Context
signals.EmitFunc(SigStartRouteHandler, func(fn interface{}) {
if f, ok := fn.(OnStartRouteHandler); ok {
ct = f(rt, ctx)
f(rt, ctx, sctx)
}
return ct
})
if c.Abort() {
if sctx.Abort() {
goto ERRTYPES
}
if c.Error() != nil {
err = c.Error()
if sctx.Error() != nil {
err = sctx.Error()
if code := errors.Guarantee(err).Code(); code >= 400 {
ctx.SetError(err)
ctx.SetCode(code)
@ -260,13 +257,11 @@ func (rt *Route) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
// Signal: OnBeforeResponse.
sctx = signals.EmitFuncContext(SigBeforeResponse,
func(fn interface{}) signals.Context {
var e signals.Context
signals.EmitFunc(SigBeforeResponse,
func(fn interface{}) {
if f, ok := fn.(OnBeforeResponse); ok {
return f(ctx, rt)
f(ctx, rt, sctx)
}
return e
})
if sctx.Error() != nil {
err = sctx.Error()
@ -301,7 +296,7 @@ func (rt *Route) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Signal: OnConnectionUpgraded.
signals.EmitFunc(SigConnectionUpgraded, func(fn interface{}) {
if f, ok := fn.(OnConnectionUpgraded); ok {
f(ctx)
f(ctx, sctx)
}
})
}
@ -318,13 +313,11 @@ func (rt *Route) ServeHTTP(w http.ResponseWriter, r *http.Request) {
err = rt.Endpoint(ctx)
// Signal: OnAfterResponse.
sctx = signals.EmitFuncContext(SigAfterResponse,
func(fn interface{}) signals.Context {
if f, ok := fn.(OnAfterResponse); ok {
return f(ctx, err)
}
return nil
})
signals.EmitFunc(SigAfterResponse, func(fn interface{}) {
if f, ok := fn.(OnAfterResponse); ok {
f(ctx, sctx, err)
}
})
if sctx.Error() != nil {
err = sctx.Error()
}
@ -348,16 +341,15 @@ ERRTYPES:
// returns a non-zero error code, that code will be returned by the HTTP
// endpoint.
if err != nil {
signals.EmitFuncContext(SigRouteError,
func(fn interface{}) signals.Context {
signals.EmitFunc(SigRouteError,
func(fn interface{}) {
var e signals.Context
if f, ok := fn.(OnRouteError); ok {
e = f(rt, ctx, err)
f(rt, ctx, sctx, err)
if e != nil && e.Error() != nil {
err = e.Error()
}
}
return e
})
}
@ -410,7 +402,7 @@ ERRTYPES:
// Signal: OnEndRouteHandler.
signals.EmitFunc(SigEndRouteHandler, func(fn interface{}) {
if f, ok := fn.(OnEndRouteHandler); ok {
f(rt, ctx)
f(rt, ctx, sctx)
}
})
}


+ 8
- 7
signals.go View File

@ -67,7 +67,7 @@ var SigStartRouteHandler = signals.New((OnStartRouteHandler)(nil))
//
// Access to the raw net/http.ResponseWriter and net/http.Request can be had
// through the Context.Response() and Context.Request() methods.
type OnStartRouteHandler func(*Route, Context) signals.Context
type OnStartRouteHandler func(*Route, Context, signals.Context)
// SigEndRouteHandler emits when the route has finished all code paths and is
// preparing to exit. It is not possible to modify the response, response
@ -76,10 +76,11 @@ type OnStartRouteHandler func(*Route, Context) signals.Context
// extensions that have hooked into the route signaling.
var SigEndRouteHandler = signals.New((OnEndRouteHandler)(nil))
// OnEndRouteHandler accepts a capstan.Route and capstan.Context.
// OnEndRouteHandler accepts a capstan.Route, capstan.Context, and the current
// signals.Context as returned by OnStartRouteHandler.
//
// The capstan.Route cannot be modified in this phase.
type OnEndRouteHandler func(*Route, Context)
type OnEndRouteHandler func(*Route, Context, signals.Context)
// SigBeforeResponse emits after the response context and headers have been
// setup but before any potential protocol upgrades are made (e.g. WebSockets).
@ -88,7 +89,7 @@ var SigBeforeResponse = signals.New((OnBeforeResponse)(nil))
// OnBeforeResponse accepts the Capstan Context and Route. If a signals.Context
// value is returned and its Error() value is set with a code >= 400, this will
// skip immediately to the route's error handling.
type OnBeforeResponse func(Context, *Route) signals.Context
type OnBeforeResponse func(Context, *Route, signals.Context)
// SigAfterResponse emits after the route has been handled and a response is
// in-flight but before any error handling code is processed.
@ -102,7 +103,7 @@ var SigAfterResponse = signals.New((OnAfterResponse)(nil))
// OnAfterResponse accepts the Capstan Context and a possible error value. Note
// that the error value may be nil, capstan.Context, capstan/errors.Redirect,
// capstan/errors.Internal, or of error.
type OnAfterResponse func(Context, error) signals.Context
type OnAfterResponse func(Context, signals.Context, error)
// SigConnectionUpgraded emits when the route has been configured to produce a
// connection upgrade header, such as for WebSockets. This signal will not be
@ -112,7 +113,7 @@ var SigConnectionUpgraded = signals.New((OnConnectionUpgraded)(nil))
// OnConnectionUpgraded accepts the capstan.Context and its state immediately
// after a successful upgrade.
type OnConnectionUpgraded func(Context)
type OnConnectionUpgraded func(Context, signals.Context)
// SigRouteError emits immediately after any error condition has been raised but
// will not emit if the handled route's endpoint has returned nil (this is in
@ -132,7 +133,7 @@ var SigRouteError = signals.New((OnRouteError)(nil))
// as well as Context.HasError(). If a capstan.Context has been returned and
// there is an error condition, both Context.HasError() and Context.Error() will
// return useful information.
type OnRouteError func(*Route, Context, error) signals.Context
type OnRouteError func(*Route, Context, signals.Context, error)
// SigRouteWriteError emits when an error condition has been reached but prior
// to the error being written to the HTTP client. This signal may be used to


Loading…
Cancel
Save