swirl/api/secret.go

108 lines
2.5 KiB
Go
Raw Permalink Normal View History

2021-12-06 12:24:22 +00:00
package api
import (
"github.com/cuigh/auxo/data"
"github.com/cuigh/auxo/net/web"
"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
)
// SecretHandler encapsulates secret related handlers.
type SecretHandler struct {
Search web.HandlerFunc `path:"/search" auth:"secret.view" desc:"search secrets"`
Find web.HandlerFunc `path:"/find" auth:"secret.view" desc:"find secret by name"`
Delete web.HandlerFunc `path:"/delete" method:"post" auth:"secret.delete" desc:"delete secret"`
Save web.HandlerFunc `path:"/save" method:"post" auth:"secret.edit" desc:"create or update secret"`
}
// NewSecret creates an instance of SecretHandler
func NewSecret(b biz.SecretBiz) *SecretHandler {
return &SecretHandler{
Search: secretSearch(b),
Find: secretFind(b),
Delete: secretDelete(b),
Save: secretSave(b),
}
}
func secretSearch(b biz.SecretBiz) web.HandlerFunc {
type Args struct {
Name string `json:"name" bind:"name"`
PageIndex int `json:"pageIndex" bind:"pageIndex"`
PageSize int `json:"pageSize" bind:"pageSize"`
}
2022-01-06 08:54:14 +00:00
return func(c web.Context) (err error) {
2021-12-06 12:24:22 +00:00
var (
args = &Args{}
secrets []*biz.Secret
total int
)
2022-01-06 08:54:14 +00:00
if err = c.Bind(args); err == nil {
ctx, cancel := misc.Context(defaultTimeout)
defer cancel()
secrets, total, err = b.Search(ctx, args.Name, args.PageIndex, args.PageSize)
2021-12-06 12:24:22 +00:00
}
if err != nil {
return
}
2022-01-06 08:54:14 +00:00
return success(c, data.Map{
2021-12-06 12:24:22 +00:00
"items": secrets,
"total": total,
})
}
}
func secretFind(b biz.SecretBiz) 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")
secret, raw, err := b.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{"secret": secret, "raw": raw})
2021-12-06 12:24:22 +00:00
}
}
func secretDelete(b biz.SecretBiz) 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 = b.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 secretSave(b biz.SecretBiz) web.HandlerFunc {
2022-01-06 08:54:14 +00:00
return func(c web.Context) error {
secret := &biz.Secret{}
err := c.Bind(secret, 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()
if secret.ID == "" {
err = b.Create(ctx, secret, c.User())
2021-12-06 12:24:22 +00:00
} else {
2022-01-06 08:54:14 +00:00
err = b.Update(ctx, secret, 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
}
}