2021-12-06 12:24:22 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/cuigh/auxo/data"
|
|
|
|
"github.com/cuigh/auxo/net/web"
|
2021-12-22 09:43:26 +00:00
|
|
|
"github.com/cuigh/auxo/util/cast"
|
2021-12-06 12:24:22 +00:00
|
|
|
"github.com/cuigh/swirl/biz"
|
2022-01-06 08:54:14 +00:00
|
|
|
"github.com/cuigh/swirl/misc"
|
2021-12-06 12:24:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NodeHandler encapsulates node related handlers.
|
|
|
|
type NodeHandler struct {
|
2021-12-17 12:13:58 +00:00
|
|
|
List web.HandlerFunc `path:"/list" auth:"node.view" desc:"list nodes"`
|
2021-12-06 12:24:22 +00:00
|
|
|
Search web.HandlerFunc `path:"/search" auth:"node.view" desc:"search nodes"`
|
|
|
|
Find web.HandlerFunc `path:"/find" auth:"node.view" desc:"find node by name"`
|
|
|
|
Delete web.HandlerFunc `path:"/delete" method:"post" auth:"node.delete" desc:"delete node"`
|
|
|
|
Save web.HandlerFunc `path:"/save" method:"post" auth:"node.edit" desc:"create or update node"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNode creates an instance of NodeHandler
|
|
|
|
func NewNode(nb biz.NodeBiz) *NodeHandler {
|
|
|
|
return &NodeHandler{
|
2021-12-17 12:13:58 +00:00
|
|
|
List: nodeList(nb),
|
2021-12-06 12:24:22 +00:00
|
|
|
Search: nodeSearch(nb),
|
|
|
|
Find: nodeFind(nb),
|
|
|
|
Delete: nodeDelete(nb),
|
|
|
|
Save: nodeSave(nb),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-17 12:13:58 +00:00
|
|
|
func nodeList(nb biz.NodeBiz) web.HandlerFunc {
|
2022-01-06 08:54:14 +00:00
|
|
|
return func(c web.Context) error {
|
|
|
|
agent := cast.ToBool(c.Query("agent"))
|
2021-12-22 09:43:26 +00:00
|
|
|
nodes, err := nb.List(agent)
|
2021-12-17 12:13:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
return success(c, nodes)
|
2021-12-17 12:13:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 12:24:22 +00:00
|
|
|
func nodeSearch(nb biz.NodeBiz) web.HandlerFunc {
|
2022-01-06 08:54:14 +00:00
|
|
|
return func(c web.Context) error {
|
|
|
|
ctx, cancel := misc.Context(defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
nodes, err := nb.Search(ctx)
|
2021-12-06 12:24:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-06 08:54:14 +00:00
|
|
|
return success(c, nodes)
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func nodeFind(nb biz.NodeBiz) web.HandlerFunc {
|
2022-01-06 08:54:14 +00:00
|
|
|
return func(c web.Context) error {
|
|
|
|
ctx, cancel := misc.Context(defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
id := c.Query("id")
|
|
|
|
node, raw, err := nb.Find(ctx, id)
|
2021-12-06 12:24:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
return success(c, data.Map{"node": node, "raw": raw})
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func nodeDelete(nb biz.NodeBiz) web.HandlerFunc {
|
|
|
|
type Args struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
return func(c web.Context) (err error) {
|
2021-12-06 12:24:22 +00:00
|
|
|
args := &Args{}
|
2022-01-06 08:54:14 +00:00
|
|
|
if err = c.Bind(args); err == nil {
|
|
|
|
ctx, cancel := misc.Context(defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
err = nb.Delete(ctx, args.ID, args.Name, c.User())
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
return ajax(c, err)
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func nodeSave(nb biz.NodeBiz) web.HandlerFunc {
|
2022-01-06 08:54:14 +00:00
|
|
|
return func(c web.Context) error {
|
2021-12-06 12:24:22 +00:00
|
|
|
n := &biz.Node{}
|
2022-01-06 08:54:14 +00:00
|
|
|
err := c.Bind(n, true)
|
2021-12-06 12:24:22 +00:00
|
|
|
if err == nil {
|
2022-01-06 08:54:14 +00:00
|
|
|
ctx, cancel := misc.Context(defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
err = nb.Update(ctx, n, c.User())
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
return ajax(c, err)
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
}
|