Refactor codes

This commit is contained in:
cuigh 2017-10-11 11:26:03 +08:00
parent 45f50d159f
commit 63d9b9bd53
18 changed files with 1220 additions and 1104 deletions

View File

@ -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,10 +20,19 @@ 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{}
return &ConfigController{
List: configList,
Delete: configDelete,
New: configNew,
Create: configCreate,
Edit: configEdit,
Update: configUpdate,
}
}
c.List = func(ctx web.Context) error {
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)
@ -34,20 +44,20 @@ func Config() (c *ConfigController) {
Add("Name", name).
Add("Configs", configs)
return ctx.Render("config/list", m)
}
}
c.Delete = func(ctx web.Context) error {
func configDelete(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 {
func configNew(ctx web.Context) error {
m := newModel(ctx)
return ctx.Render("config/new", m)
}
}
c.Create = func(ctx web.Context) error {
func configCreate(ctx web.Context) error {
v := &model.ConfigCreateInfo{}
err := ctx.Bind(v)
if err == nil {
@ -57,9 +67,9 @@ func Config() (c *ConfigController) {
}
}
return ajaxResult(ctx, err)
}
}
c.Edit = func(ctx web.Context) error {
func configEdit(ctx web.Context) error {
id := ctx.P("id")
cfg, _, err := docker.ConfigInspect(id)
if err != nil {
@ -67,9 +77,9 @@ func Config() (c *ConfigController) {
}
m := newModel(ctx).Add("Config", cfg)
return ctx.Render("config/edit", m)
}
}
c.Update = func(ctx web.Context) error {
func configUpdate(ctx web.Context) error {
v := &model.ConfigUpdateInfo{}
err := ctx.Bind(v)
if err == nil {
@ -79,7 +89,4 @@ func Config() (c *ConfigController) {
}
}
return ajaxResult(ctx, err)
}
return
}

View File

@ -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,

View File

@ -6,14 +6,19 @@ 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 {
func eventList(ctx web.Context) error {
args := &model.EventListArgs{}
err := ctx.Bind(args)
if err != nil {
@ -32,7 +37,4 @@ func Event() (c *EventController) {
m := newPagerModel(ctx, totalCount, model.PageSize, args.PageIndex).
Add("Events", events).Add("Args", args)
return ctx.Render("system/event/list", m)
}
return
}

View File

@ -8,19 +8,29 @@ 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{}
return &HomeController{
Index: homeIndex,
Login: homeLogin,
InitGet: homeInitGet,
InitPost: homeInitPost,
Error403: homeError403,
Error404: homeError404,
}
}
c.Index = func(ctx web.Context) (err error) {
func homeIndex(ctx web.Context) (err error) {
var (
count int
m = newModel(ctx)
@ -47,9 +57,9 @@ func Home() (c *HomeController) {
m.Add("StackCount", count)
return ctx.Render("index", m)
}
}
c.Login = func(ctx web.Context) error {
func homeLogin(ctx web.Context) error {
count, err := biz.User.Count()
if err != nil {
return err
@ -64,9 +74,9 @@ func Home() (c *HomeController) {
return ctx.Redirect(u)
}
return ctx.Render("login", nil)
}
}
c.InitGet = func(ctx web.Context) error {
func homeInitGet(ctx web.Context) error {
count, err := biz.User.Count()
if err != nil {
return err
@ -74,9 +84,9 @@ func Home() (c *HomeController) {
return ctx.Redirect("login")
}
return ctx.Render("init", nil)
}
}
c.InitPost = func(ctx web.Context) error {
func homeInitPost(ctx web.Context) error {
count, err := biz.User.Count()
if err != nil {
return err
@ -94,15 +104,12 @@ func Home() (c *HomeController) {
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 homeError403(ctx web.Context) error {
return ctx.Render("403", nil)
}
func homeError404(ctx web.Context) error {
return ctx.Render("404", nil)
}

View File

@ -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,

View File

@ -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,11 +19,20 @@ 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{}
return &NetworkController{
List: networkList,
New: networkNew,
Create: networkCreate,
Delete: networkDelete,
Disconnect: networkDisconnect,
Detail: networkDetail,
Raw: networkRaw,
}
}
c.List = func(ctx web.Context) error {
func networkList(ctx web.Context) error {
networks, err := docker.NetworkList()
if err != nil {
return err
@ -30,14 +40,14 @@ func Network() (c *NetworkController) {
m := newModel(ctx).Add("Networks", networks)
return ctx.Render("network/list", m)
}
}
c.New = func(ctx web.Context) error {
func networkNew(ctx web.Context) error {
m := newModel(ctx)
return ctx.Render("/network/new", m)
}
}
c.Create = func(ctx web.Context) error {
func networkCreate(ctx web.Context) error {
info := &model.NetworkCreateInfo{}
err := ctx.Bind(info)
if err != nil {
@ -48,18 +58,18 @@ func Network() (c *NetworkController) {
biz.Event.CreateNetwork(model.EventActionCreate, info.Name, info.Name, ctx.User())
}
return ajaxResult(ctx, err)
}
}
c.Delete = func(ctx web.Context) error {
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)
}
}
c.Disconnect = func(ctx web.Context) error {
func networkDisconnect(ctx web.Context) error {
name := ctx.P("name")
container := ctx.F("container")
err := docker.NetworkDisconnect(name, container)
@ -67,9 +77,9 @@ func Network() (c *NetworkController) {
biz.Event.CreateNetwork(model.EventActionDisconnect, name, name+" <-> "+container, ctx.User())
}
return ajaxResult(ctx, err)
}
}
c.Detail = func(ctx web.Context) error {
func networkDetail(ctx web.Context) error {
name := ctx.P("name")
network, err := docker.NetworkInspect(name)
if err != nil {
@ -77,9 +87,9 @@ func Network() (c *NetworkController) {
}
m := newModel(ctx).Add("Network", network)
return ctx.Render("network/detail", m)
}
}
c.Raw = func(ctx web.Context) error {
func networkRaw(ctx web.Context) error {
name := ctx.P("name")
raw, err := docker.NetworkInspectRaw(name)
if err != nil {
@ -93,7 +103,4 @@ func Network() (c *NetworkController) {
m := newModel(ctx).Add("Network", name).Add("Raw", j)
return ctx.Render("network/raw", m)
}
return
}

View File

@ -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,10 +17,19 @@ 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{}
return &NodeController{
List: nodeList,
Detail: nodeDetail,
Raw: nodeRaw,
Delete: nodeDelete,
Edit: nodeEdit,
Update: nodeUpdate,
}
}
c.List = func(ctx web.Context) error {
func nodeList(ctx web.Context) error {
nodes, err := docker.NodeList()
if err != nil {
return err
@ -27,15 +37,15 @@ func Node() (c *NodeController) {
m := newModel(ctx).Add("Nodes", nodes)
return ctx.Render("node/list", m)
}
}
c.Delete = func(ctx web.Context) error {
func nodeDelete(ctx web.Context) error {
id := ctx.F("id")
err := docker.NodeRemove(id)
return ajaxResult(ctx, err)
}
}
c.Detail = func(ctx web.Context) error {
func nodeDetail(ctx web.Context) error {
id := ctx.P("id")
node, _, err := docker.NodeInspect(id)
if err != nil {
@ -49,9 +59,9 @@ func Node() (c *NodeController) {
m := newModel(ctx).Add("Node", node).Add("Tasks", tasks)
return ctx.Render("node/detail", m)
}
}
c.Raw = func(ctx web.Context) error {
func nodeRaw(ctx web.Context) error {
id := ctx.P("id")
node, raw, err := docker.NodeInspect(id)
if err != nil {
@ -65,9 +75,9 @@ func Node() (c *NodeController) {
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 {
func nodeEdit(ctx web.Context) error {
id := ctx.P("id")
node, _, err := docker.NodeInspect(id)
if err != nil {
@ -76,9 +86,9 @@ func Node() (c *NodeController) {
m := newModel(ctx).Add("Node", node)
return ctx.Render("node/edit", m)
}
}
c.Update = func(ctx web.Context) error {
func nodeUpdate(ctx web.Context) error {
id := ctx.P("id")
info := &model.NodeUpdateInfo{}
err := ctx.Bind(info)
@ -86,7 +96,4 @@ func Node() (c *NodeController) {
err = docker.NodeUpdate(id, info)
}
return ajaxResult(ctx, err)
}
return
}

View File

@ -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,10 +14,17 @@ 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{}
return &ProfileController{
Index: profileIndex,
ModifyInfo: profileModifyInfo,
Password: profilePassword,
ModifyPassword: profileModifyPassword,
}
}
c.Index = func(ctx web.Context) error {
func profileIndex(ctx web.Context) error {
user, err := biz.User.GetByID(ctx.User().ID())
if err != nil {
return err
@ -24,9 +32,9 @@ func Profile() (c *ProfileController) {
m := newModel(ctx).Add("User", user)
return ctx.Render("profile/index", m)
}
}
c.ModifyInfo = func(ctx web.Context) error {
func profileModifyInfo(ctx web.Context) error {
user := &model.User{}
err := ctx.Bind(user)
if err == nil {
@ -34,19 +42,16 @@ func Profile() (c *ProfileController) {
err = biz.User.UpdateInfo(user)
}
return ajaxResult(ctx, err)
}
}
c.Password = func(ctx web.Context) error {
func profilePassword(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 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)
}

View File

@ -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,10 +14,17 @@ 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{}
return &RegistryController{
List: registryList,
Create: registryCreate,
Delete: registryDelete,
Update: registryUpdate,
}
}
c.List = func(ctx web.Context) error {
func registryList(ctx web.Context) error {
registries, err := biz.Registry.List()
if err != nil {
return err
@ -24,9 +32,9 @@ func Registry() (c *RegistryController) {
m := newModel(ctx).Add("Registries", registries)
return ctx.Render("registry/list", m)
}
}
c.Create = func(ctx web.Context) error {
func registryCreate(ctx web.Context) error {
registry := &model.Registry{}
err := ctx.Bind(registry)
if err != nil {
@ -34,15 +42,15 @@ func Registry() (c *RegistryController) {
}
err = biz.Registry.Create(registry, ctx.User())
return ajaxResult(ctx, err)
}
}
c.Delete = func(ctx web.Context) error {
func registryDelete(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 {
func registryUpdate(ctx web.Context) error {
registry := &model.Registry{}
err := ctx.Bind(registry)
if err != nil {
@ -50,7 +58,4 @@ func Registry() (c *RegistryController) {
}
err = biz.Registry.Update(registry, ctx.User())
return ajaxResult(ctx, err)
}
return
}

View File

@ -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,10 +18,20 @@ 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{}
return &RoleController{
Index: roleIndex,
New: roleNew,
Create: roleCreate,
Delete: roleDelete,
Detail: roleDetail,
Edit: roleEdit,
Update: roleUpdate,
}
}
c.Index = func(ctx web.Context) error {
func roleIndex(ctx web.Context) error {
roles, err := biz.Role.List()
if err != nil {
return err
@ -28,29 +39,29 @@ func Role() (c *RoleController) {
m := newModel(ctx).Add("Roles", roles)
return ctx.Render("system/role/list", m)
}
}
c.New = func(ctx web.Context) error {
func roleNew(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 {
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)
}
}
c.Delete = func(ctx web.Context) error {
func roleDelete(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 {
func roleDetail(ctx web.Context) error {
id := ctx.P("id")
role, err := biz.Role.Get(id)
if err != nil {
@ -66,9 +77,9 @@ func Role() (c *RoleController) {
}
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 {
func roleEdit(ctx web.Context) error {
id := ctx.P("id")
role, err := biz.Role.Get(id)
if err != nil {
@ -84,9 +95,9 @@ func Role() (c *RoleController) {
}
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 {
func roleUpdate(ctx web.Context) error {
role := &model.Role{}
err := ctx.Bind(role)
if err == nil {
@ -94,7 +105,4 @@ func Role() (c *RoleController) {
err = biz.Role.Update(role, ctx.User())
}
return ajaxResult(ctx, err)
}
return
}

View File

@ -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,10 +20,18 @@ 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 {
return &SecretController{
List: secretList,
Delete: secretDelete,
New: secretNew,
Create: secretCreate,
Edit: secretEdit,
Update: secretUpdate,
}
}
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)
@ -34,9 +43,9 @@ func Secret() (c *SecretController) {
Add("Name", name).
Add("Secrets", secrets)
return ctx.Render("secret/list", m)
}
}
c.Delete = func(ctx web.Context) error {
func secretDelete(ctx web.Context) error {
ids := strings.Split(ctx.F("ids"), ",")
for _, id := range ids {
err := docker.SecretRemove(id)
@ -48,14 +57,14 @@ func Secret() (c *SecretController) {
}
}
return ajaxSuccess(ctx, nil)
}
}
c.New = func(ctx web.Context) error {
func secretNew(ctx web.Context) error {
m := newModel(ctx)
return ctx.Render("secret/new", m)
}
}
c.Create = func(ctx web.Context) error {
func secretCreate(ctx web.Context) error {
v := &model.ConfigCreateInfo{}
err := ctx.Bind(v)
if err == nil {
@ -65,9 +74,9 @@ func Secret() (c *SecretController) {
}
}
return ajaxResult(ctx, err)
}
}
c.Edit = func(ctx web.Context) error {
func secretEdit(ctx web.Context) error {
id := ctx.P("id")
secret, _, err := docker.SecretInspect(id)
if err != nil {
@ -75,9 +84,9 @@ func Secret() (c *SecretController) {
}
m := newModel(ctx).Add("Secret", secret)
return ctx.Render("secret/edit", m)
}
}
c.Update = func(ctx web.Context) error {
func secretUpdate(ctx web.Context) error {
v := &model.ConfigUpdateInfo{}
err := ctx.Bind(v)
if err == nil {
@ -87,7 +96,4 @@ func Secret() (c *SecretController) {
}
}
return ajaxResult(ctx, err)
}
return
}

View File

@ -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,10 +29,23 @@ 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 {
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)
@ -43,9 +57,9 @@ func Service() (c *ServiceController) {
Add("Name", name).
Add("Services", services)
return ctx.Render("service/list", m)
}
}
c.Detail = func(ctx web.Context) error {
func serviceDetail(ctx web.Context) error {
name := ctx.P("name")
service, _, err := docker.ServiceInspect(name)
if err != nil {
@ -54,9 +68,9 @@ func Service() (c *ServiceController) {
info := model.NewServiceDetailInfo(service)
for _, vip := range service.Endpoint.VirtualIPs {
n, err := docker.NetworkInspect(vip.NetworkID)
if err != nil {
return err
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})
}
@ -68,9 +82,9 @@ func Service() (c *ServiceController) {
m := newModel(ctx).Add("Service", info).Add("Tasks", tasks)
return ctx.Render("service/detail", m)
}
}
c.Raw = func(ctx web.Context) error {
func serviceRaw(ctx web.Context) error {
name := ctx.P("name")
_, raw, err := docker.ServiceInspect(name)
if err != nil {
@ -84,9 +98,9 @@ func Service() (c *ServiceController) {
m := newModel(ctx).Add("Service", name).Add("Raw", j)
return ctx.Render("service/raw", m)
}
}
c.Logs = func(ctx web.Context) error {
func serviceLogs(ctx web.Context) error {
name := ctx.P("name")
line := cast.ToIntD(ctx.Q("line"), 500)
timestamps := cast.ToBoolD(ctx.Q("timestamps"), false)
@ -98,9 +112,9 @@ func Service() (c *ServiceController) {
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 {
func serviceDelete(ctx web.Context) error {
names := strings.Split(ctx.F("names"), ",")
for _, name := range names {
if err := docker.ServiceRemove(name); err != nil {
@ -110,9 +124,9 @@ func Service() (c *ServiceController) {
}
}
return ajaxSuccess(ctx, nil)
}
}
c.New = func(ctx web.Context) error {
func serviceNew(ctx web.Context) error {
service := &model.ServiceInfo{}
tid := ctx.Q("template")
if tid != "" {
@ -128,7 +142,8 @@ func Service() (c *ServiceController) {
}
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
}
@ -159,14 +174,18 @@ func Service() (c *ServiceController) {
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 {
func serviceCreate(ctx web.Context) error {
info := &model.ServiceInfo{}
err := ctx.Bind(info)
if err == nil {
if err != nil {
return err
}
if info.Registry != "" {
registry, err := biz.Registry.Get(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 {
@ -176,16 +195,14 @@ func Service() (c *ServiceController) {
info.Image = registry.URL + "/" + info.Image
info.RegistryAuth = registry.GetEncodedAuth()
}
err = docker.ServiceCreate(info)
}
if err == nil {
if err = docker.ServiceCreate(info); err == nil {
biz.Event.CreateService(model.EventActionCreate, info.Name, ctx.User())
}
return ajaxResult(ctx, err)
}
}
c.Edit = func(ctx web.Context) error {
func serviceEdit(ctx web.Context) error {
name := ctx.P("name")
service, _, err := docker.ServiceInspect(name)
if err != nil {
@ -211,9 +228,9 @@ func Service() (c *ServiceController) {
Add("Networks", networks).Add("CheckedNetworks", checkedNetworks).
Add("Secrets", secrets).Add("Configs", configs)
return ctx.Render("service/edit", m)
}
}
c.Update = func(ctx web.Context) error {
func serviceUpdate(ctx web.Context) error {
info := &model.ServiceInfo{}
err := ctx.Bind(info)
if err == nil {
@ -225,9 +242,9 @@ func Service() (c *ServiceController) {
biz.Event.CreateService(model.EventActionUpdate, info.Name, ctx.User())
}
return ajaxResult(ctx, err)
}
}
c.Scale = func(ctx web.Context) error {
func serviceScale(ctx web.Context) error {
name := ctx.F("name")
count, err := strconv.Atoi(ctx.F("count"))
if err != nil {
@ -239,7 +256,4 @@ func Service() (c *ServiceController) {
biz.Event.CreateService(model.EventActionScale, name, ctx.User())
}
return ajaxResult(ctx, err)
}
return
}

View File

@ -6,15 +6,21 @@ 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{}
return &SettingController{
Index: settingIndex,
Update: settingUpdate,
}
}
c.Index = func(ctx web.Context) error {
func settingIndex(ctx web.Context) error {
setting, err := biz.Setting.Get()
if err != nil {
return err
@ -22,16 +28,13 @@ func Setting() (c *SettingController) {
m := newModel(ctx).Add("Setting", setting)
return ctx.Render("system/setting/index", m)
}
}
c.Update = func(ctx web.Context) error {
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)
}
return
}

View File

@ -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,10 +22,23 @@ 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 {
func stackTaskList(ctx web.Context) error {
stacks, err := docker.StackList()
if err != nil {
return err
@ -32,18 +46,18 @@ func Stack() (c *StackController) {
m := newModel(ctx).Add("Stacks", stacks)
return ctx.Render("stack/task/list", m)
}
}
c.TaskDelete = func(ctx web.Context) error {
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)
}
}
c.ArchiveList = func(ctx web.Context) error {
func stackArchiveList(ctx web.Context) error {
args := &model.ArchiveListArgs{}
err := ctx.Bind(args)
if err != nil {
@ -63,9 +77,9 @@ func Stack() (c *StackController) {
Add("Name", args.Name).
Add("Archives", archives)
return ctx.Render("stack/archive/list", m)
}
}
c.ArchiveDetail = func(ctx web.Context) error {
func stackArchiveDetail(ctx web.Context) error {
id := ctx.P("id")
archive, err := biz.Archive.Get(id)
if err != nil {
@ -77,9 +91,9 @@ func Stack() (c *StackController) {
m := newModel(ctx).Add("Archive", archive)
return ctx.Render("stack/archive/detail", m)
}
}
c.ArchiveEdit = func(ctx web.Context) error {
func stackArchiveEdit(ctx web.Context) error {
id := ctx.P("id")
archive, err := biz.Archive.Get(id)
if err != nil {
@ -91,9 +105,9 @@ func Stack() (c *StackController) {
m := newModel(ctx).Add("Archive", archive)
return ctx.Render("stack/archive/edit", m)
}
}
c.ArchiveUpdate = func(ctx web.Context) error {
func stackArchiveUpdate(ctx web.Context) error {
archive := &model.Archive{}
err := ctx.Bind(archive)
if err == nil {
@ -110,15 +124,15 @@ func Stack() (c *StackController) {
biz.Event.CreateStackArchive(model.EventActionUpdate, archive.ID, archive.Name, ctx.User())
}
return ajaxResult(ctx, err)
}
}
c.ArchiveDelete = func(ctx web.Context) error {
func stackArchiveDelete(ctx web.Context) error {
id := ctx.F("id")
err := biz.Archive.Delete(id, ctx.User())
return ajaxResult(ctx, err)
}
}
c.ArchiveDeploy = func(ctx web.Context) error {
func stackArchiveDeploy(ctx web.Context) error {
id := ctx.F("id")
archive, err := biz.Archive.Get(id)
if err != nil {
@ -149,14 +163,14 @@ func Stack() (c *StackController) {
err = docker.StackDeploy(archive.Name, archive.Content, authes)
return ajaxResult(ctx, err)
}
}
c.ArchiveNew = func(ctx web.Context) error {
func stackArchiveNew(ctx web.Context) error {
m := newModel(ctx)
return ctx.Render("stack/archive/new", m)
}
}
c.ArchiveCreate = func(ctx web.Context) error {
func stackArchiveCreate(ctx web.Context) error {
archive := &model.Archive{}
err := ctx.Bind(archive)
if err == nil {
@ -171,7 +185,4 @@ func Stack() (c *StackController) {
err = biz.Archive.Create(archive)
}
return ajaxResult(ctx, err)
}
return
}

View File

@ -6,15 +6,21 @@ 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{}
return &TaskController{
Detail: taskDetail,
Raw: taskRaw,
}
}
c.Detail = func(ctx web.Context) error {
func taskDetail(ctx web.Context) error {
id := ctx.P("id")
task, _, err := docker.TaskInspect(id)
if err != nil {
@ -23,9 +29,9 @@ func Task() (c *TaskController) {
m := newModel(ctx).Add("Task", task)
return ctx.Render("task/detail", m)
}
}
c.Raw = func(ctx web.Context) error {
func taskRaw(ctx web.Context) error {
id := ctx.P("id")
task, raw, err := docker.TaskInspect(id)
if err != nil {
@ -39,7 +45,4 @@ func Task() (c *TaskController) {
m := newModel(ctx).Add("Task", task).Add("Raw", j)
return ctx.Render("task/raw", m)
}
return
}

View File

@ -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{
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
}

View File

@ -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,10 +20,22 @@ 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 {
func userIndex(ctx web.Context) error {
args := &model.UserListArgs{}
err := ctx.Bind(args)
if err != nil {
@ -43,9 +56,9 @@ func User() (c *UserController) {
Add("Filter", args.Filter).
Add("Users", users)
return ctx.Render("system/user/list", m)
}
}
c.New = func(ctx web.Context) error {
func userNew(ctx web.Context) error {
roles, err := biz.Role.List()
if err != nil {
return err
@ -53,9 +66,9 @@ func User() (c *UserController) {
m := newModel(ctx).Add("Roles", roles)
return ctx.Render("system/user/new", m)
}
}
c.Create = func(ctx web.Context) error {
func userCreate(ctx web.Context) error {
user := &model.User{}
err := ctx.Bind(user)
if err == nil {
@ -63,9 +76,9 @@ func User() (c *UserController) {
err = biz.User.Create(user, ctx.User())
}
return ajaxResult(ctx, err)
}
}
c.Detail = func(ctx web.Context) error {
func userDetail(ctx web.Context) error {
name := ctx.P("name")
user, err := biz.User.GetByName(name)
if err != nil {
@ -95,9 +108,9 @@ func User() (c *UserController) {
m := newModel(ctx).Add("User", user).Add("Roles", roles)
return ctx.Render("system/user/detail", m)
}
}
c.Edit = func(ctx web.Context) error {
func userEdit(ctx web.Context) error {
name := ctx.P("name")
user, err := biz.User.GetByName(name)
if err != nil {
@ -118,34 +131,31 @@ func User() (c *UserController) {
}
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 {
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)
}
}
c.Block = func(ctx web.Context) error {
func userBlock(ctx web.Context) error {
id := ctx.F("id")
err := biz.User.Block(id)
return ajaxResult(ctx, err)
}
}
c.Unblock = func(ctx web.Context) error {
func userUnblock(ctx web.Context) error {
id := ctx.F("id")
err := biz.User.Unblock(id)
return ajaxResult(ctx, err)
}
}
c.Delete = func(ctx web.Context) error {
func userDelete(ctx web.Context) error {
id := ctx.F("id")
err := biz.User.Delete(id)
return ajaxResult(ctx, err)
}
return
}

View File

@ -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,10 +22,20 @@ 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{}
return &VolumeController{
List: volumeList,
New: volumeNew,
Create: volumeCreate,
Delete: volumeDelete,
Prune: volumePrune,
Detail: volumeDetail,
Raw: volumeRaw,
}
}
c.List = func(ctx web.Context) error {
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)
@ -36,14 +47,14 @@ func Volume() (c *VolumeController) {
//Add("Name", name).
Add("Volumes", volumes)
return ctx.Render("volume/list", m)
}
}
c.New = func(ctx web.Context) error {
func volumeNew(ctx web.Context) error {
m := newModel(ctx)
return ctx.Render("/volume/new", m)
}
}
c.Create = func(ctx web.Context) error {
func volumeCreate(ctx web.Context) error {
info := &model.VolumeCreateInfo{}
err := ctx.Bind(info)
if err != nil {
@ -54,9 +65,9 @@ func Volume() (c *VolumeController) {
biz.Event.CreateVolume(model.EventActionCreate, info.Name, ctx.User())
}
return ajaxResult(ctx, err)
}
}
c.Delete = func(ctx web.Context) error {
func volumeDelete(ctx web.Context) error {
names := strings.Split(ctx.F("names"), ",")
for _, name := range names {
if err := docker.VolumeRemove(name); err != nil {
@ -66,17 +77,17 @@ func Volume() (c *VolumeController) {
}
}
return ajaxSuccess(ctx, nil)
}
}
c.Prune = func(ctx web.Context) error {
func volumePrune(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 {
func volumeDetail(ctx web.Context) error {
name := ctx.P("name")
volume, _, err := docker.VolumeInspectRaw(name)
if err != nil {
@ -84,9 +95,9 @@ func Volume() (c *VolumeController) {
}
m := newModel(ctx).Add("Volume", volume)
return ctx.Render("volume/detail", m)
}
}
c.Raw = func(ctx web.Context) error {
func volumeRaw(ctx web.Context) error {
name := ctx.P("name")
_, raw, err := docker.VolumeInspectRaw(name)
if err != nil {
@ -100,7 +111,4 @@ func Volume() (c *VolumeController) {
m := newModel(ctx).Add("Volume", name).Add("Raw", j)
return ctx.Render("volume/raw", m)
}
return
}