github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/internal/test/controller.go (about)

     1  package test
     2  
     3  import (
     4  	"github.com/volts-dev/volts/router"
     5  	"github.com/volts-dev/volts/router/middleware/event"
     6  )
     7  
     8  type (
     9  	ArithCtrl struct {
    10  	}
    11  
    12  	CtrlWithMiddleware struct {
    13  		event.TEvent
    14  		Session *TestSession
    15  	}
    16  )
    17  
    18  func (ctrl CtrlWithMiddleware) Init(cfg *router.ControllerConfig) {
    19  	cfg.AddFilter(ctrl.Session.Name(), "WithoutMiddlware")
    20  }
    21  
    22  func (ctrl CtrlWithMiddleware) Before(ctx *router.THttpContext) {
    23  	ctx.Write([]byte("event:Before"))
    24  }
    25  
    26  func (ctrl CtrlWithMiddleware) After(ctx *router.THttpContext) {
    27  	ctx.Write([]byte("event:After"))
    28  }
    29  
    30  func (ctrl CtrlWithMiddleware) HelloWorld(ctx *router.THttpContext) {
    31  	ctx.Write([]byte("Hello World!"))
    32  }
    33  
    34  func (ctrl CtrlWithMiddleware) WithoutMiddlware(ctx *router.THttpContext) {
    35  	ctx.Write([]byte("WithoutMiddlware"))
    36  }
    37  
    38  func (t ArithCtrl) Mul(ctx *router.TRpcContext) {
    39  	log.Info("IP:")
    40  	//hd.Request().
    41  	arg := Args{}
    42  	reply := &Reply{}
    43  	err := ctx.Request().Body().Decode(&arg)
    44  	if err != nil {
    45  		reply.Str = err.Error()
    46  		ctx.Response().WriteStream(reply)
    47  		return
    48  	}
    49  
    50  	reply.Num = arg.Num1 * arg.Num2
    51  	reply.Flt = 0.01001 * float64(arg.Num2)
    52  	reply.Str = "Mul"
    53  	//reply.Num = args.Num1 * args.Num2
    54  
    55  	//hd.Info("Mul2", t, args, *reply)
    56  	err = ctx.Response().WriteStream(reply)
    57  	if err != nil {
    58  		ctx.Abort(err.Error())
    59  	}
    60  	return
    61  }