diff --git a/main.go b/main.go index 148a16c..226b93e 100644 --- a/main.go +++ b/main.go @@ -9,13 +9,15 @@ import ( var ( zk string - zk_path string + zkPath string tick int + listen string ) func init() { flag.StringVar(&zk, "zk", "127.0.0.1:2181", "Zookeeper servers comma separated") - flag.StringVar(&zk_path, "zk_path", "/rbmd", "Zookeeper path") + flag.StringVar(&zkPath, "zkPath", "/rbmd", "Zookeeper path") + flag.StringVar(&listen, "listen", "0.0.0.0:9076", "HTTP API listen address") flag.IntVar(&tick, "tick", 5, "Tick time loop") flag.Parse() } @@ -23,8 +25,9 @@ func init() { func main() { config := rbmd.Zk{ strings.Split(zk, ","), - zk_path, + zkPath, tick, } - rbmd.Run(config) + s := rbmd.ServerConf{listen} + rbmd.Run(config, s) } diff --git a/src/rbmd/http.go b/src/rbmd/http.go new file mode 100644 index 0000000..cda9b12 --- /dev/null +++ b/src/rbmd/http.go @@ -0,0 +1,51 @@ +package rbmd + +import ( + "net/http" + "log" + "encoding/json" +) + +//ServerConf configuration of http api server +type ServerConf struct { + Addr string +} + +//ServeHTTP start http server +func (s ServerConf) ServeHTTP(z ZooNode, fqdn string) { + + // Return JSON of full quorum status + http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) { + w.Write(z.GetState()) + }) + + // Return string with quorum health check result + http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte(z.GetQuorumHealth())) + }) + + // Return JSON of node description + http.HandleFunc("/node", func(w http.ResponseWriter, r *http.Request) { + n := GetNodeState(fqdn) + state, err := json.Marshal(n) + if err != nil { + log.Fatal(err) + } + w.Write(state) + }) + + // Mount volume. Accept JSON. Return JSON. + http.HandleFunc("/mount", func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("Not implemented yet.")) + }) + + // Umount volume. Accept JSON. Return JSON. + http.HandleFunc("/umount", func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("Not implemented yet.")) + }) + + server := &http.Server{ + Addr: s.Addr, + } + log.Fatal(server.ListenAndServe()) +} diff --git a/src/rbmd/state.go b/src/rbmd/state.go index e94e15b..3b8f0fc 100644 --- a/src/rbmd/state.go +++ b/src/rbmd/state.go @@ -11,7 +11,7 @@ import ( ) //Run -- start main loop -func Run(zoo Zk) { +func Run(zoo Zk, s ServerConf) { connection, err := zoo.InitConnection() if err != nil { log.Fatal(err) @@ -20,6 +20,9 @@ func Run(zoo Zk) { z := ZooNode{zoo.Path, connection} + // Serve HTTP API + go s.ServeHTTP(z, fqdn) + for { node, err := z.EnsureZooPath(strings.Join([]string{"cluster/", fqdn, "/state"}, "")) if err != nil { @@ -52,4 +55,14 @@ func (z ZooNode) UpdateState(zkPath string, fqdn string) { } } +//GetState return cluster status +func (z ZooNode) GetState() []byte { + quorumStatePath := strings.Join([]string{z.Path, "/log/quorum"}, "") + stateJSON, _, err := z.Conn.Get(quorumStatePath) + if err != nil { + log.Fatal(err) + } + + return stateJSON +}