add config
This commit is contained in:
parent
6e7f43dfdd
commit
e8f59e320d
1
brain.go
1
brain.go
|
@ -10,6 +10,7 @@ import (
|
|||
|
||||
// UnimplementedBrainServiceServer can be embedded to have forward compatible implementations.
|
||||
type BrainService struct {
|
||||
cnf Config
|
||||
IsRunningMain atomic.Int64
|
||||
}
|
||||
|
||||
|
|
41
config.go
41
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
|
||||
}
|
||||
|
|
6
config/test.yaml
Normal file
6
config/test.yaml
Normal 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
12
config_test.go
Normal 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
1
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
|
||||
)
|
||||
|
|
3
go.sum
3
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=
|
||||
|
|
Loading…
Reference in New Issue
Block a user