diff --git a/controller/config.go b/controller/config.go index 26e4bb3..e9c3772 100644 --- a/controller/config.go +++ b/controller/config.go @@ -10,6 +10,7 @@ import ( "github.com/cuigh/swirl/model" ) +// ConfigController is a controller of docker config type ConfigController struct { List web.HandlerFunc `path:"/" name:"config.list" authorize:"!" desc:"config list page"` Delete web.HandlerFunc `path:"/delete" method:"post" name:"config.delete" authorize:"!" desc:"delete config"` @@ -19,67 +20,73 @@ type ConfigController struct { Update web.HandlerFunc `path:"/:id/update" method:"post" name:"config.update" authorize:"!" desc:"update config"` } +// Config creates an instance of ConfigController func Config() (c *ConfigController) { - c = &ConfigController{} - - c.List = func(ctx web.Context) error { - name := ctx.Q("name") - page := cast.ToIntD(ctx.Q("page"), 1) - configs, totalCount, err := docker.ConfigList(name, page, model.PageSize) - if err != nil { - return err - } - - m := newPagerModel(ctx, totalCount, model.PageSize, page). - Add("Name", name). - Add("Configs", configs) - return ctx.Render("config/list", m) + return &ConfigController{ + List: configList, + Delete: configDelete, + New: configNew, + Create: configCreate, + Edit: configEdit, + Update: configUpdate, } - - c.Delete = func(ctx web.Context) error { - ids := strings.Split(ctx.F("ids"), ",") - err := docker.ConfigRemove(ids) - return ajaxResult(ctx, err) - } - - c.New = func(ctx web.Context) error { - m := newModel(ctx) - return ctx.Render("config/new", m) - } - - c.Create = func(ctx web.Context) error { - v := &model.ConfigCreateInfo{} - err := ctx.Bind(v) - if err == nil { - err = docker.ConfigCreate(v) - if err == nil { - biz.Event.CreateConfig(model.EventActionCreate, v.Name, ctx.User()) - } - } - return ajaxResult(ctx, err) - } - - c.Edit = func(ctx web.Context) error { - id := ctx.P("id") - cfg, _, err := docker.ConfigInspect(id) - if err != nil { - return err - } - m := newModel(ctx).Add("Config", cfg) - return ctx.Render("config/edit", m) - } - - c.Update = func(ctx web.Context) error { - v := &model.ConfigUpdateInfo{} - err := ctx.Bind(v) - if err == nil { - err = docker.ConfigUpdate(v) - if err == nil { - biz.Event.CreateConfig(model.EventActionUpdate, v.Name, ctx.User()) - } - } - return ajaxResult(ctx, err) - } - - return +} + +func configList(ctx web.Context) error { + name := ctx.Q("name") + page := cast.ToIntD(ctx.Q("page"), 1) + configs, totalCount, err := docker.ConfigList(name, page, model.PageSize) + if err != nil { + return err + } + + m := newPagerModel(ctx, totalCount, model.PageSize, page). + Add("Name", name). + Add("Configs", configs) + return ctx.Render("config/list", m) +} + +func configDelete(ctx web.Context) error { + ids := strings.Split(ctx.F("ids"), ",") + err := docker.ConfigRemove(ids) + return ajaxResult(ctx, err) +} + +func configNew(ctx web.Context) error { + m := newModel(ctx) + return ctx.Render("config/new", m) +} + +func configCreate(ctx web.Context) error { + v := &model.ConfigCreateInfo{} + err := ctx.Bind(v) + if err == nil { + err = docker.ConfigCreate(v) + if err == nil { + biz.Event.CreateConfig(model.EventActionCreate, v.Name, ctx.User()) + } + } + return ajaxResult(ctx, err) +} + +func configEdit(ctx web.Context) error { + id := ctx.P("id") + cfg, _, err := docker.ConfigInspect(id) + if err != nil { + return err + } + m := newModel(ctx).Add("Config", cfg) + return ctx.Render("config/edit", m) +} + +func configUpdate(ctx web.Context) error { + v := &model.ConfigUpdateInfo{} + err := ctx.Bind(v) + if err == nil { + err = docker.ConfigUpdate(v) + if err == nil { + biz.Event.CreateConfig(model.EventActionUpdate, v.Name, ctx.User()) + } + } + return ajaxResult(ctx, err) } diff --git a/controller/container.go b/controller/container.go index bacc5c7..3bf9e52 100644 --- a/controller/container.go +++ b/controller/container.go @@ -10,6 +10,7 @@ import ( "github.com/cuigh/swirl/model" ) +// ContainerController is a controller of docker container type ContainerController struct { List web.HandlerFunc `path:"/" name:"container.list" authorize:"!" desc:"container list page"` Detail web.HandlerFunc `path:"/:id/detail" name:"container.detail" authorize:"!" desc:"container detail page"` @@ -18,6 +19,7 @@ type ContainerController struct { Delete web.HandlerFunc `path:"/delete" method:"post" name:"container.delete" authorize:"!" desc:"delete container"` } +// Container creates an instance of ContainerController func Container() (c *ContainerController) { return &ContainerController{ List: containerList, diff --git a/controller/event.go b/controller/event.go index 9e281b5..fd0003c 100644 --- a/controller/event.go +++ b/controller/event.go @@ -6,33 +6,35 @@ import ( "github.com/cuigh/swirl/model" ) +// EventController is a controller of user events type EventController struct { List web.HandlerFunc `path:"/" name:"event.list" authorize:"!" desc:"event list page"` } +// Event creates an instance of EventController func Event() (c *EventController) { - c = &EventController{} + return &EventController{ + List: eventList, + } +} - c.List = func(ctx web.Context) error { - args := &model.EventListArgs{} - err := ctx.Bind(args) - if err != nil { - return err - } - args.PageSize = model.PageSize - if args.PageIndex == 0 { - args.PageIndex = 1 - } - - events, totalCount, err := biz.Event.List(args) - if err != nil { - return err - } - - m := newPagerModel(ctx, totalCount, model.PageSize, args.PageIndex). - Add("Events", events).Add("Args", args) - return ctx.Render("system/event/list", m) +func eventList(ctx web.Context) error { + args := &model.EventListArgs{} + err := ctx.Bind(args) + if err != nil { + return err + } + args.PageSize = model.PageSize + if args.PageIndex == 0 { + args.PageIndex = 1 } - return + events, totalCount, err := biz.Event.List(args) + if err != nil { + return err + } + + m := newPagerModel(ctx, totalCount, model.PageSize, args.PageIndex). + Add("Events", events).Add("Args", args) + return ctx.Render("system/event/list", m) } diff --git a/controller/home.go b/controller/home.go index d0c6380..543192f 100644 --- a/controller/home.go +++ b/controller/home.go @@ -8,101 +8,108 @@ import ( "github.com/cuigh/swirl/model" ) +// HomeController is a basic controller of site type HomeController struct { Index web.HandlerFunc `path:"/" name:"index" authorize:"?" desc:"index page"` - Error403 web.HandlerFunc `path:"/403" name:"403" authorize:"?" desc:"403 page"` - Error404 web.HandlerFunc `path:"/404" name:"404" authorize:"*" desc:"404 page"` Login web.HandlerFunc `path:"/login" name:"login" authorize:"*" desc:"sign in page"` InitGet web.HandlerFunc `path:"/init" name:"init" authorize:"*" desc:"initialize page"` InitPost web.HandlerFunc `path:"/init" method:"post" name:"init" authorize:"*" desc:"initialize system"` + Error403 web.HandlerFunc `path:"/403" name:"403" authorize:"?" desc:"403 page"` + Error404 web.HandlerFunc `path:"/404" name:"404" authorize:"*" desc:"404 page"` } +// Home creates an instance of HomeController func Home() (c *HomeController) { - c = &HomeController{} - - c.Index = func(ctx web.Context) (err error) { - var ( - count int - m = newModel(ctx) - ) - - if count, err = docker.NodeCount(); err != nil { - return - } - m.Add("NodeCount", count) - - if count, err = docker.NetworkCount(); err != nil { - return - } - m.Add("NetworkCount", count) - - if count, err = docker.ServiceCount(); err != nil { - return - } - m.Add("ServiceCount", count) - - if count, err = docker.StackCount(); err != nil { - return - } - m.Add("StackCount", count) - - return ctx.Render("index", m) + return &HomeController{ + Index: homeIndex, + Login: homeLogin, + InitGet: homeInitGet, + InitPost: homeInitPost, + Error403: homeError403, + Error404: homeError404, } - - c.Login = func(ctx web.Context) error { - count, err := biz.User.Count() - if err != nil { - return err - } else if count == 0 { - return ctx.Redirect("init") - } - if ctx.User() != nil { - u := ctx.Q("from") - if u == "" { - u = "/" - } - return ctx.Redirect(u) - } - return ctx.Render("login", nil) - } - - c.InitGet = func(ctx web.Context) error { - count, err := biz.User.Count() - if err != nil { - return err - } else if count > 0 { - return ctx.Redirect("login") - } - return ctx.Render("init", nil) - } - - c.InitPost = func(ctx web.Context) error { - count, err := biz.User.Count() - if err != nil { - return err - } else if count > 0 { - return errors.New("Swirl was already initialized") - } - - user := &model.User{} - err = ctx.Bind(user) - if err != nil { - return err - } - - user.Admin = true - user.Type = model.UserTypeInternal - err = biz.User.Create(user, nil) - return ajaxResult(ctx, err) - } - - c.Error403 = func(ctx web.Context) error { - return ctx.Render("403", nil) - } - - c.Error404 = func(ctx web.Context) error { - return ctx.Render("404", nil) - } - - return +} + +func homeIndex(ctx web.Context) (err error) { + var ( + count int + m = newModel(ctx) + ) + + if count, err = docker.NodeCount(); err != nil { + return + } + m.Add("NodeCount", count) + + if count, err = docker.NetworkCount(); err != nil { + return + } + m.Add("NetworkCount", count) + + if count, err = docker.ServiceCount(); err != nil { + return + } + m.Add("ServiceCount", count) + + if count, err = docker.StackCount(); err != nil { + return + } + m.Add("StackCount", count) + + return ctx.Render("index", m) +} + +func homeLogin(ctx web.Context) error { + count, err := biz.User.Count() + if err != nil { + return err + } else if count == 0 { + return ctx.Redirect("init") + } + if ctx.User() != nil { + u := ctx.Q("from") + if u == "" { + u = "/" + } + return ctx.Redirect(u) + } + return ctx.Render("login", nil) +} + +func homeInitGet(ctx web.Context) error { + count, err := biz.User.Count() + if err != nil { + return err + } else if count > 0 { + return ctx.Redirect("login") + } + return ctx.Render("init", nil) +} + +func homeInitPost(ctx web.Context) error { + count, err := biz.User.Count() + if err != nil { + return err + } else if count > 0 { + return errors.New("Swirl was already initialized") + } + + user := &model.User{} + err = ctx.Bind(user) + if err != nil { + return err + } + + user.Admin = true + user.Type = model.UserTypeInternal + err = biz.User.Create(user, nil) + return ajaxResult(ctx, err) +} + +func homeError403(ctx web.Context) error { + return ctx.Render("403", nil) +} + +func homeError404(ctx web.Context) error { + return ctx.Render("404", nil) } diff --git a/controller/image.go b/controller/image.go index fefde88..f808d2a 100644 --- a/controller/image.go +++ b/controller/image.go @@ -10,6 +10,7 @@ import ( "github.com/cuigh/swirl/model" ) +// ImageController is a controller of docker image type ImageController struct { List web.HandlerFunc `path:"/" name:"image.list" authorize:"!" desc:"image list page"` Detail web.HandlerFunc `path:"/:id/detail" name:"image.detail" authorize:"!" desc:"image detail page"` @@ -17,6 +18,7 @@ type ImageController struct { Delete web.HandlerFunc `path:"/delete" method:"post" name:"image.delete" authorize:"!" desc:"delete image"` } +// Image creates an instance of ImageController func Image() (c *ImageController) { return &ImageController{ List: imageList, diff --git a/controller/network.go b/controller/network.go index 30045fb..e24c062 100644 --- a/controller/network.go +++ b/controller/network.go @@ -8,6 +8,7 @@ import ( "github.com/cuigh/swirl/model" ) +// NetworkController is a controller of docker network type NetworkController struct { List web.HandlerFunc `path:"/" name:"network.list" authorize:"!" desc:"network list page"` New web.HandlerFunc `path:"/new" name:"network.new" authorize:"!" desc:"new network page"` @@ -18,82 +19,88 @@ type NetworkController struct { Raw web.HandlerFunc `path:"/:name/raw" name:"network.raw" authorize:"!" desc:"network raw page"` } -// Network create a NetworkController instance. +// Network creates a NetworkController instance. func Network() (c *NetworkController) { - c = &NetworkController{} - - c.List = func(ctx web.Context) error { - networks, err := docker.NetworkList() - if err != nil { - return err - } - - m := newModel(ctx).Add("Networks", networks) - return ctx.Render("network/list", m) + return &NetworkController{ + List: networkList, + New: networkNew, + Create: networkCreate, + Delete: networkDelete, + Disconnect: networkDisconnect, + Detail: networkDetail, + Raw: networkRaw, } - - c.New = func(ctx web.Context) error { - m := newModel(ctx) - return ctx.Render("/network/new", m) - } - - c.Create = func(ctx web.Context) error { - info := &model.NetworkCreateInfo{} - err := ctx.Bind(info) - if err != nil { - return err - } - err = docker.NetworkCreate(info) - if err == nil { - biz.Event.CreateNetwork(model.EventActionCreate, info.Name, info.Name, ctx.User()) - } - return ajaxResult(ctx, err) - } - - c.Delete = func(ctx web.Context) error { - name := ctx.F("name") - err := docker.NetworkRemove(name) - if err == nil { - biz.Event.CreateNetwork(model.EventActionDelete, name, name, ctx.User()) - } - return ajaxResult(ctx, err) - } - - c.Disconnect = func(ctx web.Context) error { - name := ctx.P("name") - container := ctx.F("container") - err := docker.NetworkDisconnect(name, container) - if err == nil { - biz.Event.CreateNetwork(model.EventActionDisconnect, name, name+" <-> "+container, ctx.User()) - } - return ajaxResult(ctx, err) - } - - c.Detail = func(ctx web.Context) error { - name := ctx.P("name") - network, err := docker.NetworkInspect(name) - if err != nil { - return err - } - m := newModel(ctx).Add("Network", network) - return ctx.Render("network/detail", m) - } - - c.Raw = func(ctx web.Context) error { - name := ctx.P("name") - raw, err := docker.NetworkInspectRaw(name) - if err != nil { - return err - } - - j, err := misc.JSONIndent(raw) - if err != nil { - return err - } - - m := newModel(ctx).Add("Network", name).Add("Raw", j) - return ctx.Render("network/raw", m) - } - - return +} + +func networkList(ctx web.Context) error { + networks, err := docker.NetworkList() + if err != nil { + return err + } + + m := newModel(ctx).Add("Networks", networks) + return ctx.Render("network/list", m) +} + +func networkNew(ctx web.Context) error { + m := newModel(ctx) + return ctx.Render("/network/new", m) +} + +func networkCreate(ctx web.Context) error { + info := &model.NetworkCreateInfo{} + err := ctx.Bind(info) + if err != nil { + return err + } + err = docker.NetworkCreate(info) + if err == nil { + biz.Event.CreateNetwork(model.EventActionCreate, info.Name, info.Name, ctx.User()) + } + return ajaxResult(ctx, err) +} + +func networkDelete(ctx web.Context) error { + name := ctx.F("name") + err := docker.NetworkRemove(name) + if err == nil { + biz.Event.CreateNetwork(model.EventActionDelete, name, name, ctx.User()) + } + return ajaxResult(ctx, err) +} + +func networkDisconnect(ctx web.Context) error { + name := ctx.P("name") + container := ctx.F("container") + err := docker.NetworkDisconnect(name, container) + if err == nil { + biz.Event.CreateNetwork(model.EventActionDisconnect, name, name+" <-> "+container, ctx.User()) + } + return ajaxResult(ctx, err) +} + +func networkDetail(ctx web.Context) error { + name := ctx.P("name") + network, err := docker.NetworkInspect(name) + if err != nil { + return err + } + m := newModel(ctx).Add("Network", network) + return ctx.Render("network/detail", m) +} + +func networkRaw(ctx web.Context) error { + name := ctx.P("name") + raw, err := docker.NetworkInspectRaw(name) + if err != nil { + return err + } + + j, err := misc.JSONIndent(raw) + if err != nil { + return err + } + + m := newModel(ctx).Add("Network", name).Add("Raw", j) + return ctx.Render("network/raw", m) } diff --git a/controller/node.go b/controller/node.go index 30b7ae1..2784a2a 100644 --- a/controller/node.go +++ b/controller/node.go @@ -7,6 +7,7 @@ import ( "github.com/cuigh/swirl/model" ) +// NodeController is a controller of swarm node type NodeController struct { List web.HandlerFunc `path:"/" name:"node.list" authorize:"!" desc:"node list page"` Detail web.HandlerFunc `path:"/:id/detail" name:"node.detail" authorize:"!" desc:"node detail page"` @@ -16,77 +17,83 @@ type NodeController struct { Update web.HandlerFunc `path:"/:id/update" method:"post" name:"node.update" authorize:"!" desc:"update node"` } +// Node creates an instance of NodeController func Node() (c *NodeController) { - c = &NodeController{} - - c.List = func(ctx web.Context) error { - nodes, err := docker.NodeList() - if err != nil { - return err - } - - m := newModel(ctx).Add("Nodes", nodes) - return ctx.Render("node/list", m) + return &NodeController{ + List: nodeList, + Detail: nodeDetail, + Raw: nodeRaw, + Delete: nodeDelete, + Edit: nodeEdit, + Update: nodeUpdate, } - - c.Delete = func(ctx web.Context) error { - id := ctx.F("id") - err := docker.NodeRemove(id) - return ajaxResult(ctx, err) - } - - c.Detail = func(ctx web.Context) error { - id := ctx.P("id") - node, _, err := docker.NodeInspect(id) - if err != nil { - return err - } - - tasks, err := docker.TaskList("", id) - if err != nil { - return err - } - - m := newModel(ctx).Add("Node", node).Add("Tasks", tasks) - return ctx.Render("node/detail", m) - } - - c.Raw = func(ctx web.Context) error { - id := ctx.P("id") - node, raw, err := docker.NodeInspect(id) - if err != nil { - return err - } - - j, err := misc.JSONIndent(raw) - if err != nil { - return err - } - - m := newModel(ctx).Add("ID", id).Add("Node", node).Add("Raw", j) - return ctx.Render("node/raw", m) - } - - c.Edit = func(ctx web.Context) error { - id := ctx.P("id") - node, _, err := docker.NodeInspect(id) - if err != nil { - return err - } - - m := newModel(ctx).Add("Node", node) - return ctx.Render("node/edit", m) - } - - c.Update = func(ctx web.Context) error { - id := ctx.P("id") - info := &model.NodeUpdateInfo{} - err := ctx.Bind(info) - if err == nil { - err = docker.NodeUpdate(id, info) - } - return ajaxResult(ctx, err) - } - - return +} + +func nodeList(ctx web.Context) error { + nodes, err := docker.NodeList() + if err != nil { + return err + } + + m := newModel(ctx).Add("Nodes", nodes) + return ctx.Render("node/list", m) +} + +func nodeDelete(ctx web.Context) error { + id := ctx.F("id") + err := docker.NodeRemove(id) + return ajaxResult(ctx, err) +} + +func nodeDetail(ctx web.Context) error { + id := ctx.P("id") + node, _, err := docker.NodeInspect(id) + if err != nil { + return err + } + + tasks, err := docker.TaskList("", id) + if err != nil { + return err + } + + m := newModel(ctx).Add("Node", node).Add("Tasks", tasks) + return ctx.Render("node/detail", m) +} + +func nodeRaw(ctx web.Context) error { + id := ctx.P("id") + node, raw, err := docker.NodeInspect(id) + if err != nil { + return err + } + + j, err := misc.JSONIndent(raw) + if err != nil { + return err + } + + m := newModel(ctx).Add("ID", id).Add("Node", node).Add("Raw", j) + return ctx.Render("node/raw", m) +} + +func nodeEdit(ctx web.Context) error { + id := ctx.P("id") + node, _, err := docker.NodeInspect(id) + if err != nil { + return err + } + + m := newModel(ctx).Add("Node", node) + return ctx.Render("node/edit", m) +} + +func nodeUpdate(ctx web.Context) error { + id := ctx.P("id") + info := &model.NodeUpdateInfo{} + err := ctx.Bind(info) + if err == nil { + err = docker.NodeUpdate(id, info) + } + return ajaxResult(ctx, err) } diff --git a/controller/profile.go b/controller/profile.go index eac6d95..f9558fc 100644 --- a/controller/profile.go +++ b/controller/profile.go @@ -6,6 +6,7 @@ import ( "github.com/cuigh/swirl/model" ) +// ProfileController is a controller of user profile type ProfileController struct { Index web.HandlerFunc `path:"/" name:"profile.info" authorize:"?" desc:"profile info page"` ModifyInfo web.HandlerFunc `path:"/" method:"post" name:"profile.info.modify" authorize:"?" desc:"modify info"` @@ -13,40 +14,44 @@ type ProfileController struct { ModifyPassword web.HandlerFunc `path:"/password" method:"post" name:"profile.password.modify" authorize:"?" desc:"modify password"` } +// Profile creates an instance of ProfileController func Profile() (c *ProfileController) { - c = &ProfileController{} - - c.Index = func(ctx web.Context) error { - user, err := biz.User.GetByID(ctx.User().ID()) - if err != nil { - return err - } - - m := newModel(ctx).Add("User", user) - return ctx.Render("profile/index", m) + return &ProfileController{ + Index: profileIndex, + ModifyInfo: profileModifyInfo, + Password: profilePassword, + ModifyPassword: profileModifyPassword, } - - c.ModifyInfo = func(ctx web.Context) error { - user := &model.User{} - err := ctx.Bind(user) - if err == nil { - user.ID = ctx.User().ID() - err = biz.User.UpdateInfo(user) - } - return ajaxResult(ctx, err) - } - - c.Password = func(ctx web.Context) error { - m := newModel(ctx) - return ctx.Render("profile/password", m) - } - - c.ModifyPassword = func(ctx web.Context) error { - old_pwd := ctx.F("password_old") - new_pwd := ctx.F("password") - err := biz.User.UpdatePassword(ctx.User().ID(), old_pwd, new_pwd) - return ajaxResult(ctx, err) - } - - return +} + +func profileIndex(ctx web.Context) error { + user, err := biz.User.GetByID(ctx.User().ID()) + if err != nil { + return err + } + + m := newModel(ctx).Add("User", user) + return ctx.Render("profile/index", m) +} + +func profileModifyInfo(ctx web.Context) error { + user := &model.User{} + err := ctx.Bind(user) + if err == nil { + user.ID = ctx.User().ID() + err = biz.User.UpdateInfo(user) + } + return ajaxResult(ctx, err) +} + +func profilePassword(ctx web.Context) error { + m := newModel(ctx) + return ctx.Render("profile/password", m) +} + +func profileModifyPassword(ctx web.Context) error { + oldPwd := ctx.F("password_old") + newPwd := ctx.F("password") + err := biz.User.UpdatePassword(ctx.User().ID(), oldPwd, newPwd) + return ajaxResult(ctx, err) } diff --git a/controller/registry.go b/controller/registry.go index aabe283..3cccc99 100644 --- a/controller/registry.go +++ b/controller/registry.go @@ -6,6 +6,7 @@ import ( "github.com/cuigh/swirl/model" ) +// RegistryController is a controller of docker registry type RegistryController struct { List web.HandlerFunc `path:"/" name:"registry.list" authorize:"!" desc:"registry list page"` Create web.HandlerFunc `path:"/create" method:"post" name:"registry.create" authorize:"!" desc:"create registry"` @@ -13,44 +14,48 @@ type RegistryController struct { Update web.HandlerFunc `path:"/update" method:"post" name:"registry.update" authorize:"!" desc:"update registry"` } +// Registry creates an instance of RegistryController func Registry() (c *RegistryController) { - c = &RegistryController{} - - c.List = func(ctx web.Context) error { - registries, err := biz.Registry.List() - if err != nil { - return err - } - - m := newModel(ctx).Add("Registries", registries) - return ctx.Render("registry/list", m) + return &RegistryController{ + List: registryList, + Create: registryCreate, + Delete: registryDelete, + Update: registryUpdate, } - - c.Create = func(ctx web.Context) error { - registry := &model.Registry{} - err := ctx.Bind(registry) - if err != nil { - return err - } - err = biz.Registry.Create(registry, ctx.User()) - return ajaxResult(ctx, err) - } - - c.Delete = func(ctx web.Context) error { - id := ctx.F("id") - err := biz.Registry.Delete(id, ctx.User()) - return ajaxResult(ctx, err) - } - - c.Update = func(ctx web.Context) error { - registry := &model.Registry{} - err := ctx.Bind(registry) - if err != nil { - return err - } - err = biz.Registry.Update(registry, ctx.User()) - return ajaxResult(ctx, err) - } - - return +} + +func registryList(ctx web.Context) error { + registries, err := biz.Registry.List() + if err != nil { + return err + } + + m := newModel(ctx).Add("Registries", registries) + return ctx.Render("registry/list", m) +} + +func registryCreate(ctx web.Context) error { + registry := &model.Registry{} + err := ctx.Bind(registry) + if err != nil { + return err + } + err = biz.Registry.Create(registry, ctx.User()) + return ajaxResult(ctx, err) +} + +func registryDelete(ctx web.Context) error { + id := ctx.F("id") + err := biz.Registry.Delete(id, ctx.User()) + return ajaxResult(ctx, err) +} + +func registryUpdate(ctx web.Context) error { + registry := &model.Registry{} + err := ctx.Bind(registry) + if err != nil { + return err + } + err = biz.Registry.Update(registry, ctx.User()) + return ajaxResult(ctx, err) } diff --git a/controller/role.go b/controller/role.go index 6bcfc0a..174ab0c 100644 --- a/controller/role.go +++ b/controller/role.go @@ -7,6 +7,7 @@ import ( "github.com/cuigh/swirl/model" ) +// RoleController is a controller of user role type RoleController struct { Index web.HandlerFunc `path:"/" name:"role.list" authorize:"!" desc:"role list page"` New web.HandlerFunc `path:"/new" name:"role.new" authorize:"!" desc:"new role page"` @@ -17,84 +18,91 @@ type RoleController struct { Update web.HandlerFunc `path:"/:id/update" method:"post" name:"role.update" authorize:"!" desc:"update role"` } +// Role creates an instance of RoleController func Role() (c *RoleController) { - c = &RoleController{} - - c.Index = func(ctx web.Context) error { - roles, err := biz.Role.List() - if err != nil { - return err - } - - m := newModel(ctx).Add("Roles", roles) - return ctx.Render("system/role/list", m) + return &RoleController{ + Index: roleIndex, + New: roleNew, + Create: roleCreate, + Delete: roleDelete, + Detail: roleDetail, + Edit: roleEdit, + Update: roleUpdate, } - - c.New = func(ctx web.Context) error { - m := newModel(ctx).Add("Perms", misc.Perms) - return ctx.Render("system/role/new", m) - } - - c.Create = func(ctx web.Context) error { - role := &model.Role{} - err := ctx.Bind(role) - if err == nil { - err = biz.Role.Create(role, ctx.User()) - } - return ajaxResult(ctx, err) - } - - c.Delete = func(ctx web.Context) error { - id := ctx.F("id") - err := biz.Role.Delete(id, ctx.User()) - return ajaxResult(ctx, err) - } - - c.Detail = func(ctx web.Context) error { - id := ctx.P("id") - role, err := biz.Role.Get(id) - if err != nil { - return err - } - if role == nil { - return web.ErrNotFound - } - - perms := make(map[string]struct{}) - for _, p := range role.Perms { - perms[p] = model.Placeholder - } - m := newModel(ctx).Add("Role", role).Add("Perms", misc.Perms).Add("CheckedPerms", perms) - return ctx.Render("system/role/detail", m) - } - - c.Edit = func(ctx web.Context) error { - id := ctx.P("id") - role, err := biz.Role.Get(id) - if err != nil { - return err - } - if role == nil { - return web.ErrNotFound - } - - perms := make(map[string]struct{}) - for _, p := range role.Perms { - perms[p] = model.Placeholder - } - m := newModel(ctx).Add("Role", role).Add("Perms", misc.Perms).Add("CheckedPerms", perms) - return ctx.Render("system/role/edit", m) - } - - c.Update = func(ctx web.Context) error { - role := &model.Role{} - err := ctx.Bind(role) - if err == nil { - role.ID = ctx.P("id") - err = biz.Role.Update(role, ctx.User()) - } - return ajaxResult(ctx, err) - } - - return +} + +func roleIndex(ctx web.Context) error { + roles, err := biz.Role.List() + if err != nil { + return err + } + + m := newModel(ctx).Add("Roles", roles) + return ctx.Render("system/role/list", m) +} + +func roleNew(ctx web.Context) error { + m := newModel(ctx).Add("Perms", misc.Perms) + return ctx.Render("system/role/new", m) +} + +func roleCreate(ctx web.Context) error { + role := &model.Role{} + err := ctx.Bind(role) + if err == nil { + err = biz.Role.Create(role, ctx.User()) + } + return ajaxResult(ctx, err) +} + +func roleDelete(ctx web.Context) error { + id := ctx.F("id") + err := biz.Role.Delete(id, ctx.User()) + return ajaxResult(ctx, err) +} + +func roleDetail(ctx web.Context) error { + id := ctx.P("id") + role, err := biz.Role.Get(id) + if err != nil { + return err + } + if role == nil { + return web.ErrNotFound + } + + perms := make(map[string]struct{}) + for _, p := range role.Perms { + perms[p] = model.Placeholder + } + m := newModel(ctx).Add("Role", role).Add("Perms", misc.Perms).Add("CheckedPerms", perms) + return ctx.Render("system/role/detail", m) +} + +func roleEdit(ctx web.Context) error { + id := ctx.P("id") + role, err := biz.Role.Get(id) + if err != nil { + return err + } + if role == nil { + return web.ErrNotFound + } + + perms := make(map[string]struct{}) + for _, p := range role.Perms { + perms[p] = model.Placeholder + } + m := newModel(ctx).Add("Role", role).Add("Perms", misc.Perms).Add("CheckedPerms", perms) + return ctx.Render("system/role/edit", m) +} + +func roleUpdate(ctx web.Context) error { + role := &model.Role{} + err := ctx.Bind(role) + if err == nil { + role.ID = ctx.P("id") + err = biz.Role.Update(role, ctx.User()) + } + return ajaxResult(ctx, err) } diff --git a/controller/secret.go b/controller/secret.go index 1d5f225..19c7d19 100644 --- a/controller/secret.go +++ b/controller/secret.go @@ -10,6 +10,7 @@ import ( "github.com/cuigh/swirl/model" ) +// SecretController is a controller of docker secret type SecretController struct { List web.HandlerFunc `path:"/" name:"secret.list" authorize:"!" desc:"secret list page"` Delete web.HandlerFunc `path:"/delete" method:"post" name:"secret.delete" authorize:"!" desc:"delete secret"` @@ -19,75 +20,80 @@ type SecretController struct { Update web.HandlerFunc `path:"/:id/update" method:"post" name:"secret.update" authorize:"!" desc:"update secret"` } +// Secret creates an instance of SecretController func Secret() (c *SecretController) { - c = &SecretController{} - - c.List = func(ctx web.Context) error { - name := ctx.Q("name") - page := cast.ToIntD(ctx.Q("page"), 1) - secrets, totalCount, err := docker.SecretList(name, page, model.PageSize) - if err != nil { - return err - } - - m := newPagerModel(ctx, totalCount, model.PageSize, page). - Add("Name", name). - Add("Secrets", secrets) - return ctx.Render("secret/list", m) + return &SecretController{ + List: secretList, + Delete: secretDelete, + New: secretNew, + Create: secretCreate, + Edit: secretEdit, + Update: secretUpdate, } - - c.Delete = func(ctx web.Context) error { - ids := strings.Split(ctx.F("ids"), ",") - for _, id := range ids { - err := docker.SecretRemove(id) - if err != nil { - return ajaxResult(ctx, err) - } else { - // todo: - biz.Event.CreateSecret(model.EventActionDelete, id, ctx.User()) - } - } - return ajaxSuccess(ctx, nil) - } - - c.New = func(ctx web.Context) error { - m := newModel(ctx) - return ctx.Render("secret/new", m) - } - - c.Create = func(ctx web.Context) error { - v := &model.ConfigCreateInfo{} - err := ctx.Bind(v) - if err == nil { - err = docker.SecretCreate(v) - if err == nil { - biz.Event.CreateSecret(model.EventActionCreate, v.Name, ctx.User()) - } - } - return ajaxResult(ctx, err) - } - - c.Edit = func(ctx web.Context) error { - id := ctx.P("id") - secret, _, err := docker.SecretInspect(id) - if err != nil { - return err - } - m := newModel(ctx).Add("Secret", secret) - return ctx.Render("secret/edit", m) - } - - c.Update = func(ctx web.Context) error { - v := &model.ConfigUpdateInfo{} - err := ctx.Bind(v) - if err == nil { - err = docker.SecretUpdate(v) - if err == nil { - biz.Event.CreateSecret(model.EventActionUpdate, v.Name, ctx.User()) - } - } - return ajaxResult(ctx, err) - } - - return +} +func secretList(ctx web.Context) error { + name := ctx.Q("name") + page := cast.ToIntD(ctx.Q("page"), 1) + secrets, totalCount, err := docker.SecretList(name, page, model.PageSize) + if err != nil { + return err + } + + m := newPagerModel(ctx, totalCount, model.PageSize, page). + Add("Name", name). + Add("Secrets", secrets) + return ctx.Render("secret/list", m) +} + +func secretDelete(ctx web.Context) error { + ids := strings.Split(ctx.F("ids"), ",") + for _, id := range ids { + err := docker.SecretRemove(id) + if err != nil { + return ajaxResult(ctx, err) + } else { + // todo: + biz.Event.CreateSecret(model.EventActionDelete, id, ctx.User()) + } + } + return ajaxSuccess(ctx, nil) +} + +func secretNew(ctx web.Context) error { + m := newModel(ctx) + return ctx.Render("secret/new", m) +} + +func secretCreate(ctx web.Context) error { + v := &model.ConfigCreateInfo{} + err := ctx.Bind(v) + if err == nil { + err = docker.SecretCreate(v) + if err == nil { + biz.Event.CreateSecret(model.EventActionCreate, v.Name, ctx.User()) + } + } + return ajaxResult(ctx, err) +} + +func secretEdit(ctx web.Context) error { + id := ctx.P("id") + secret, _, err := docker.SecretInspect(id) + if err != nil { + return err + } + m := newModel(ctx).Add("Secret", secret) + return ctx.Render("secret/edit", m) +} + +func secretUpdate(ctx web.Context) error { + v := &model.ConfigUpdateInfo{} + err := ctx.Bind(v) + if err == nil { + err = docker.SecretUpdate(v) + if err == nil { + biz.Event.CreateSecret(model.EventActionUpdate, v.Name, ctx.User()) + } + } + return ajaxResult(ctx, err) } diff --git a/controller/service.go b/controller/service.go index 3c75762..6fcb0df 100644 --- a/controller/service.go +++ b/controller/service.go @@ -15,6 +15,7 @@ import ( "github.com/cuigh/swirl/model" ) +// ServiceController is a controller of docker service type ServiceController struct { List web.HandlerFunc `path:"/" name:"service.list" authorize:"!" desc:"service list page"` Detail web.HandlerFunc `path:"/:name/detail" name:"service.detail" authorize:"!" desc:"service detail page"` @@ -28,218 +29,231 @@ type ServiceController struct { Update web.HandlerFunc `path:"/:name/edit" method:"post" name:"service.update" authorize:"!" desc:"update service"` } +// Service creates an instance of ServiceController func Service() (c *ServiceController) { - c = &ServiceController{} + return &ServiceController{ + List: serviceList, + Detail: serviceDetail, + Raw: serviceRaw, + Logs: serviceLogs, + Delete: serviceDelete, + New: serviceNew, + Create: serviceCreate, + Edit: serviceEdit, + Update: serviceUpdate, + Scale: serviceScale, + } +} - c.List = func(ctx web.Context) error { - name := ctx.Q("name") - page := cast.ToIntD(ctx.Q("page"), 1) - services, totalCount, err := docker.ServiceList(name, page, model.PageSize) - if err != nil { - return err - } - - m := newPagerModel(ctx, totalCount, model.PageSize, page). - Add("Name", name). - Add("Services", services) - return ctx.Render("service/list", m) +func serviceList(ctx web.Context) error { + name := ctx.Q("name") + page := cast.ToIntD(ctx.Q("page"), 1) + services, totalCount, err := docker.ServiceList(name, page, model.PageSize) + if err != nil { + return err } - c.Detail = func(ctx web.Context) error { - name := ctx.P("name") - service, _, err := docker.ServiceInspect(name) - if err != nil { - return err - } + m := newPagerModel(ctx, totalCount, model.PageSize, page). + Add("Name", name). + Add("Services", services) + return ctx.Render("service/list", m) +} - info := model.NewServiceDetailInfo(service) - for _, vip := range service.Endpoint.VirtualIPs { - n, err := docker.NetworkInspect(vip.NetworkID) - if err != nil { - return err - } - info.Networks = append(info.Networks, model.Network{ID: vip.NetworkID, Name: n.Name, Address: vip.Addr}) - } - - tasks, err := docker.TaskList(name, "") - if err != nil { - return err - } - - m := newModel(ctx).Add("Service", info).Add("Tasks", tasks) - return ctx.Render("service/detail", m) +func serviceDetail(ctx web.Context) error { + name := ctx.P("name") + service, _, err := docker.ServiceInspect(name) + if err != nil { + return err } - c.Raw = func(ctx web.Context) error { - name := ctx.P("name") - _, raw, err := docker.ServiceInspect(name) + info := model.NewServiceDetailInfo(service) + for _, vip := range service.Endpoint.VirtualIPs { + n, e := docker.NetworkInspect(vip.NetworkID) + if e != nil { + return e + } + info.Networks = append(info.Networks, model.Network{ID: vip.NetworkID, Name: n.Name, Address: vip.Addr}) + } + + tasks, err := docker.TaskList(name, "") + if err != nil { + return err + } + + m := newModel(ctx).Add("Service", info).Add("Tasks", tasks) + return ctx.Render("service/detail", m) +} + +func serviceRaw(ctx web.Context) error { + name := ctx.P("name") + _, raw, err := docker.ServiceInspect(name) + if err != nil { + return err + } + + j, err := misc.JSONIndent(raw) + if err != nil { + return err + } + + m := newModel(ctx).Add("Service", name).Add("Raw", j) + return ctx.Render("service/raw", m) +} + +func serviceLogs(ctx web.Context) error { + name := ctx.P("name") + line := cast.ToIntD(ctx.Q("line"), 500) + timestamps := cast.ToBoolD(ctx.Q("timestamps"), false) + stdout, stderr, err := docker.ServiceLogs(name, line, timestamps) + if err != nil { + return err + } + + m := newModel(ctx).Add("Service", name).Add("Line", line).Add("Timestamps", timestamps). + Add("Stdout", stdout.String()).Add("Stderr", stderr.String()) + return ctx.Render("service/logs", m) +} + +func serviceDelete(ctx web.Context) error { + names := strings.Split(ctx.F("names"), ",") + for _, name := range names { + if err := docker.ServiceRemove(name); err != nil { + return ajaxResult(ctx, err) + } else { + biz.Event.CreateService(model.EventActionDelete, name, ctx.User()) + } + } + return ajaxSuccess(ctx, nil) +} + +func serviceNew(ctx web.Context) error { + service := &model.ServiceInfo{} + tid := ctx.Q("template") + if tid != "" { + tpl, err := biz.Template.Get(tid) if err != nil { return err } - j, err := misc.JSONIndent(raw) - if err != nil { - return err - } - - m := newModel(ctx).Add("Service", name).Add("Raw", j) - return ctx.Render("service/raw", m) - } - - c.Logs = func(ctx web.Context) error { - name := ctx.P("name") - line := cast.ToIntD(ctx.Q("line"), 500) - timestamps := cast.ToBoolD(ctx.Q("timestamps"), false) - stdout, stderr, err := docker.ServiceLogs(name, line, timestamps) - if err != nil { - return err - } - - m := newModel(ctx).Add("Service", name).Add("Line", line).Add("Timestamps", timestamps). - Add("Stdout", stdout.String()).Add("Stderr", stderr.String()) - return ctx.Render("service/logs", m) - } - - c.Delete = func(ctx web.Context) error { - names := strings.Split(ctx.F("names"), ",") - for _, name := range names { - if err := docker.ServiceRemove(name); err != nil { - return ajaxResult(ctx, err) - } else { - biz.Event.CreateService(model.EventActionDelete, name, ctx.User()) - } - } - return ajaxSuccess(ctx, nil) - } - - c.New = func(ctx web.Context) error { - service := &model.ServiceInfo{} - tid := ctx.Q("template") - if tid != "" { - tpl, err := biz.Template.Get(tid) + if tpl != nil { + err = json.Unmarshal([]byte(tpl.Content), service) if err != nil { return err } - if tpl != nil { - err = json.Unmarshal([]byte(tpl.Content), service) + if service.Registry != "" { + var registry *model.Registry + registry, err = biz.Registry.Get(service.Registry) if err != nil { return err } - - if service.Registry != "" { - registry, err := biz.Registry.Get(service.Registry) - if err != nil { - return err - } - service.RegistryURL = registry.URL - } + service.RegistryURL = registry.URL } } - - networks, err := docker.NetworkList() - if err != nil { - return err - } - secrets, _, err := docker.SecretList("", 1, 100) - if err != nil { - return err - } - configs, _, err := docker.ConfigList("", 1, 100) - if err != nil { - return err - } - registries, err := biz.Registry.List() - if err != nil { - return err - } - checkedNetworks := set.FromSlice(service.Networks, func(i int) interface{} { return service.Networks[i] }) - - m := newModel(ctx).Add("Service", service).Add("Registries", registries). - Add("Networks", networks).Add("CheckedNetworks", checkedNetworks). - Add("Secrets", secrets).Add("Configs", configs) - return ctx.Render("service/new", m) } - c.Create = func(ctx web.Context) error { - info := &model.ServiceInfo{} - err := ctx.Bind(info) - if err == nil { - if info.Registry != "" { - registry, err := biz.Registry.Get(info.Registry) - if err != nil { - return errors.Wrap("Load registry info failed", err) - } else if registry == nil { - return errors.New("Can't load registry info") - } - - info.Image = registry.URL + "/" + info.Image - info.RegistryAuth = registry.GetEncodedAuth() - } - err = docker.ServiceCreate(info) - } - - if err == nil { - biz.Event.CreateService(model.EventActionCreate, info.Name, ctx.User()) - } - return ajaxResult(ctx, err) + networks, err := docker.NetworkList() + if err != nil { + return err } - - c.Edit = func(ctx web.Context) error { - name := ctx.P("name") - service, _, err := docker.ServiceInspect(name) - if err != nil { - return err - } - - networks, err := docker.NetworkList() - if err != nil { - return err - } - secrets, _, err := docker.SecretList("", 1, 100) - if err != nil { - return err - } - configs, _, err := docker.ConfigList("", 1, 100) - if err != nil { - return err - } - - checkedNetworks := set.FromSlice(service.Endpoint.VirtualIPs, func(i int) interface{} { return service.Endpoint.VirtualIPs[i].NetworkID }) - - m := newModel(ctx).Add("Service", model.NewServiceInfo(service)). - Add("Networks", networks).Add("CheckedNetworks", checkedNetworks). - Add("Secrets", secrets).Add("Configs", configs) - return ctx.Render("service/edit", m) + secrets, _, err := docker.SecretList("", 1, 100) + if err != nil { + return err } - - c.Update = func(ctx web.Context) error { - info := &model.ServiceInfo{} - err := ctx.Bind(info) - if err == nil { - info.Name = ctx.P("name") - err = docker.ServiceUpdate(info) - } - - if err == nil { - biz.Event.CreateService(model.EventActionUpdate, info.Name, ctx.User()) - } - return ajaxResult(ctx, err) + configs, _, err := docker.ConfigList("", 1, 100) + if err != nil { + return err } - - c.Scale = func(ctx web.Context) error { - name := ctx.F("name") - count, err := strconv.Atoi(ctx.F("count")) - if err != nil { - return err - } - - err = docker.ServiceScale(name, uint64(count)) - if err == nil { - biz.Event.CreateService(model.EventActionScale, name, ctx.User()) - } - return ajaxResult(ctx, err) + registries, err := biz.Registry.List() + if err != nil { + return err } + checkedNetworks := set.FromSlice(service.Networks, func(i int) interface{} { return service.Networks[i] }) - return + m := newModel(ctx).Add("Service", service).Add("Registries", registries). + Add("Networks", networks).Add("CheckedNetworks", checkedNetworks). + Add("Secrets", secrets).Add("Configs", configs) + return ctx.Render("service/new", m) +} + +func serviceCreate(ctx web.Context) error { + info := &model.ServiceInfo{} + err := ctx.Bind(info) + if err != nil { + return err + } + + if info.Registry != "" { + var registry *model.Registry + registry, err = biz.Registry.Get(info.Registry) + if err != nil { + return errors.Wrap("Load registry info failed", err) + } else if registry == nil { + return errors.New("Can't load registry info") + } + + info.Image = registry.URL + "/" + info.Image + info.RegistryAuth = registry.GetEncodedAuth() + } + + if err = docker.ServiceCreate(info); err == nil { + biz.Event.CreateService(model.EventActionCreate, info.Name, ctx.User()) + } + return ajaxResult(ctx, err) +} + +func serviceEdit(ctx web.Context) error { + name := ctx.P("name") + service, _, err := docker.ServiceInspect(name) + if err != nil { + return err + } + + networks, err := docker.NetworkList() + if err != nil { + return err + } + secrets, _, err := docker.SecretList("", 1, 100) + if err != nil { + return err + } + configs, _, err := docker.ConfigList("", 1, 100) + if err != nil { + return err + } + + checkedNetworks := set.FromSlice(service.Endpoint.VirtualIPs, func(i int) interface{} { return service.Endpoint.VirtualIPs[i].NetworkID }) + + m := newModel(ctx).Add("Service", model.NewServiceInfo(service)). + Add("Networks", networks).Add("CheckedNetworks", checkedNetworks). + Add("Secrets", secrets).Add("Configs", configs) + return ctx.Render("service/edit", m) +} + +func serviceUpdate(ctx web.Context) error { + info := &model.ServiceInfo{} + err := ctx.Bind(info) + if err == nil { + info.Name = ctx.P("name") + err = docker.ServiceUpdate(info) + } + + if err == nil { + biz.Event.CreateService(model.EventActionUpdate, info.Name, ctx.User()) + } + return ajaxResult(ctx, err) +} + +func serviceScale(ctx web.Context) error { + name := ctx.F("name") + count, err := strconv.Atoi(ctx.F("count")) + if err != nil { + return err + } + + err = docker.ServiceScale(name, uint64(count)) + if err == nil { + biz.Event.CreateService(model.EventActionScale, name, ctx.User()) + } + return ajaxResult(ctx, err) } diff --git a/controller/setting.go b/controller/setting.go index 7e13f69..4d36bcf 100644 --- a/controller/setting.go +++ b/controller/setting.go @@ -6,32 +6,35 @@ import ( "github.com/cuigh/swirl/model" ) +// SettingController is a controller of system setting type SettingController struct { Index web.HandlerFunc `path:"/" name:"setting.edit" authorize:"!" desc:"settings edit page"` Update web.HandlerFunc `path:"/" name:"setting.update" method:"post" authorize:"!" desc:"update settings"` } +// Setting creates an instance of SettingController func Setting() (c *SettingController) { - c = &SettingController{} - - c.Index = func(ctx web.Context) error { - setting, err := biz.Setting.Get() - if err != nil { - return err - } - - m := newModel(ctx).Add("Setting", setting) - return ctx.Render("system/setting/index", m) + return &SettingController{ + Index: settingIndex, + Update: settingUpdate, } - - c.Update = func(ctx web.Context) error { - setting := &model.Setting{} - err := ctx.Bind(setting) - if err == nil { - err = biz.Setting.Update(setting, ctx.User()) - } - return ajaxResult(ctx, err) - } - - return +} + +func settingIndex(ctx web.Context) error { + setting, err := biz.Setting.Get() + if err != nil { + return err + } + + m := newModel(ctx).Add("Setting", setting) + return ctx.Render("system/setting/index", m) +} + +func settingUpdate(ctx web.Context) error { + setting := &model.Setting{} + err := ctx.Bind(setting) + if err == nil { + err = biz.Setting.Update(setting, ctx.User()) + } + return ajaxResult(ctx, err) } diff --git a/controller/stack.go b/controller/stack.go index a11da16..83523e5 100644 --- a/controller/stack.go +++ b/controller/stack.go @@ -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) } diff --git a/controller/task.go b/controller/task.go index c2784c8..5c8c5ea 100644 --- a/controller/task.go +++ b/controller/task.go @@ -6,40 +6,43 @@ import ( "github.com/cuigh/swirl/misc" ) +// TaskController is a controller of swarm task type TaskController struct { Detail web.HandlerFunc `path:"/:id/detail" name:"task.detail" authorize:"!" desc:"task detail page"` Raw web.HandlerFunc `path:"/:id/raw" name:"task.raw" authorize:"!" desc:"task raw page"` } +// Task creates an instance of TaskController func Task() (c *TaskController) { - c = &TaskController{} - - c.Detail = func(ctx web.Context) error { - id := ctx.P("id") - task, _, err := docker.TaskInspect(id) - if err != nil { - return err - } - - m := newModel(ctx).Add("Task", task) - return ctx.Render("task/detail", m) + return &TaskController{ + Detail: taskDetail, + Raw: taskRaw, } - - c.Raw = func(ctx web.Context) error { - id := ctx.P("id") - task, raw, err := docker.TaskInspect(id) - if err != nil { - return err - } - - j, err := misc.JSONIndent(raw) - if err != nil { - return err - } - - m := newModel(ctx).Add("Task", task).Add("Raw", j) - return ctx.Render("task/raw", m) - } - - return +} + +func taskDetail(ctx web.Context) error { + id := ctx.P("id") + task, _, err := docker.TaskInspect(id) + if err != nil { + return err + } + + m := newModel(ctx).Add("Task", task) + return ctx.Render("task/detail", m) +} + +func taskRaw(ctx web.Context) error { + id := ctx.P("id") + task, raw, err := docker.TaskInspect(id) + if err != nil { + return err + } + + j, err := misc.JSONIndent(raw) + if err != nil { + return err + } + + m := newModel(ctx).Add("Task", task).Add("Raw", j) + return ctx.Render("task/raw", m) } diff --git a/controller/template.go b/controller/template.go index da1c6c5..98d8917 100644 --- a/controller/template.go +++ b/controller/template.go @@ -10,6 +10,7 @@ import ( "github.com/cuigh/swirl/model" ) +// TemplateController is a controller of service template type TemplateController struct { List web.HandlerFunc `path:"/" name:"template.list" authorize:"!" desc:"service template list page"` New web.HandlerFunc `path:"/new" name:"template.new" authorize:"!" desc:"new service template page"` @@ -19,6 +20,7 @@ type TemplateController struct { Delete web.HandlerFunc `path:"/delete" method:"post" name:"template.delete" authorize:"!" desc:"delete service template"` } +// Template creates an instance of TemplateController func Template() (c *TemplateController) { return &TemplateController{ List: templateList, @@ -80,10 +82,13 @@ func templateCreate(ctx web.Context) error { info := &model.ServiceInfo{} err := ctx.Bind(info) if err == nil { - tpl := &model.Template{Name: info.Name} + var ( + content []byte + tpl = &model.Template{Name: info.Name} + ) info.Name = "" - content, err := json.Marshal(info) + content, err = json.Marshal(info) if err != nil { return err } @@ -111,7 +116,8 @@ func templateEdit(ctx web.Context) error { service.Name = tpl.Name if service.Registry != "" { - registry, err := biz.Registry.Get(service.Registry) + var registry *model.Registry + registry, err = biz.Registry.Get(service.Registry) if err != nil { return err } @@ -146,13 +152,16 @@ func templateUpdate(ctx web.Context) error { info := &model.ServiceInfo{} err := ctx.Bind(info) if err == nil { - tpl := &model.Template{ - ID: ctx.P("id"), - Name: info.Name, - } + var ( + content []byte + tpl = &model.Template{ + ID: ctx.P("id"), + Name: info.Name, + } + ) info.Name = "" - content, err := json.Marshal(info) + content, err = json.Marshal(info) if err != nil { return err } diff --git a/controller/user.go b/controller/user.go index 89e3686..32aa9c0 100644 --- a/controller/user.go +++ b/controller/user.go @@ -7,6 +7,7 @@ import ( "github.com/cuigh/swirl/model" ) +// UserController is a controller of user type UserController struct { Index web.HandlerFunc `path:"/" name:"user.list" authorize:"!" desc:"user list page"` New web.HandlerFunc `path:"/new" name:"user.new" authorize:"!" desc:"new user page"` @@ -19,133 +20,142 @@ type UserController struct { Delete web.HandlerFunc `path:"/delete" method:"post" name:"user.delete" authorize:"!" desc:"delete user"` } +// User creates an instance of UserController func User() (c *UserController) { - c = &UserController{} + return &UserController{ + Index: userIndex, + New: userNew, + Create: userCreate, + Detail: userDetail, + Edit: userEdit, + Update: userUpdate, + Block: userBlock, + Unblock: userUnblock, + Delete: userDelete, + } +} - c.Index = func(ctx web.Context) error { - args := &model.UserListArgs{} - err := ctx.Bind(args) - if err != nil { - return err - } - args.PageSize = model.PageSize - if args.PageIndex == 0 { - args.PageIndex = 1 - } - - users, totalCount, err := biz.User.List(args) - if err != nil { - return err - } - - m := newPagerModel(ctx, totalCount, model.PageSize, args.PageIndex). - Add("Query", args.Query). - Add("Filter", args.Filter). - Add("Users", users) - return ctx.Render("system/user/list", m) +func userIndex(ctx web.Context) error { + args := &model.UserListArgs{} + err := ctx.Bind(args) + if err != nil { + return err + } + args.PageSize = model.PageSize + if args.PageIndex == 0 { + args.PageIndex = 1 } - c.New = func(ctx web.Context) error { - roles, err := biz.Role.List() - if err != nil { - return err - } - - m := newModel(ctx).Add("Roles", roles) - return ctx.Render("system/user/new", m) + users, totalCount, err := biz.User.List(args) + if err != nil { + return err } - c.Create = func(ctx web.Context) error { - user := &model.User{} - err := ctx.Bind(user) - if err == nil { - user.Type = model.UserTypeInternal - err = biz.User.Create(user, ctx.User()) - } - return ajaxResult(ctx, err) + m := newPagerModel(ctx, totalCount, model.PageSize, args.PageIndex). + Add("Query", args.Query). + Add("Filter", args.Filter). + Add("Users", users) + return ctx.Render("system/user/list", m) +} + +func userNew(ctx web.Context) error { + roles, err := biz.Role.List() + if err != nil { + return err } - c.Detail = func(ctx web.Context) error { - name := ctx.P("name") - user, err := biz.User.GetByName(name) - if err != nil { - return err - } - if user == nil { - return web.ErrNotFound - } + m := newModel(ctx).Add("Roles", roles) + return ctx.Render("system/user/new", m) +} - var ( - roles map[string]string - role *model.Role - ) - if len(user.Roles) > 0 { - roles = map[string]string{} - for _, id := range user.Roles { - role, err = biz.Role.Get(id) - if err != nil { - return err - } else if role != nil { - roles[id] = role.Name - } else { - log.Get("user").Warnf("Role %v is invalid", id) - } +func userCreate(ctx web.Context) error { + user := &model.User{} + err := ctx.Bind(user) + if err == nil { + user.Type = model.UserTypeInternal + err = biz.User.Create(user, ctx.User()) + } + return ajaxResult(ctx, err) +} + +func userDetail(ctx web.Context) error { + name := ctx.P("name") + user, err := biz.User.GetByName(name) + if err != nil { + return err + } + if user == nil { + return web.ErrNotFound + } + + var ( + roles map[string]string + role *model.Role + ) + if len(user.Roles) > 0 { + roles = map[string]string{} + for _, id := range user.Roles { + role, err = biz.Role.Get(id) + if err != nil { + return err + } else if role != nil { + roles[id] = role.Name + } else { + log.Get("user").Warnf("Role %v is invalid", id) } } - - m := newModel(ctx).Add("User", user).Add("Roles", roles) - return ctx.Render("system/user/detail", m) } - c.Edit = func(ctx web.Context) error { - name := ctx.P("name") - user, err := biz.User.GetByName(name) - if err != nil { - return err - } - if user == nil { - return web.ErrNotFound - } - - roles, err := biz.Role.List() - if err != nil { - return err - } - - userRoles := make(map[string]struct{}) - for _, id := range user.Roles { - userRoles[id] = model.Placeholder - } - m := newModel(ctx).Add("User", user).Add("Roles", roles).Add("UserRoles", userRoles) - return ctx.Render("system/user/edit", m) - } - - c.Update = func(ctx web.Context) error { - user := &model.User{} - err := ctx.Bind(user) - if err == nil { - err = biz.User.Update(user, ctx.User()) - } - return ajaxResult(ctx, err) - } - - c.Block = func(ctx web.Context) error { - id := ctx.F("id") - err := biz.User.Block(id) - return ajaxResult(ctx, err) - } - - c.Unblock = func(ctx web.Context) error { - id := ctx.F("id") - err := biz.User.Unblock(id) - return ajaxResult(ctx, err) - } - - c.Delete = func(ctx web.Context) error { - id := ctx.F("id") - err := biz.User.Delete(id) - return ajaxResult(ctx, err) - } - - return + m := newModel(ctx).Add("User", user).Add("Roles", roles) + return ctx.Render("system/user/detail", m) +} + +func userEdit(ctx web.Context) error { + name := ctx.P("name") + user, err := biz.User.GetByName(name) + if err != nil { + return err + } + if user == nil { + return web.ErrNotFound + } + + roles, err := biz.Role.List() + if err != nil { + return err + } + + userRoles := make(map[string]struct{}) + for _, id := range user.Roles { + userRoles[id] = model.Placeholder + } + m := newModel(ctx).Add("User", user).Add("Roles", roles).Add("UserRoles", userRoles) + return ctx.Render("system/user/edit", m) +} + +func userUpdate(ctx web.Context) error { + user := &model.User{} + err := ctx.Bind(user) + if err == nil { + err = biz.User.Update(user, ctx.User()) + } + return ajaxResult(ctx, err) +} + +func userBlock(ctx web.Context) error { + id := ctx.F("id") + err := biz.User.Block(id) + return ajaxResult(ctx, err) +} + +func userUnblock(ctx web.Context) error { + id := ctx.F("id") + err := biz.User.Unblock(id) + return ajaxResult(ctx, err) +} + +func userDelete(ctx web.Context) error { + id := ctx.F("id") + err := biz.User.Delete(id) + return ajaxResult(ctx, err) } diff --git a/controller/volume.go b/controller/volume.go index 8e1a4c9..fb3c9a0 100644 --- a/controller/volume.go +++ b/controller/volume.go @@ -11,6 +11,7 @@ import ( "github.com/cuigh/swirl/model" ) +// VolumeController is a controller of docker volume type VolumeController struct { List web.HandlerFunc `path:"/" name:"volume.list" authorize:"!" desc:"volume list page"` New web.HandlerFunc `path:"/new" name:"volume.new" authorize:"!" desc:"new volume page"` @@ -21,86 +22,93 @@ type VolumeController struct { Raw web.HandlerFunc `path:"/:name/raw" name:"volume.raw" authorize:"!" desc:"volume raw page"` } +// Volume creates an instance of VolumeController func Volume() (c *VolumeController) { - c = &VolumeController{} - - c.List = func(ctx web.Context) error { - //name := ctx.Q("name") - page := cast.ToIntD(ctx.Q("page"), 1) - volumes, totalCount, err := docker.VolumeList(page, model.PageSize) - if err != nil { - return err - } - - m := newPagerModel(ctx, totalCount, model.PageSize, page). - //Add("Name", name). - Add("Volumes", volumes) - return ctx.Render("volume/list", m) + return &VolumeController{ + List: volumeList, + New: volumeNew, + Create: volumeCreate, + Delete: volumeDelete, + Prune: volumePrune, + Detail: volumeDetail, + Raw: volumeRaw, } - - c.New = func(ctx web.Context) error { - m := newModel(ctx) - return ctx.Render("/volume/new", m) - } - - c.Create = func(ctx web.Context) error { - info := &model.VolumeCreateInfo{} - err := ctx.Bind(info) - if err != nil { - return err - } - err = docker.VolumeCreate(info) - if err == nil { - biz.Event.CreateVolume(model.EventActionCreate, info.Name, ctx.User()) - } - return ajaxResult(ctx, err) - } - - c.Delete = func(ctx web.Context) error { - names := strings.Split(ctx.F("names"), ",") - for _, name := range names { - if err := docker.VolumeRemove(name); err != nil { - return ajaxResult(ctx, err) - } else { - biz.Event.CreateVolume(model.EventActionDelete, name, ctx.User()) - } - } - return ajaxSuccess(ctx, nil) - } - - c.Prune = func(ctx web.Context) error { - report, err := docker.VolumePrune() - if err == nil { - return ajaxSuccess(ctx, report) - } - return ajaxResult(ctx, err) - } - - c.Detail = func(ctx web.Context) error { - name := ctx.P("name") - volume, _, err := docker.VolumeInspectRaw(name) - if err != nil { - return err - } - m := newModel(ctx).Add("Volume", volume) - return ctx.Render("volume/detail", m) - } - - c.Raw = func(ctx web.Context) error { - name := ctx.P("name") - _, raw, err := docker.VolumeInspectRaw(name) - if err != nil { - return err - } - - j, err := misc.JSONIndent(raw) - if err != nil { - return err - } - - m := newModel(ctx).Add("Volume", name).Add("Raw", j) - return ctx.Render("volume/raw", m) - } - - return +} + +func volumeList(ctx web.Context) error { + //name := ctx.Q("name") + page := cast.ToIntD(ctx.Q("page"), 1) + volumes, totalCount, err := docker.VolumeList(page, model.PageSize) + if err != nil { + return err + } + + m := newPagerModel(ctx, totalCount, model.PageSize, page). + //Add("Name", name). + Add("Volumes", volumes) + return ctx.Render("volume/list", m) +} + +func volumeNew(ctx web.Context) error { + m := newModel(ctx) + return ctx.Render("/volume/new", m) +} + +func volumeCreate(ctx web.Context) error { + info := &model.VolumeCreateInfo{} + err := ctx.Bind(info) + if err != nil { + return err + } + err = docker.VolumeCreate(info) + if err == nil { + biz.Event.CreateVolume(model.EventActionCreate, info.Name, ctx.User()) + } + return ajaxResult(ctx, err) +} + +func volumeDelete(ctx web.Context) error { + names := strings.Split(ctx.F("names"), ",") + for _, name := range names { + if err := docker.VolumeRemove(name); err != nil { + return ajaxResult(ctx, err) + } else { + biz.Event.CreateVolume(model.EventActionDelete, name, ctx.User()) + } + } + return ajaxSuccess(ctx, nil) +} + +func volumePrune(ctx web.Context) error { + report, err := docker.VolumePrune() + if err == nil { + return ajaxSuccess(ctx, report) + } + return ajaxResult(ctx, err) +} + +func volumeDetail(ctx web.Context) error { + name := ctx.P("name") + volume, _, err := docker.VolumeInspectRaw(name) + if err != nil { + return err + } + m := newModel(ctx).Add("Volume", volume) + return ctx.Render("volume/detail", m) +} + +func volumeRaw(ctx web.Context) error { + name := ctx.P("name") + _, raw, err := docker.VolumeInspectRaw(name) + if err != nil { + return err + } + + j, err := misc.JSONIndent(raw) + if err != nil { + return err + } + + m := newModel(ctx).Add("Volume", name).Add("Raw", j) + return ctx.Render("volume/raw", m) }