From 413254b9d2e6cc2d93d170925f0f766e7faccb29 Mon Sep 17 00:00:00 2001 From: miraclesu Date: Mon, 9 Jan 2017 11:10:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86=E7=9B=AE=E5=BD=95=E7=BB=93?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/web/server.go | 7 ++++--- conf/files/web.json.sample | 4 ++-- etcd.go | 22 ---------------------- job.go => models/job.go | 2 +- node.go => models/node.go | 2 +- web/handlers.go | 29 +++++++++++++++++++++-------- 6 files changed, 29 insertions(+), 37 deletions(-) delete mode 100644 etcd.go rename job.go => models/job.go (95%) rename node.go => models/node.go (86%) diff --git a/bin/web/server.go b/bin/web/server.go index f840167..daa579e 100644 --- a/bin/web/server.go +++ b/bin/web/server.go @@ -7,7 +7,6 @@ import ( "sunteng/commons/event" "sunteng/commons/log" - "sunteng/cronsun" "sunteng/cronsun/conf" "sunteng/cronsun/web" ) @@ -15,7 +14,8 @@ import ( func main() { l, err := net.Listen("tcp", conf.Config.Web.BindAddr) if err != nil { - cronsun.Fatalln(err) + log.Error(err.Error()) + return } // Create a cmux. @@ -23,7 +23,8 @@ func main() { httpL := m.Match(cmux.HTTP1Fast()) httpServer, err := web.InitRouters() if err != nil { - cronsun.Fatalln(err) + log.Error(err.Error()) + return } go httpServer.Serve(httpL) diff --git a/conf/files/web.json.sample b/conf/files/web.json.sample index afe30df..4565abd 100644 --- a/conf/files/web.json.sample +++ b/conf/files/web.json.sample @@ -1,4 +1,4 @@ { - BindAddr: ":7079", - UIDir: "" + "BindAddr": ":7079", + "UIDir": "" } \ No newline at end of file diff --git a/etcd.go b/etcd.go deleted file mode 100644 index 2a0c753..0000000 --- a/etcd.go +++ /dev/null @@ -1,22 +0,0 @@ -package cronsun - -import ( - "sunteng/cronsun/conf" - - "github.com/coreos/etcd/clientv3" -) - -var etcdClient *clientv3.Client - -func EtcdInstance() (*clientv3.Client, error) { - if etcdClient != nil { - return etcdClient, nil - } - - if err := conf.Init(); err != nil { - return nil, err - } - - etcdClient, err := clientv3.New(conf.Config.Etcd) - return etcdClient, err -} diff --git a/job.go b/models/job.go similarity index 95% rename from job.go rename to models/job.go index 32d8a73..3b074ff 100644 --- a/job.go +++ b/models/job.go @@ -1,4 +1,4 @@ -package cronsun +package models type Job struct { Id string `json:"id"` diff --git a/node.go b/models/node.go similarity index 86% rename from node.go rename to models/node.go index f60a4bf..a2bf84d 100644 --- a/node.go +++ b/models/node.go @@ -1,4 +1,4 @@ -package cronsun +package models type Node struct { Pid int `json:"pid"` diff --git a/web/handlers.go b/web/handlers.go index b6aaeef..dd4fedd 100644 --- a/web/handlers.go +++ b/web/handlers.go @@ -4,6 +4,7 @@ import ( "crypto/sha1" "encoding/hex" "encoding/json" + "fmt" "net/http" "path" "sort" @@ -15,16 +16,28 @@ import ( "github.com/gorilla/mux" "golang.org/x/net/context" - "fmt" "sunteng/commons/log" - "sunteng/cronsun" "sunteng/cronsun/conf" + "sunteng/cronsun/models" ) var etcdClient *clientv3.Client +func EtcdInstance() (*clientv3.Client, error) { + if etcdClient != nil { + return etcdClient, nil + } + + if err := conf.Init(); err != nil { + return nil, err + } + + etcdClient, err := clientv3.New(conf.Config.Etcd) + return etcdClient, err +} + func InitRouters() (s *http.Server, err error) { - etcdClient, err = cronsun.EtcdInstance() + etcdClient, err = EtcdInstance() if err != nil { return nil, err } @@ -91,9 +104,9 @@ func getJobsByGroupName(w http.ResponseWriter, r *http.Request) { return } - var jobList = make([]*cronsun.Job, 0, resp.Count) + var jobList = make([]*models.Job, 0, resp.Count) for i := range resp.Kvs { - job := &cronsun.Job{} + job := &models.Job{} err = json.Unmarshal(resp.Kvs[i].Value, &job) if err != nil { outJSONError(w, http.StatusInternalServerError, err.Error()) @@ -106,7 +119,7 @@ func getJobsByGroupName(w http.ResponseWriter, r *http.Request) { } func updateJob(w http.ResponseWriter, r *http.Request) { - job := &cronsun.Job{} + job := &models.Job{} decoder := json.NewDecoder(r.Body) err := decoder.Decode(&job) if err != nil { @@ -174,9 +187,9 @@ func getNodeGroupByName(w http.ResponseWriter, r *http.Request) { return } - var nodeList = make([]*cronsun.Node, 0, resp.Count) + var nodeList = make([]*models.Node, 0, resp.Count) for i := range resp.Kvs { - node := &cronsun.Node{} + node := &models.Node{} err = json.Unmarshal(resp.Kvs[i].Value, &node) if err != nil { outJSONError(w, http.StatusInternalServerError, err.Error())