diff --git a/brain.go b/brain.go index 566b5ff..b5653fa 100644 --- a/brain.go +++ b/brain.go @@ -10,6 +10,7 @@ import ( // UnimplementedBrainServiceServer can be embedded to have forward compatible implementations. type BrainService struct { + cnf Config IsRunningMain atomic.Int64 } diff --git a/config.go b/config.go index accd1a9..e01da8a 100644 --- a/config.go +++ b/config.go @@ -1,5 +1,46 @@ package brain +import ( + "io" + "os" + "sync" + + "gopkg.in/yaml.v3" +) + // Config 配置 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 } diff --git a/config/test.yaml b/config/test.yaml new file mode 100644 index 0000000..d2312f3 --- /dev/null +++ b/config/test.yaml @@ -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 diff --git a/config_test.go b/config_test.go new file mode 100644 index 0000000..d90b31e --- /dev/null +++ b/config_test.go @@ -0,0 +1,12 @@ +package brain + +import ( + "log" + "testing" +) + +func TestCaseMain(t *testing.T) { + cnf := Config{} + cnf.Load("config/test.yaml") + log.Println(cnf) +} diff --git a/go.mod b/go.mod index 950e760..3852731 100644 --- a/go.mod +++ b/go.mod @@ -10,4 +10,5 @@ require ( google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect google.golang.org/grpc v1.56.1 // indirect google.golang.org/protobuf v1.31.0 // indirect + gopkg.in/yaml.v3 v3.0.1 ) diff --git a/go.sum b/go.sum index 59b7cd4..8aa865b 100644 --- a/go.sum +++ b/go.sum @@ -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.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= 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= diff --git a/state.go b/state.go index eff880f..61f6a63 100644 --- a/state.go +++ b/state.go @@ -1,4 +1,13 @@ package brain type State struct { + Value int +} + +func (state *State) Set(v int) { + state.Value = v +} + +func (state *State) Get() int { + return state.Value }