wheat-cache/storage/cmd/root.go

59 lines
1.4 KiB
Go
Raw Normal View History

2021-08-15 15:19:22 +08:00
// Package cmd /*
2021-08-14 23:21:40 +08:00
package cmd
import (
"fmt"
2021-08-31 23:31:31 +08:00
"log"
"net"
2021-08-14 23:21:40 +08:00
2021-11-02 14:45:08 +08:00
"gitee.com/wheat-os/wheatCache/storage/service"
2021-10-15 22:35:16 +08:00
2021-11-02 14:45:08 +08:00
_ "gitee.com/wheat-os/wheatCache/conf"
"gitee.com/wheat-os/wheatCache/pkg/logx"
"gitee.com/wheat-os/wheatCache/pkg/proto"
"gitee.com/wheat-os/wheatCache/pkg/util/server"
2021-09-05 15:00:39 +08:00
"github.com/spf13/cobra"
2021-09-05 17:08:35 +08:00
"github.com/spf13/viper"
2021-09-05 15:00:39 +08:00
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
2021-08-14 23:21:40 +08:00
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "storage",
2021-08-31 23:31:31 +08:00
Short: "storage",
Long: `start storage server`,
Run: func(cmd *cobra.Command, args []string) {
2021-10-27 23:53:25 +08:00
storageServer := service.NewSingleServer()
2021-09-05 17:08:35 +08:00
conf := viper.GetStringMap("storage")
host := conf["host"].(string)
port := conf["port"].(int)
tcpAddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", host, port))
if err != nil {
log.Fatalln(err)
}
listen, err := net.ListenTCP("tcp", tcpAddr)
2021-08-31 23:31:31 +08:00
if err != nil {
2021-10-15 22:35:16 +08:00
logx.Panicln(err)
2021-08-31 23:31:31 +08:00
}
s := grpc.NewServer()
proto.RegisterCommServerServer(s, storageServer)
reflection.Register(s)
2021-10-16 22:51:51 +08:00
server.ElegantExitServer(s)
2021-08-31 23:31:31 +08:00
2021-10-24 00:34:16 +08:00
logx.Info("start storage in addr: %s", tcpAddr.String())
2021-08-31 23:31:31 +08:00
if err := s.Serve(listen); err != nil {
2021-10-16 22:51:51 +08:00
logx.Errorln(err)
2021-08-31 23:31:31 +08:00
}
},
2021-08-14 23:21:40 +08:00
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
cobra.CheckErr(rootCmd.Execute())
}