github.com/vmware/transport-go@v1.3.4/plank/cmd/main.go (about)

     1  // Copyright 2019-2021 VMware, Inc.
     2  // SPDX-License-Identifier: BSD-2-Clause
     3  
     4  package main
     5  
     6  import (
     7  	"github.com/spf13/cobra"
     8  	"github.com/vmware/transport-go/plank/pkg/server"
     9  	"github.com/vmware/transport-go/plank/services"
    10  	"github.com/vmware/transport-go/plank/utils"
    11  	"os"
    12  )
    13  
    14  var version string
    15  
    16  func main() {
    17  	var serverConfig *server.PlatformServerConfig
    18  
    19  	// define the root command - entry of our application
    20  	app := &cobra.Command{
    21  		Use:     "plank",
    22  		Version: version,
    23  		Short:   "Plank demo application",
    24  	}
    25  
    26  	// define a command that starts the Plank server
    27  	startCmd := &cobra.Command{
    28  		Use:   "start-server",
    29  		Short: "Start Plank server",
    30  		RunE: func(cmd *cobra.Command, args []string) error {
    31  			var platformServer server.PlatformServer
    32  			platformServer = server.NewPlatformServer(serverConfig)
    33  
    34  			// register services
    35  
    36  			// ping-pong service
    37  			if err := platformServer.RegisterService(services.NewPingPongService(),
    38  				services.PingPongServiceChan); err != nil {
    39  				return err
    40  			}
    41  
    42  			// stock ticker service
    43  			if err := platformServer.RegisterService(services.NewStockTickerService(),
    44  				services.StockTickerServiceChannel); err != nil {
    45  				return err
    46  			}
    47  
    48  			// simple stream service
    49  			if err := platformServer.RegisterService(services.NewSimpleStreamService(),
    50  				services.SimpleStreamServiceChannel); err != nil {
    51  				return err
    52  			}
    53  
    54  			// joke service
    55  			if err := platformServer.RegisterService(services.NewJokeService(),
    56  				services.JokeServiceChannel); err != nil {
    57  				return err
    58  			}
    59  
    60  			// start server
    61  			sysChan := make(chan os.Signal, 1)
    62  			platformServer.StartServer(sysChan)
    63  
    64  			return nil
    65  		},
    66  	}
    67  
    68  	// create a new server configuration. this Cobra variant of the server.CreateServerConfig() function
    69  	// configures and parses flags from the command line arguments into Cobra Command's structure. otherwise,
    70  	// it is identical to server.CreateServerConfig() which you can use if you don't want to use Cobra.
    71  	serverConfig, err := server.CreateServerConfigForCobraCommand(startCmd)
    72  	if err != nil {
    73  		utils.Log.Fatalln(err)
    74  	}
    75  
    76  	// add startCmd command to app
    77  	app.AddCommand(startCmd)
    78  
    79  	// start the app
    80  	if err = app.Execute(); err != nil {
    81  		utils.Log.Fatalln(err)
    82  	}
    83  }