From d191e00b79d092bbeaaaae788f2670b4fd29b5b1 Mon Sep 17 00:00:00 2001 From: Doflatango Date: Wed, 18 Jan 2017 16:58:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A3=80=E6=9F=A5=E7=94=A8=E4=BD=9Ckey?= =?UTF-8?q?=E7=9A=84id=E5=92=8C=E5=90=8D=E7=A7=B0=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E8=8E=B7=E5=8F=96=E6=B4=BB=E5=8A=A8=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E7=9A=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .editorconfig | 2 +- .gitignore | 1 + models/client.go | 5 +++++ models/errors.go | 7 +++++-- models/group.go | 5 +++++ models/job.go | 9 +++++++++ models/node.go | 15 +++++++++++++++ web/node.go | 11 +++++++++-- web/routers.go | 2 ++ 9 files changed, 52 insertions(+), 5 deletions(-) diff --git a/.editorconfig b/.editorconfig index 36ecc6d..72813d1 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,6 +1,6 @@ # for vscode extension[editorconfig] root = true -[*.{js,json,html}] +[*.{js,json,html,vue}] indent_style = space indent_size = 2 \ No newline at end of file diff --git a/.gitignore b/.gitignore index 5867049..a41a17d 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ conf/files/*.json .tags_sorted_by_file bin/*/*server .DS_Store +web/ui/node_modules \ No newline at end of file diff --git a/models/client.go b/models/client.go index 9b5c093..fbf8b6b 100644 --- a/models/client.go +++ b/models/client.go @@ -7,6 +7,7 @@ import ( client "github.com/coreos/etcd/clientv3" + "strings" "sunteng/cronsun/conf" ) @@ -103,3 +104,7 @@ func (c *Client) Watch(key string, opts ...client.OpOption) client.WatchChan { defer cancel() return c.Client.Watch(ctx, key, opts...) } + +func IsValidAsKeyPath(s string) bool { + return strings.IndexByte(s, '/') == -1 +} diff --git a/models/errors.go b/models/errors.go index 4e2c837..39836a4 100644 --- a/models/errors.go +++ b/models/errors.go @@ -6,8 +6,11 @@ var ( ErrNotFound = errors.New("Record not found.") ErrValueMayChanged = errors.New("The value has been changed by others on this time.") - ErrEmptyJobName = errors.New("Name of job is empty.") - ErrEmptyJobCommand = errors.New("Command of job is empty.") + ErrEmptyJobName = errors.New("Name of job is empty.") + ErrEmptyJobCommand = errors.New("Command of job is empty.") + ErrIllegalJobId = errors.New("Invalid id that includes illegal characters such as '/'.") + ErrIllegalJobGroupName = errors.New("Invalid job group name that includes illegal characters such as '/'.") ErrEmptyNodeGroupName = errors.New("Name of node group is empty.") + ErrIllegalNodeGroupId = errors.New("Invalid node group id that includes illegal characters such as '/'.") ) diff --git a/models/group.go b/models/group.go index 567eac1..013cd33 100644 --- a/models/group.go +++ b/models/group.go @@ -77,6 +77,11 @@ func (g *Group) Put(rev int64) (*client.PutResponse, error) { } func (g *Group) Check() error { + g.ID = strings.TrimSpace(g.ID) + if !IsValidAsKeyPath(g.ID) { + return ErrIllegalNodeGroupId + } + g.Name = strings.TrimSpace(g.Name) if len(g.Name) == 0 { return ErrEmptyNodeGroupName diff --git a/models/job.go b/models/job.go index 0c26760..098b8d1 100644 --- a/models/job.go +++ b/models/job.go @@ -135,6 +135,11 @@ func (j *Job) Key() string { } func (j *Job) Check() error { + j.ID = strings.TrimSpace(j.ID) + if !IsValidAsKeyPath(j.ID) { + return ErrIllegalJobId + } + j.Name = strings.TrimSpace(j.Name) if len(j.Name) == 0 { return ErrEmptyJobName @@ -145,6 +150,10 @@ func (j *Job) Check() error { j.Group = DefaultJobGroup } + if !IsValidAsKeyPath(j.Group) { + return ErrIllegalJobGroupName + } + // 不修改 Command 的内容,简单判断是否为空 if len(strings.TrimSpace(j.Command)) == 0 { return ErrEmptyJobCommand diff --git a/models/node.go b/models/node.go index a8a9314..bbe432f 100644 --- a/models/node.go +++ b/models/node.go @@ -60,3 +60,18 @@ func (n *Node) Exist() (pid int, err error) { return -1, nil } + +var procKeyLen = len(conf.Config.Proc) + +func GetActivityNodeList() (ids []string, err error) { + resp, err := DefalutClient.Get(conf.Config.Proc, client.WithPrefix(), client.WithKeysOnly()) + if err != nil { + return + } + + for _, n := range resp.Kvs { + ids = append(ids, string(n.Key[procKeyLen:])) + } + + return +} diff --git a/web/node.go b/web/node.go index c98e4a7..1e30976 100644 --- a/web/node.go +++ b/web/node.go @@ -32,8 +32,6 @@ func (n *Node) UpdateGroup(w http.ResponseWriter, r *http.Request) { if len(g.ID) == 0 { successCode = http.StatusCreated g.ID = models.NextID() - } else { - } if err = g.Check(); err != nil { @@ -98,3 +96,12 @@ func (n *Node) DeleteGroup(w http.ResponseWriter, r *http.Request) { outJSONWithCode(w, http.StatusNoContent, nil) } + +func (n *Node) GetActivityNodeList(w http.ResponseWriter, r *http.Request) { + ids, err := models.GetActivityNodeList() + if err != nil { + outJSONError(w, http.StatusInternalServerError, err.Error()) + return + } + outJSONWithCode(w, http.StatusOK, ids) +} diff --git a/web/routers.go b/web/routers.go index 76ed77e..4839104 100644 --- a/web/routers.go +++ b/web/routers.go @@ -32,6 +32,8 @@ func InitRouters() (s *http.Server, err error) { h = BaseHandler{Handle: jobHandler.DeleteJob} subrouter.Handle("/job/{group}-{id}", h).Methods("DELETE") + h = BaseHandler{Handle: nodeHandler.GetActivityNodeList} + subrouter.Handle("/node/activitys", h).Methods("GET") // get node group list h = BaseHandler{Handle: nodeHandler.GetGroups} subrouter.Handle("/node/groups", h).Methods("GET")