cronsun/web/node.go

108 lines
2.4 KiB
Go
Raw Normal View History

2017-01-09 02:32:14 +00:00
package web
2017-01-11 08:12:37 +00:00
import (
"encoding/json"
"net/http"
"strings"
2017-01-12 08:35:30 +00:00
v3 "github.com/coreos/etcd/clientv3"
2017-01-11 08:12:37 +00:00
"github.com/gorilla/mux"
"sunteng/commons/log"
"sunteng/cronsun/conf"
"sunteng/cronsun/models"
)
type Node struct{}
var ngKeyDeepLen = len(conf.Config.Group)
2017-01-12 08:35:30 +00:00
func (n *Node) UpdateGroup(w http.ResponseWriter, r *http.Request) {
g := models.Group{}
de := json.NewDecoder(r.Body)
var err error
if err = de.Decode(&g); err != nil {
outJSONError(w, http.StatusBadRequest, err.Error())
2017-01-11 08:12:37 +00:00
return
}
2017-01-12 08:35:30 +00:00
defer r.Body.Close()
2017-01-11 08:12:37 +00:00
2017-01-12 08:35:30 +00:00
var successCode = http.StatusOK
g.ID = strings.TrimSpace(g.ID)
if len(g.ID) == 0 {
successCode = http.StatusCreated
g.ID = models.NextID()
}
if err = g.Check(); err != nil {
outJSONError(w, http.StatusBadRequest, err.Error())
return
2017-01-11 08:12:37 +00:00
}
// @TODO modRev
var modRev int64 = 0
if _, err = g.Put(modRev); err != nil {
2017-01-12 08:35:30 +00:00
outJSONError(w, http.StatusBadRequest, err.Error())
return
2017-01-11 08:12:37 +00:00
}
2017-01-12 08:35:30 +00:00
outJSONWithCode(w, successCode, nil)
2017-01-11 08:12:37 +00:00
}
2017-01-12 08:35:30 +00:00
func (n *Node) GetGroups(w http.ResponseWriter, r *http.Request) {
resp, err := models.DefalutClient.Get(conf.Config.Group, v3.WithPrefix(), v3.WithSort(v3.SortByKey, v3.SortAscend))
2017-01-11 08:12:37 +00:00
if err != nil {
outJSONError(w, http.StatusInternalServerError, err.Error())
return
}
2017-01-12 08:35:30 +00:00
var list = make([]*models.Group, 0, resp.Count)
2017-01-11 08:12:37 +00:00
for i := range resp.Kvs {
2017-01-12 08:35:30 +00:00
g := models.Group{}
err = json.Unmarshal(resp.Kvs[i].Value, &g)
2017-01-11 08:12:37 +00:00
if err != nil {
2017-01-12 08:35:30 +00:00
log.Errorf("node.GetGroups(key: %s) error: %s", string(resp.Kvs[i].Key), err.Error())
2017-01-11 08:12:37 +00:00
outJSONError(w, http.StatusInternalServerError, err.Error())
return
}
2017-01-12 08:35:30 +00:00
list = append(list, &g)
2017-01-11 08:12:37 +00:00
}
2017-01-12 08:35:30 +00:00
outJSON(w, list)
2017-01-11 08:12:37 +00:00
}
2017-01-12 08:35:30 +00:00
func (n *Node) GetGroupByGroupId(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
g, err := models.GetGroupById(vars["id"])
2017-01-11 08:12:37 +00:00
if err != nil {
outJSONError(w, http.StatusInternalServerError, err.Error())
return
}
2017-01-12 08:35:30 +00:00
if g == nil {
outJSONWithCode(w, http.StatusNotFound, nil)
2017-01-11 08:12:37 +00:00
return
}
2017-01-12 08:35:30 +00:00
outJSON(w, g)
2017-01-11 08:12:37 +00:00
}
2017-01-12 08:35:30 +00:00
func (n *Node) DeleteGroup(w http.ResponseWriter, r *http.Request) {
2017-01-16 06:30:55 +00:00
vars := mux.Vars(r)
_, err := models.DeleteGroupById(vars["id"])
if err != nil {
outJSONError(w, http.StatusInternalServerError, err.Error())
return
}
outJSONWithCode(w, http.StatusNoContent, nil)
2017-01-12 08:35:30 +00:00
}
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)
}