forked from p93542168/wheat-cache
commit
414cd6d9a7
|
@ -13,6 +13,7 @@
|
||||||
*.out
|
*.out
|
||||||
|
|
||||||
.idea
|
.idea
|
||||||
|
.vscode
|
||||||
|
|
||||||
|
|
||||||
# build file
|
# build 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
|
||||||
|
}
|
|
@ -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")
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
version: 'v1.0'
|
||||||
|
|
||||||
|
env: 'dev'
|
||||||
|
|
||||||
|
storage:
|
||||||
|
host: '127.0.0.1'
|
||||||
|
port: 5890
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -5,6 +5,8 @@ go 1.16
|
||||||
require (
|
require (
|
||||||
github.com/spf13/cobra v1.2.1
|
github.com/spf13/cobra v1.2.1
|
||||||
github.com/spf13/viper v1.8.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/grpc v1.38.0
|
||||||
google.golang.org/protobuf v1.26.0
|
google.golang.org/protobuf v1.26.0
|
||||||
)
|
)
|
||||||
|
|
3
go.sum
3
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-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-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-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-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/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.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
|
8
makefile
8
makefile
|
@ -3,8 +3,12 @@ STORAGE_PATH = $(BASE_PATH)/storage
|
||||||
BASE_OUT = $(BASE_PATH)/bin
|
BASE_OUT = $(BASE_PATH)/bin
|
||||||
|
|
||||||
dcgen:
|
dcgen:
|
||||||
python3 ./shell/proto.py
|
@python3 ./shell/proto.py
|
||||||
|
|
||||||
.PHONY : build
|
.PHONY : build
|
||||||
build:
|
build:
|
||||||
cd storage && go build -o $(BASE_OUT)/storage
|
@cd storage && go build -o $(BASE_OUT)/storage
|
||||||
|
|
||||||
|
.PHONY: install
|
||||||
|
install:
|
||||||
|
@make build
|
|
@ -3,22 +3,21 @@ package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"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"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"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"
|
"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
|
// rootCmd represents the base command when called without any subcommands
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "storage",
|
Use: "storage",
|
||||||
|
@ -27,7 +26,16 @@ var rootCmd = &cobra.Command{
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
storageServer := server.NewServer()
|
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 {
|
if err != nil {
|
||||||
log.Panicln(err)
|
log.Panicln(err)
|
||||||
}
|
}
|
||||||
|
@ -63,41 +71,3 @@ var rootCmd = &cobra.Command{
|
||||||
func Execute() {
|
func Execute() {
|
||||||
cobra.CheckErr(rootCmd.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())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -15,7 +15,9 @@ limitations under the License.
|
||||||
*/
|
*/
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "gitee.com/timedb/wheatCache/storage/cmd"
|
import (
|
||||||
|
"gitee.com/timedb/wheatCache/storage/cmd"
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cmd.Execute()
|
cmd.Execute()
|
||||||
|
|
Loading…
Reference in New Issue