wheat-cache/storage/cmd/root.go

74 lines
1.6 KiB
Go

// Package cmd /*
package cmd
import (
"fmt"
"log"
"net"
"os"
"os/signal"
"syscall"
_ "gitee.com/timedb/wheatCache/conf"
"gitee.com/timedb/wheatCache/pkg/proto"
"gitee.com/timedb/wheatCache/storage/server"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "storage",
Short: "storage",
Long: `start storage server`,
Run: func(cmd *cobra.Command, args []string) {
storageServer := server.NewServer()
// 先写死, 等配置文件
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)
if err != nil {
log.Panicln(err)
}
s := grpc.NewServer()
proto.RegisterCommServerServer(s, storageServer)
reflection.Register(s)
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGHUP, syscall.SIGINT)
go func() {
select {
case <-c:
s.Stop()
msg := `
|-------Wheat tools---------|
| see you next time |
|thank you for your efforts |
|---------------------------|
`
fmt.Println(msg)
}
}()
if err := s.Serve(listen); err != nil {
log.Panicln(err)
}
},
}
// 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())
}