github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/router/middleware/event/event.go (about)

     1  package event
     2  
     3  import (
     4  	"github.com/volts-dev/volts/router"
     5  )
     6  
     7  type (
     8  	httpEvent interface {
     9  		Before(ctx *router.THttpContext)
    10  		After(ctx *router.THttpContext)
    11  	}
    12  
    13  	rpcEvent interface {
    14  		Before(ctx *router.TRpcContext)
    15  		After(ctx *router.TRpcContext)
    16  	}
    17  
    18  	TEvent struct {
    19  	}
    20  )
    21  
    22  func NewEvent(router router.IRouter) router.IMiddleware {
    23  	return &TEvent{}
    24  }
    25  
    26  func (self *TEvent) Name() string {
    27  	return "event"
    28  }
    29  
    30  func (self *TEvent) Handler(ctx router.IContext) {
    31  	ctrl := ctx.Handler().Controller()
    32  	switch v := ctrl.(type) {
    33  	case *router.THttpContext:
    34  		if c, ok := ctrl.(httpEvent); ok {
    35  			c.Before(v)
    36  			ctx.Next()
    37  			c.After(v)
    38  		}
    39  
    40  	case *router.TRpcContext:
    41  		if c, ok := ctrl.(rpcEvent); ok {
    42  			c.Before(v)
    43  			ctx.Next()
    44  			c.After(v)
    45  		}
    46  	}
    47  }