11package main
22
33import "github.com/samuel/go-zookeeper/zk"
4- import yaml "gopkg.in/yaml.v2"
54
65import (
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.
1716type 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
2428var (
@@ -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+
8383func 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