add config

This commit is contained in:
eson 2023-07-04 10:08:43 +08:00
parent 6e7f43dfdd
commit e8f59e320d
7 changed files with 73 additions and 0 deletions

View File

@ -10,6 +10,7 @@ import (
// UnimplementedBrainServiceServer can be embedded to have forward compatible implementations. // UnimplementedBrainServiceServer can be embedded to have forward compatible implementations.
type BrainService struct { type BrainService struct {
cnf Config
IsRunningMain atomic.Int64 IsRunningMain atomic.Int64
} }

View File

@ -1,5 +1,46 @@
package brain package brain
import (
"io"
"os"
"sync"
"gopkg.in/yaml.v3"
)
// Config 配置 // Config 配置
type Config struct { type Config struct {
cnfLock sync.Mutex
Cluster `yaml:"cluster"`
Self `yaml:"self"`
}
type Cluster struct {
Member []string `yaml:"member"`
}
type Self struct {
Addr string `yaml:"addr"`
Candidate struct {
Weight int `yaml:"weight"`
} `yaml:"candidate"`
}
func (cnf *Config) Load(pth string) error {
f, err := os.Open(pth)
if err != nil {
return err
}
data, err := io.ReadAll(f)
if err != nil {
return err
}
err = yaml.Unmarshal(data, cnf)
if err != nil {
return err
}
return nil
} }

6
config/test.yaml Normal file
View File

@ -0,0 +1,6 @@
cluster:
member: ["127.0.0.1:2020","127.0.0.1:3030"]
self:
addr: "127.0.0.1:4040"
candidate:
weight: 0

12
config_test.go Normal file
View File

@ -0,0 +1,12 @@
package brain
import (
"log"
"testing"
)
func TestCaseMain(t *testing.T) {
cnf := Config{}
cnf.Load("config/test.yaml")
log.Println(cnf)
}

1
go.mod
View File

@ -10,4 +10,5 @@ require (
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
google.golang.org/grpc v1.56.1 // indirect google.golang.org/grpc v1.56.1 // indirect
google.golang.org/protobuf v1.31.0 // indirect google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1
) )

3
go.sum
View File

@ -19,3 +19,6 @@ google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cn
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -1,4 +1,13 @@
package brain package brain
type State struct { type State struct {
Value int
}
func (state *State) Set(v int) {
state.Value = v
}
func (state *State) Get() int {
return state.Value
} }