From 43392236b6391207452fe4a2fbbde9c761742cd7 Mon Sep 17 00:00:00 2001 From: bandl <1658002533@qq.com> Date: Sat, 16 Oct 2021 22:52:37 +0800 Subject: [PATCH] feat(gateway): add gateway root --- gateway/cmd/root.go | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/gateway/cmd/root.go b/gateway/cmd/root.go index f81b95b..d98e590 100644 --- a/gateway/cmd/root.go +++ b/gateway/cmd/root.go @@ -1,7 +1,17 @@ package cmd import ( + "fmt" + "net" + + _ "gitee.com/timedb/wheatCache/conf" + wheatCodec "gitee.com/timedb/wheatCache/gateway/codec" + "gitee.com/timedb/wheatCache/gateway/proxy" + "gitee.com/timedb/wheatCache/pkg/logx" + "gitee.com/timedb/wheatCache/pkg/util/server" "github.com/spf13/cobra" + "github.com/spf13/viper" + "google.golang.org/grpc" ) // rootCmd represents the base command when called without any subcommands @@ -9,7 +19,27 @@ var rootCmd = &cobra.Command{ Use: "getway", Short: "getway", Long: `start getway server`, - Run: func(cmd *cobra.Command, args []string) {}, + Run: func(cmd *cobra.Command, args []string) { + host := viper.GetString("gateway.host") + port := viper.GetInt("gateway.port") + + tcpAddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", host, port)) + if err != nil { + logx.Panic("get gateway addr err:%v", err) + } + listen, err := net.ListenTCP("tcp", tcpAddr) + if err != nil { + logx.Panic("get gateway tcp conn err:%v", err) + } + + gatewayServer := GetGatewayServer() + server.ElegantExitServer(gatewayServer) + + logx.Info("start gateway in addr: %s", tcpAddr.String()) + if err := gatewayServer.Serve(listen); err != nil { + logx.Errorln(err) + } + }, } // Execute adds all child commands to the root command and sets flags appropriately. @@ -17,3 +47,14 @@ var rootCmd = &cobra.Command{ func Execute() { cobra.CheckErr(rootCmd.Execute()) } + +func GetGatewayServer() *grpc.Server { + opts := make([]grpc.ServerOption, 0) + opts = append( + opts, + grpc.ForceServerCodec(wheatCodec.Codec()), + grpc.UnknownServiceHandler(proxy.TransparentHandler(proxy.GetDirectorByServiceHash())), + ) + + return grpc.NewServer(opts...) +}