github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/demo/rpc/hello_world/main.go (about)

     1  package main
     2  
     3  import (
     4  	"github.com/volts-dev/volts"
     5  	"github.com/volts-dev/volts/client"
     6  	"github.com/volts-dev/volts/internal/test"
     7  	"github.com/volts-dev/volts/logger"
     8  	"github.com/volts-dev/volts/router"
     9  	"github.com/volts-dev/volts/server"
    10  	"github.com/volts-dev/volts/transport"
    11  )
    12  
    13  type (
    14  	arith struct {
    15  	}
    16  
    17  	Arith interface {
    18  		Mul(hd *router.TRpcContext, args *test.Args, reply *test.Reply) error
    19  	}
    20  )
    21  
    22  func (t arith) Mul(hd *router.TRpcContext, args *test.Args, reply *test.Reply) error {
    23  	hd.Info("IP:")
    24  	reply.Flt = 0.01001
    25  	reply.Str = "Mul"
    26  	reply.Num = args.Num1 * args.Num2
    27  
    28  	hd.Info("Mul2", t, args, *reply)
    29  	return nil
    30  }
    31  
    32  func main() {
    33  	r := router.New()
    34  	r.Url("CONNECT", "Arith", new(arith))
    35  
    36  	srv := server.New(
    37  		server.WithRouter(r),
    38  	)
    39  
    40  	app := volts.New(
    41  		volts.Server(srv),
    42  		volts.Transport(transport.NewTCPTransport()),
    43  		volts.Debug(),
    44  	)
    45  
    46  	go app.Run()
    47  
    48  	service := "Arith.Mul"
    49  	endpoint := "Test.Endpoint"
    50  	address := "127.0.0.1:35999"
    51  	cli, _ := client.NewHttpClient()
    52  	req, _ := cli.NewRequest(service, endpoint, nil)
    53  
    54  	// test calling remote address
    55  	if _, err := cli.Call(req, client.WithAddress(address)); err != nil {
    56  		logger.Err("call with address error:", err)
    57  	}
    58  
    59  	<-make(chan byte)
    60  }