mirror of
https://github.com/cuigh/swirl
synced 2025-06-26 18:16:50 +00:00
Refactor codes
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/cuigh/swirl/model"
|
||||
)
|
||||
|
||||
// StackController is a controller of docker stack(compose)
|
||||
type StackController struct {
|
||||
TaskList web.HandlerFunc `path:"/task/" name:"stack.task.list" authorize:"!" desc:"stack task list page"`
|
||||
TaskDelete web.HandlerFunc `path:"/task/delete" method:"post" name:"stack.task.delete" authorize:"!" desc:"delete stack task"`
|
||||
@@ -21,157 +22,167 @@ type StackController struct {
|
||||
ArchiveCreate web.HandlerFunc `path:"/archive/new" method:"post" name:"stack.archive.create" authorize:"!" desc:"create stack.archive"`
|
||||
}
|
||||
|
||||
// Stack creates an instance of StackController
|
||||
func Stack() (c *StackController) {
|
||||
c = &StackController{}
|
||||
return &StackController{
|
||||
TaskList: stackTaskList,
|
||||
TaskDelete: stackTaskDelete,
|
||||
ArchiveList: stackArchiveList,
|
||||
ArchiveDetail: stackArchiveDetail,
|
||||
ArchiveEdit: stackArchiveEdit,
|
||||
ArchiveUpdate: stackArchiveUpdate,
|
||||
ArchiveDelete: stackArchiveDelete,
|
||||
ArchiveDeploy: stackArchiveDeploy,
|
||||
ArchiveNew: stackArchiveNew,
|
||||
ArchiveCreate: stackArchiveCreate,
|
||||
}
|
||||
}
|
||||
|
||||
c.TaskList = func(ctx web.Context) error {
|
||||
stacks, err := docker.StackList()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
m := newModel(ctx).Add("Stacks", stacks)
|
||||
return ctx.Render("stack/task/list", m)
|
||||
func stackTaskList(ctx web.Context) error {
|
||||
stacks, err := docker.StackList()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.TaskDelete = func(ctx web.Context) error {
|
||||
name := ctx.F("name")
|
||||
err := docker.StackRemove(name)
|
||||
if err == nil {
|
||||
biz.Event.CreateStackTask(model.EventActionDelete, name, ctx.User())
|
||||
}
|
||||
return ajaxResult(ctx, err)
|
||||
m := newModel(ctx).Add("Stacks", stacks)
|
||||
return ctx.Render("stack/task/list", m)
|
||||
}
|
||||
|
||||
func stackTaskDelete(ctx web.Context) error {
|
||||
name := ctx.F("name")
|
||||
err := docker.StackRemove(name)
|
||||
if err == nil {
|
||||
biz.Event.CreateStackTask(model.EventActionDelete, name, ctx.User())
|
||||
}
|
||||
return ajaxResult(ctx, err)
|
||||
}
|
||||
|
||||
func stackArchiveList(ctx web.Context) error {
|
||||
args := &model.ArchiveListArgs{}
|
||||
err := ctx.Bind(args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
args.PageSize = model.PageSize
|
||||
if args.PageIndex == 0 {
|
||||
args.PageIndex = 1
|
||||
}
|
||||
|
||||
c.ArchiveList = func(ctx web.Context) error {
|
||||
args := &model.ArchiveListArgs{}
|
||||
err := ctx.Bind(args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
args.PageSize = model.PageSize
|
||||
if args.PageIndex == 0 {
|
||||
args.PageIndex = 1
|
||||
}
|
||||
|
||||
archives, totalCount, err := biz.Archive.List(args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
m := newPagerModel(ctx, totalCount, model.PageSize, args.PageIndex).
|
||||
Add("Name", args.Name).
|
||||
Add("Archives", archives)
|
||||
return ctx.Render("stack/archive/list", m)
|
||||
archives, totalCount, err := biz.Archive.List(args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.ArchiveDetail = func(ctx web.Context) error {
|
||||
id := ctx.P("id")
|
||||
archive, err := biz.Archive.Get(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if archive == nil {
|
||||
return web.ErrNotFound
|
||||
}
|
||||
m := newPagerModel(ctx, totalCount, model.PageSize, args.PageIndex).
|
||||
Add("Name", args.Name).
|
||||
Add("Archives", archives)
|
||||
return ctx.Render("stack/archive/list", m)
|
||||
}
|
||||
|
||||
m := newModel(ctx).Add("Archive", archive)
|
||||
return ctx.Render("stack/archive/detail", m)
|
||||
func stackArchiveDetail(ctx web.Context) error {
|
||||
id := ctx.P("id")
|
||||
archive, err := biz.Archive.Get(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if archive == nil {
|
||||
return web.ErrNotFound
|
||||
}
|
||||
|
||||
c.ArchiveEdit = func(ctx web.Context) error {
|
||||
id := ctx.P("id")
|
||||
archive, err := biz.Archive.Get(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if archive == nil {
|
||||
return web.ErrNotFound
|
||||
}
|
||||
m := newModel(ctx).Add("Archive", archive)
|
||||
return ctx.Render("stack/archive/detail", m)
|
||||
}
|
||||
|
||||
m := newModel(ctx).Add("Archive", archive)
|
||||
return ctx.Render("stack/archive/edit", m)
|
||||
func stackArchiveEdit(ctx web.Context) error {
|
||||
id := ctx.P("id")
|
||||
archive, err := biz.Archive.Get(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if archive == nil {
|
||||
return web.ErrNotFound
|
||||
}
|
||||
|
||||
c.ArchiveUpdate = func(ctx web.Context) error {
|
||||
archive := &model.Archive{}
|
||||
err := ctx.Bind(archive)
|
||||
if err == nil {
|
||||
// Validate format
|
||||
_, err = compose.Parse(archive.Name, archive.Content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
m := newModel(ctx).Add("Archive", archive)
|
||||
return ctx.Render("stack/archive/edit", m)
|
||||
}
|
||||
|
||||
archive.UpdatedBy = ctx.User().ID()
|
||||
err = biz.Archive.Update(archive)
|
||||
func stackArchiveUpdate(ctx web.Context) error {
|
||||
archive := &model.Archive{}
|
||||
err := ctx.Bind(archive)
|
||||
if err == nil {
|
||||
// Validate format
|
||||
_, err = compose.Parse(archive.Name, archive.Content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err == nil {
|
||||
biz.Event.CreateStackArchive(model.EventActionUpdate, archive.ID, archive.Name, ctx.User())
|
||||
}
|
||||
return ajaxResult(ctx, err)
|
||||
|
||||
archive.UpdatedBy = ctx.User().ID()
|
||||
err = biz.Archive.Update(archive)
|
||||
}
|
||||
if err == nil {
|
||||
biz.Event.CreateStackArchive(model.EventActionUpdate, archive.ID, archive.Name, ctx.User())
|
||||
}
|
||||
return ajaxResult(ctx, err)
|
||||
}
|
||||
|
||||
func stackArchiveDelete(ctx web.Context) error {
|
||||
id := ctx.F("id")
|
||||
err := biz.Archive.Delete(id, ctx.User())
|
||||
return ajaxResult(ctx, err)
|
||||
}
|
||||
|
||||
func stackArchiveDeploy(ctx web.Context) error {
|
||||
id := ctx.F("id")
|
||||
archive, err := biz.Archive.Get(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.ArchiveDelete = func(ctx web.Context) error {
|
||||
id := ctx.F("id")
|
||||
err := biz.Archive.Delete(id, ctx.User())
|
||||
return ajaxResult(ctx, err)
|
||||
cfg, err := compose.Parse(archive.Name, archive.Content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.ArchiveDeploy = func(ctx web.Context) error {
|
||||
id := ctx.F("id")
|
||||
archive, err := biz.Archive.Get(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
registries, err := biz.Registry.List()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cfg, err := compose.Parse(archive.Name, archive.Content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
registries, err := biz.Registry.List()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Find auth info from registry
|
||||
authes := map[string]string{}
|
||||
for _, sc := range cfg.Services {
|
||||
if _, ok := authes[sc.Image]; !ok {
|
||||
for _, r := range registries {
|
||||
if r.Match(sc.Image) {
|
||||
authes[sc.Image] = r.GetEncodedAuth()
|
||||
}
|
||||
// Find auth info from registry
|
||||
authes := map[string]string{}
|
||||
for _, sc := range cfg.Services {
|
||||
if _, ok := authes[sc.Image]; !ok {
|
||||
for _, r := range registries {
|
||||
if r.Match(sc.Image) {
|
||||
authes[sc.Image] = r.GetEncodedAuth()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
err = docker.StackDeploy(archive.Name, archive.Content, authes)
|
||||
return ajaxResult(ctx, err)
|
||||
}
|
||||
|
||||
c.ArchiveNew = func(ctx web.Context) error {
|
||||
m := newModel(ctx)
|
||||
return ctx.Render("stack/archive/new", m)
|
||||
}
|
||||
|
||||
c.ArchiveCreate = func(ctx web.Context) error {
|
||||
archive := &model.Archive{}
|
||||
err := ctx.Bind(archive)
|
||||
if err == nil {
|
||||
// Validate format
|
||||
_, err = compose.Parse(archive.Name, archive.Content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
archive.CreatedBy = ctx.User().ID()
|
||||
archive.UpdatedBy = archive.CreatedBy
|
||||
err = biz.Archive.Create(archive)
|
||||
}
|
||||
return ajaxResult(ctx, err)
|
||||
}
|
||||
|
||||
return
|
||||
err = docker.StackDeploy(archive.Name, archive.Content, authes)
|
||||
return ajaxResult(ctx, err)
|
||||
}
|
||||
|
||||
func stackArchiveNew(ctx web.Context) error {
|
||||
m := newModel(ctx)
|
||||
return ctx.Render("stack/archive/new", m)
|
||||
}
|
||||
|
||||
func stackArchiveCreate(ctx web.Context) error {
|
||||
archive := &model.Archive{}
|
||||
err := ctx.Bind(archive)
|
||||
if err == nil {
|
||||
// Validate format
|
||||
_, err = compose.Parse(archive.Name, archive.Content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
archive.CreatedBy = ctx.User().ID()
|
||||
archive.UpdatedBy = archive.CreatedBy
|
||||
err = biz.Archive.Create(archive)
|
||||
}
|
||||
return ajaxResult(ctx, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user