!15 添加 配置文件的使用

Merge pull request !15 from bandl/feat-project-conf
This commit is contained in:
bandl 2021-09-09 03:13:22 +00:00 committed by Gitee
commit 414cd6d9a7
9 changed files with 130 additions and 50 deletions

1
.gitignore vendored
View File

@ -13,6 +13,7 @@
*.out
.idea
.vscode
# build file

56
conf/public_conf.go Normal file
View File

@ -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
}

36
conf/public_conf_test.go Normal file
View File

@ -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")
}

8
conf/wheat-cache.yaml Normal file
View File

@ -0,0 +1,8 @@
version: 'v1.0'
env: 'dev'
storage:
host: '127.0.0.1'
port: 5890

2
go.mod
View File

@ -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
)

3
go.sum
View File

@ -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=

View File

@ -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
@cd storage && go build -o $(BASE_OUT)/storage
.PHONY: install
install:
@make build

View File

@ -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())
}
}

View File

@ -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()