diff --git a/.gitignore b/.gitignore index 826c304..6c5c7d6 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ *.out .idea +.vscode # build file diff --git a/conf/public_conf.go b/conf/public_conf.go new file mode 100644 index 0000000..56153da --- /dev/null +++ b/conf/public_conf.go @@ -0,0 +1,56 @@ +package conf + +import ( + "log" + + "github.com/spf13/viper" +) + +const ( + linuxPath = "/etc/wheat-cache/" + + devPath = "./conf" + devPathBin = "../conf" +) + +func init() { + setDefaultConfValue() + err := LoadConf("") + switch err.(type) { + case nil: + case viper.ConfigFileNotFoundError: + formatPath := []string{linuxPath, devPath, devPath} + log.Fatalf("The profile could not be read, read path:%v", formatPath) + default: + log.Fatalf("The resolution of the profile failed, err: %v", err) + + } +} + +func setDefaultConfValue() { + // 设置一些默认值 + viper.Set("version", "base-01") +} + +func LoadConf(path string) error { + if path != "" { + viper.AddConfigPath(path) + } + viper.SetConfigName("wheat-cache") + + // 添加默认读取地址 + // linux + viper.AddConfigPath(linuxPath) + + // 开发环境 + viper.AddConfigPath(devPath) + viper.AddConfigPath(devPathBin) + + viper.SetConfigType("yaml") + + err := viper.ReadInConfig() + if err != nil { + return err + } + return nil +} diff --git a/conf/public_conf_test.go b/conf/public_conf_test.go new file mode 100644 index 0000000..addb4b2 --- /dev/null +++ b/conf/public_conf_test.go @@ -0,0 +1,36 @@ +package conf + +import ( + "testing" + + "github.com/spf13/viper" + "github.com/stretchr/testify/require" +) + +func TestLoadConf(t *testing.T) { + type args struct { + path string + } + tests := []struct { + name string + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := LoadConf(tt.args.path); (err != nil) != tt.wantErr { + t.Errorf("LoadConf() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } + +} + +func TestConf(t *testing.T) { + viper.Set("host", "1222") + host := viper.GetString("host") + + require.Equal(t, host, "1222") +} diff --git a/conf/wheat-cache.yaml b/conf/wheat-cache.yaml new file mode 100644 index 0000000..4475627 --- /dev/null +++ b/conf/wheat-cache.yaml @@ -0,0 +1,8 @@ +version: 'v1.0' + +env: 'dev' + +storage: + host: '127.0.0.1' + port: 5890 + diff --git a/go.mod b/go.mod index d654f1b..8dd54cf 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,8 @@ go 1.16 require ( github.com/spf13/cobra v1.2.1 github.com/spf13/viper v1.8.1 + github.com/stretchr/testify v1.7.0 + golang.org/x/sys v0.0.0-20210903071746-97244b99971b // indirect google.golang.org/grpc v1.38.0 google.golang.org/protobuf v1.26.0 ) diff --git a/go.sum b/go.sum index 795c424..4cb7eeb 100644 --- a/go.sum +++ b/go.sum @@ -397,8 +397,9 @@ golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210903071746-97244b99971b h1:3Dq0eVHn0uaQJmPO+/aYPI/fRMqdrVDbu7MQcku54gg= +golang.org/x/sys v0.0.0-20210903071746-97244b99971b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/makefile b/makefile index 93023d6..b4525c1 100644 --- a/makefile +++ b/makefile @@ -3,8 +3,12 @@ STORAGE_PATH = $(BASE_PATH)/storage BASE_OUT = $(BASE_PATH)/bin dcgen: - python3 ./shell/proto.py + @python3 ./shell/proto.py .PHONY : build build: - cd storage && go build -o $(BASE_OUT)/storage \ No newline at end of file + @cd storage && go build -o $(BASE_OUT)/storage + +.PHONY: install +install: + @make build \ No newline at end of file diff --git a/storage/cmd/root.go b/storage/cmd/root.go index d26625a..0a6b201 100644 --- a/storage/cmd/root.go +++ b/storage/cmd/root.go @@ -3,22 +3,21 @@ package cmd import ( "fmt" - "gitee.com/timedb/wheatCache/storage/proto" - "gitee.com/timedb/wheatCache/storage/server" - "github.com/spf13/cobra" - "google.golang.org/grpc" - "google.golang.org/grpc/reflection" "log" "net" "os" "os/signal" "syscall" + _ "gitee.com/timedb/wheatCache/conf" + "gitee.com/timedb/wheatCache/storage/proto" + "gitee.com/timedb/wheatCache/storage/server" + "github.com/spf13/cobra" "github.com/spf13/viper" + "google.golang.org/grpc" + "google.golang.org/grpc/reflection" ) -var cfgFile string - // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "storage", @@ -27,7 +26,16 @@ var rootCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { storageServer := server.NewServer() // 先写死, 等配置文件 - listen, err := net.Listen("tcp", ":5201") + 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) } @@ -63,41 +71,3 @@ var rootCmd = &cobra.Command{ func Execute() { cobra.CheckErr(rootCmd.Execute()) } - -func init() { - cobra.OnInitialize(initConfig) - - // Here you will define your flags and configuration settings. - // Cobra supports persistent flags, which, if defined here, - // will be global for your application. - - rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.storage.yaml)") - - // Cobra also supports local flags, which will only run - // when this action is called directly. - rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") -} - -// initConfig reads in config file and ENV variables if set. -func initConfig() { - if cfgFile != "" { - // Use config file from the flag. - viper.SetConfigFile(cfgFile) - } else { - // Find home directory. - home, err := os.UserHomeDir() - cobra.CheckErr(err) - - // Search config in home directory with name ".storage" (without extension). - viper.AddConfigPath(home) - viper.SetConfigType("yaml") - viper.SetConfigName(".storage") - } - - viper.AutomaticEnv() // read in environment variables that match - - // If a config file is found, read it in. - if err := viper.ReadInConfig(); err == nil { - fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) - } -} diff --git a/storage/main.go b/storage/main.go index caf97ea..15638de 100644 --- a/storage/main.go +++ b/storage/main.go @@ -15,7 +15,9 @@ limitations under the License. */ package main -import "gitee.com/timedb/wheatCache/storage/cmd" +import ( + "gitee.com/timedb/wheatCache/storage/cmd" +) func main() { cmd.Execute()