swirl/api/stack.go

168 lines
3.7 KiB
Go
Raw Normal View History

2021-12-06 12:24:22 +00:00
package api
import (
"io"
"github.com/cuigh/auxo/net/web"
"github.com/cuigh/swirl/biz"
2021-12-23 11:28:31 +00:00
"github.com/cuigh/swirl/dao"
2021-12-06 12:24:22 +00:00
"github.com/cuigh/swirl/docker/compose"
2022-01-06 08:54:14 +00:00
"github.com/cuigh/swirl/misc"
2021-12-06 12:24:22 +00:00
)
// StackHandler encapsulates stack related handlers.
type StackHandler struct {
Search web.HandlerFunc `path:"/search" auth:"stack.view" desc:"search stacks"`
Find web.HandlerFunc `path:"/find" auth:"stack.view" desc:"find stack by name"`
Delete web.HandlerFunc `path:"/delete" method:"post" auth:"stack.delete" desc:"delete stack"`
Shutdown web.HandlerFunc `path:"/shutdown" method:"post" auth:"stack.shutdown" desc:"shutdown stack"`
Deploy web.HandlerFunc `path:"/deploy" method:"post" auth:"stack.deploy" desc:"deploy stack"`
Save web.HandlerFunc `path:"/save" method:"post" auth:"stack.edit" desc:"create or update stack"`
Upload web.HandlerFunc `path:"/upload" method:"post" auth:"*" desc:"upload compose file"`
}
// NewStack creates an instance of StackHandler
func NewStack(b biz.StackBiz) *StackHandler {
return &StackHandler{
Search: stackSearch(b),
Find: stackFind(b),
Delete: stackDelete(b),
Shutdown: stackShutdown(b),
Deploy: stackDeploy(b),
Save: stackSave(b),
Upload: stackUpload,
}
}
2022-01-06 08:54:14 +00:00
func stackUpload(c web.Context) (err error) {
file, _, err := c.File("content")
2021-12-06 12:24:22 +00:00
if err != nil {
return err
}
b, err := io.ReadAll(file)
if err != nil {
return err
}
2022-01-06 08:54:14 +00:00
return c.Data(b)
2021-12-06 12:24:22 +00:00
}
func stackSearch(b biz.StackBiz) web.HandlerFunc {
type Args struct {
Name string `json:"name" bind:"name"`
Filter string `json:"filter" bind:"filter"`
}
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{}
2021-12-23 11:28:31 +00:00
stacks []*dao.Stack
2021-12-06 12:24:22 +00:00
)
2022-01-06 08:54:14 +00:00
if err = c.Bind(args); err == nil {
ctx, cancel := misc.Context(defaultTimeout)
defer cancel()
stacks, err = b.Search(ctx, args.Name, args.Filter)
2021-12-06 12:24:22 +00:00
}
if err != nil {
return
}
2022-01-06 08:54:14 +00:00
return success(c, stacks)
2021-12-06 12:24:22 +00:00
}
}
func stackFind(b biz.StackBiz) web.HandlerFunc {
2022-01-06 08:54:14 +00:00
return func(c web.Context) error {
ctx, cancel := misc.Context(defaultTimeout)
defer cancel()
name := c.Query("name")
stack, err := b.Find(ctx, name)
2021-12-06 12:24:22 +00:00
if err != nil {
return err
}
2022-01-06 08:54:14 +00:00
return success(c, stack)
2021-12-06 12:24:22 +00:00
}
}
func stackDelete(b biz.StackBiz) web.HandlerFunc {
type Args struct {
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.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 stackShutdown(b biz.StackBiz) web.HandlerFunc {
type Args struct {
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.Shutdown(ctx, 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 stackDeploy(b biz.StackBiz) web.HandlerFunc {
type Args struct {
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.Deploy(ctx, 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 stackSave(b biz.StackBiz) web.HandlerFunc {
2021-12-16 12:23:08 +00:00
type Args struct {
ID string `json:"id"`
2021-12-23 11:28:31 +00:00
dao.Stack
2021-12-16 12:23:08 +00:00
}
2022-01-06 08:54:14 +00:00
return func(c web.Context) error {
2021-12-16 12:23:08 +00:00
stack := &Args{}
2022-01-06 08:54:14 +00:00
err := c.Bind(stack, true)
2021-12-06 12:24:22 +00:00
if err != nil {
return err
}
// Validate format
_, err = compose.Parse(stack.Name, stack.Content)
if err != nil {
return err
}
2022-01-06 08:54:14 +00:00
ctx, cancel := misc.Context(defaultTimeout)
defer cancel()
2021-12-06 12:24:22 +00:00
if stack.ID == "" {
2022-01-06 08:54:14 +00:00
err = b.Create(ctx, &stack.Stack, c.User())
2021-12-06 12:24:22 +00:00
} else {
2022-01-06 08:54:14 +00:00
err = b.Update(ctx, &stack.Stack, 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
}
}