检查用作key的id和名称,添加获取活动节点的接口

pull/1/head
Doflatango 2017-01-18 16:58:47 +08:00 committed by miraclesu
parent d3e88e39ba
commit d191e00b79
9 changed files with 52 additions and 5 deletions

View File

@ -1,6 +1,6 @@
# for vscode extension[editorconfig] # for vscode extension[editorconfig]
root = true root = true
[*.{js,json,html}] [*.{js,json,html,vue}]
indent_style = space indent_style = space
indent_size = 2 indent_size = 2

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ conf/files/*.json
.tags_sorted_by_file .tags_sorted_by_file
bin/*/*server bin/*/*server
.DS_Store .DS_Store
web/ui/node_modules

View File

@ -7,6 +7,7 @@ import (
client "github.com/coreos/etcd/clientv3" client "github.com/coreos/etcd/clientv3"
"strings"
"sunteng/cronsun/conf" "sunteng/cronsun/conf"
) )
@ -103,3 +104,7 @@ func (c *Client) Watch(key string, opts ...client.OpOption) client.WatchChan {
defer cancel() defer cancel()
return c.Client.Watch(ctx, key, opts...) return c.Client.Watch(ctx, key, opts...)
} }
func IsValidAsKeyPath(s string) bool {
return strings.IndexByte(s, '/') == -1
}

View File

@ -6,8 +6,11 @@ var (
ErrNotFound = errors.New("Record not found.") ErrNotFound = errors.New("Record not found.")
ErrValueMayChanged = errors.New("The value has been changed by others on this time.") ErrValueMayChanged = errors.New("The value has been changed by others on this time.")
ErrEmptyJobName = errors.New("Name of job is empty.") ErrEmptyJobName = errors.New("Name of job is empty.")
ErrEmptyJobCommand = errors.New("Command 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.") ErrEmptyNodeGroupName = errors.New("Name of node group is empty.")
ErrIllegalNodeGroupId = errors.New("Invalid node group id that includes illegal characters such as '/'.")
) )

View File

@ -77,6 +77,11 @@ func (g *Group) Put(rev int64) (*client.PutResponse, error) {
} }
func (g *Group) Check() error { func (g *Group) Check() error {
g.ID = strings.TrimSpace(g.ID)
if !IsValidAsKeyPath(g.ID) {
return ErrIllegalNodeGroupId
}
g.Name = strings.TrimSpace(g.Name) g.Name = strings.TrimSpace(g.Name)
if len(g.Name) == 0 { if len(g.Name) == 0 {
return ErrEmptyNodeGroupName return ErrEmptyNodeGroupName

View File

@ -135,6 +135,11 @@ func (j *Job) Key() string {
} }
func (j *Job) Check() error { func (j *Job) Check() error {
j.ID = strings.TrimSpace(j.ID)
if !IsValidAsKeyPath(j.ID) {
return ErrIllegalJobId
}
j.Name = strings.TrimSpace(j.Name) j.Name = strings.TrimSpace(j.Name)
if len(j.Name) == 0 { if len(j.Name) == 0 {
return ErrEmptyJobName return ErrEmptyJobName
@ -145,6 +150,10 @@ func (j *Job) Check() error {
j.Group = DefaultJobGroup j.Group = DefaultJobGroup
} }
if !IsValidAsKeyPath(j.Group) {
return ErrIllegalJobGroupName
}
// 不修改 Command 的内容,简单判断是否为空 // 不修改 Command 的内容,简单判断是否为空
if len(strings.TrimSpace(j.Command)) == 0 { if len(strings.TrimSpace(j.Command)) == 0 {
return ErrEmptyJobCommand return ErrEmptyJobCommand

View File

@ -60,3 +60,18 @@ func (n *Node) Exist() (pid int, err error) {
return -1, nil 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
}

View File

@ -32,8 +32,6 @@ func (n *Node) UpdateGroup(w http.ResponseWriter, r *http.Request) {
if len(g.ID) == 0 { if len(g.ID) == 0 {
successCode = http.StatusCreated successCode = http.StatusCreated
g.ID = models.NextID() g.ID = models.NextID()
} else {
} }
if err = g.Check(); err != nil { 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) 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)
}

View File

@ -32,6 +32,8 @@ func InitRouters() (s *http.Server, err error) {
h = BaseHandler{Handle: jobHandler.DeleteJob} h = BaseHandler{Handle: jobHandler.DeleteJob}
subrouter.Handle("/job/{group}-{id}", h).Methods("DELETE") subrouter.Handle("/job/{group}-{id}", h).Methods("DELETE")
h = BaseHandler{Handle: nodeHandler.GetActivityNodeList}
subrouter.Handle("/node/activitys", h).Methods("GET")
// get node group list // get node group list
h = BaseHandler{Handle: nodeHandler.GetGroups} h = BaseHandler{Handle: nodeHandler.GetGroups}
subrouter.Handle("/node/groups", h).Methods("GET") subrouter.Handle("/node/groups", h).Methods("GET")