github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/example/echo/cmd/cli-echo/cmd_server.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"runtime"
     7  
     8  	"go.opentelemetry.io/otel/trace"
     9  
    10  	"github.com/ronaksoft/rony"
    11  	"github.com/ronaksoft/rony/config"
    12  	"github.com/ronaksoft/rony/edge"
    13  	"github.com/ronaksoft/rony/errors"
    14  	service "github.com/ronaksoft/rony/example/echo/rpc"
    15  	"github.com/spf13/cobra"
    16  )
    17  
    18  var edgeServer *edge.Server
    19  
    20  var ServerCmd = &cobra.Command{
    21  	Use: "server",
    22  	RunE: func(cmd *cobra.Command, args []string) error {
    23  		err := config.BindCmdFlags(cmd)
    24  		if err != nil {
    25  			return errors.WrapText("bind flag:")(err)
    26  		}
    27  
    28  		var tr trace.Tracer
    29  		if tp := initTracer(); tp != nil {
    30  			defer func() {
    31  				tp.Shutdown(context.Background())
    32  			}()
    33  			tr = tp.Tracer("SampleEchoServer")
    34  		}
    35  
    36  		// Instantiate the edge server
    37  		edgeServer = edge.NewServer(
    38  			config.GetString("server.id"),
    39  			edge.WithTracer(tr),
    40  			edge.WithTcpGateway(edge.TcpGatewayConfig{
    41  				Concurrency:   runtime.NumCPU() * 100,
    42  				ListenAddress: config.GetString("gateway.listen"),
    43  				MaxIdleTime:   config.GetDuration("idle-time"),
    44  				Protocol:      rony.TCP,
    45  				ExternalAddrs: config.GetStringSlice("gateway.advertise.url"),
    46  			}),
    47  		)
    48  
    49  		// Register the implemented service into the edge server
    50  		service.RegisterSample(&service.Sample{}, edgeServer)
    51  
    52  		// Start the edge server components
    53  		edgeServer.Start()
    54  
    55  		// Wait until a shutdown signal received.
    56  		edgeServer.WaitForSignal(os.Kill, os.Interrupt)
    57  		edgeServer.Shutdown()
    58  
    59  		return nil
    60  	},
    61  }