Skip to content

Commit c787ac3

Browse files
committed
modify ServerConfig.json format.
1 parent 853c4ce commit c787ac3

3 files changed

Lines changed: 65 additions & 77 deletions

File tree

ServerConfig.json

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
{
2-
"websitehost": "192.168.2.80:8901",
3-
"centerhost": "192.168.2.80:8985",
4-
"storage": {
5-
"mongo": {
6-
"hosts": "192.168.2.80:27017,192.168.2.81:27017,192.168.2.82:27017",
7-
"database": "cloudtask",
8-
"auth": {
9-
"user": "datastoreAdmin",
10-
"password": "ds4dev"
11-
},
12-
"options": [
13-
"maxPoolSize=20",
14-
"replicaSet=mgoCluster",
15-
"authSource=admin"
16-
]
17-
}
18-
}
2+
"zookeeper": {
3+
"hosts": "192.168.2.80:2181,192.168.2.81:2181,192.168.2.82:2181",
4+
"root": "/cloudtask"
5+
},
6+
"serverconfig":{
7+
"websitehost": "192.168.2.80:8901",
8+
"centerhost": "192.168.2.80:8985",
9+
"storage": {
10+
"mongo": {
11+
"hosts": "192.168.2.80:27017,192.168.2.81:27017,192.168.2.82:27017",
12+
"database": "cloudtask",
13+
"auth": {
14+
"user": "datastoreAdmin",
15+
"password": "ds4dev"
16+
},
17+
"options": [
18+
"maxPoolSize=20",
19+
"replicaSet=mgoCluster",
20+
"authSource=admin"
21+
]
22+
}
23+
}
24+
}
1925
}

config.yaml

Lines changed: 0 additions & 3 deletions
This file was deleted.

main.go

Lines changed: 42 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package main
22

33
import "github.com/samuel/go-zookeeper/zk"
4-
import yaml "gopkg.in/yaml.v2"
54

65
import (
7-
"flag"
6+
"bytes"
7+
"encoding/json"
88
"fmt"
99
"io/ioutil"
10-
"os"
1110
"strings"
1211
"time"
1312
)
@@ -16,9 +15,14 @@ import (
1615
//zookeeper cluster parameters.
1716
type Configuration struct {
1817
Zookeeper struct {
19-
Hosts string `yaml:"hosts" json:"hosts"`
20-
Root string `yaml:"root" json:"root"`
21-
} `yaml:"zookeeper" json:"zookeeper"`
18+
Hosts string `json:"hosts"`
19+
Root string `json:"root"`
20+
} `json:"zookeeper"`
21+
ServerConfig struct {
22+
WebsiteHost string `json:"websitehost"`
23+
CenterHost string `json:"centerhost"`
24+
Storage map[string]interface{} `json:"storage"`
25+
} `json:"serverconfig"`
2226
}
2327

2428
var (
@@ -27,47 +31,28 @@ var (
2731
zkConnTimeout = time.Second * 15
2832
)
2933

30-
func readConfiguration(file string) (*Configuration, error) {
34+
func initServerConfigData(conf *Configuration) (string, []byte, error) {
3135

32-
fd, err := os.OpenFile(file, os.O_RDONLY, 0777)
33-
if err != nil {
34-
return nil, err
35-
}
36-
37-
defer fd.Close()
38-
buf, err := ioutil.ReadAll(fd)
39-
if err != nil {
40-
return nil, err
41-
}
42-
43-
conf := &Configuration{}
44-
if err := yaml.Unmarshal(buf, conf); err != nil {
45-
return nil, err
46-
}
47-
return conf, nil
48-
}
49-
50-
func initServerConfigData(hosts string, root string) (string, []byte, error) {
51-
52-
servers := strings.Split(hosts, ",")
53-
conn, event, err := zk.Connect(servers, zkConnTimeout)
36+
hosts := strings.Split(conf.Zookeeper.Hosts, ",")
37+
conn, event, err := zk.Connect(hosts, zkConnTimeout)
5438
if err != nil {
5539
return "", nil, err
5640
}
5741

5842
<-event
5943
defer conn.Close()
60-
serverConfigPath := root + "/ServerConfig"
44+
serverConfigPath := conf.Zookeeper.Root + "/ServerConfig"
6145
ret, _, err := conn.Exists(serverConfigPath)
6246
if err != nil {
6347
return "", nil, err
6448
}
6549

66-
data, err := ioutil.ReadFile("./ServerConfig.json")
67-
if err != nil {
68-
return "", nil, fmt.Errorf("ServerConfig.json read failure, %s", err)
50+
buf := bytes.NewBuffer([]byte{})
51+
if err = json.NewEncoder(buf).Encode(conf.ServerConfig); err != nil {
52+
return "", nil, err
6953
}
7054

55+
data := buf.Bytes()
7156
if !ret {
7257
if _, err := conn.Create(serverConfigPath, data, zkFlags, zkACL); err != nil {
7358
return "", nil, err
@@ -80,38 +65,38 @@ func initServerConfigData(hosts string, root string) (string, []byte, error) {
8065
return serverConfigPath, data, nil
8166
}
8267

68+
func readConfiguration() (*Configuration, error) {
69+
70+
data, err := ioutil.ReadFile("./ServerConfig.json")
71+
if err != nil {
72+
return nil, err
73+
}
74+
75+
conf := &Configuration{}
76+
err = json.NewDecoder(bytes.NewBuffer(data)).Decode(conf)
77+
if err != nil {
78+
return nil, err
79+
}
80+
return conf, nil
81+
}
82+
8383
func main() {
8484

85-
var (
86-
configFile string
87-
zkHosts string
88-
zkRoot string
89-
)
90-
91-
flag.StringVar(&configFile, "f", "./config.yaml", "coudtask initconfig etc.")
92-
flag.StringVar(&zkHosts, "hosts", "127.0.0.1:2181", "zookeeper hosts.")
93-
flag.StringVar(&zkRoot, "root", "/cloudtask", "zookeeper root path.")
94-
flag.Parse()
95-
96-
if configFile != "" {
97-
conf, err := readConfiguration(configFile)
98-
if err != nil {
99-
fmt.Errorf("config file invalid, %s", err)
100-
return
101-
}
102-
zkHosts = conf.Zookeeper.Hosts
103-
zkRoot = conf.Zookeeper.Root
85+
conf, err := readConfiguration()
86+
if err != nil {
87+
fmt.Errorf("ServerConfig.json invalid, %s", err)
88+
return
10489
}
10590

106-
if ret := strings.HasPrefix(zkRoot, "/"); !ret {
107-
zkRoot = "/" + zkRoot
91+
if ret := strings.HasPrefix(conf.Zookeeper.Root, "/"); !ret {
92+
conf.Zookeeper.Root = "/" + conf.Zookeeper.Root
10893
}
10994

110-
if ret := strings.HasSuffix(zkRoot, "/"); ret {
111-
zkRoot = strings.TrimSuffix(zkRoot, "/")
95+
if ret := strings.HasSuffix(conf.Zookeeper.Root, "/"); ret {
96+
conf.Zookeeper.Root = strings.TrimSuffix(conf.Zookeeper.Root, "/")
11297
}
11398

114-
path, data, err := initServerConfigData(zkHosts, zkRoot)
99+
path, data, err := initServerConfigData(conf)
115100
if err != nil {
116101
fmt.Errorf("init server config failure, %s", err)
117102
return

0 commit comments

Comments
 (0)