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)
@ -36,18 +46,18 @@ func Config() (c *ConfigController) {
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 {
@ -59,7 +69,7 @@ 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 {
@ -69,7 +79,7 @@ func Config() (c *ConfigController) {
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 {
@ -80,6 +90,3 @@ 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 {
@ -33,6 +38,3 @@ func Event() (c *EventController) {
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)
@ -49,7 +59,7 @@ func Home() (c *HomeController) {
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
@ -66,7 +76,7 @@ func Home() (c *HomeController) {
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
@ -76,7 +86,7 @@ func Home() (c *HomeController) {
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
@ -96,13 +106,10 @@ func Home() (c *HomeController) {
return ajaxResult(ctx, err)
}
c.Error403 = func(ctx web.Context) error {
func homeError403(ctx web.Context) error {
return ctx.Render("403", nil)
}
c.Error404 = func(ctx web.Context) error {
func homeError404(ctx web.Context) error {
return ctx.Render("404", nil)
}
return
}

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
@ -32,12 +42,12 @@ func Network() (c *NetworkController) {
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 {
@ -50,7 +60,7 @@ func Network() (c *NetworkController) {
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 {
@ -59,7 +69,7 @@ func Network() (c *NetworkController) {
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)
@ -69,7 +79,7 @@ func Network() (c *NetworkController) {
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 {
@ -79,7 +89,7 @@ func Network() (c *NetworkController) {
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 {
@ -94,6 +104,3 @@ 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
@ -29,13 +39,13 @@ func Node() (c *NodeController) {
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 {
@ -51,7 +61,7 @@ func Node() (c *NodeController) {
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 {
@ -67,7 +77,7 @@ func Node() (c *NodeController) {
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 {
@ -78,7 +88,7 @@ func Node() (c *NodeController) {
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)
@ -87,6 +97,3 @@ func Node() (c *NodeController) {
}
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
@ -26,7 +34,7 @@ func Profile() (c *ProfileController) {
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 {
@ -36,17 +44,14 @@ func Profile() (c *ProfileController) {
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)
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)
}
return
}

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
@ -26,7 +34,7 @@ func Registry() (c *RegistryController) {
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 {
@ -36,13 +44,13 @@ func Registry() (c *RegistryController) {
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 {
@ -51,6 +59,3 @@ 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
@ -30,12 +41,12 @@ func Role() (c *RoleController) {
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 {
@ -44,13 +55,13 @@ func Role() (c *RoleController) {
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 {
@ -68,7 +79,7 @@ func Role() (c *RoleController) {
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 {
@ -86,7 +97,7 @@ func Role() (c *RoleController) {
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 {
@ -95,6 +106,3 @@ func Role() (c *RoleController) {
}
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)
@ -36,7 +45,7 @@ func Secret() (c *SecretController) {
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)
@ -50,12 +59,12 @@ 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 {
@ -67,7 +76,7 @@ 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 {
@ -77,7 +86,7 @@ func Secret() (c *SecretController) {
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 {
@ -88,6 +97,3 @@ 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)
@ -45,7 +59,7 @@ func Service() (c *ServiceController) {
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})
}
@ -70,7 +84,7 @@ func Service() (c *ServiceController) {
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 {
@ -86,7 +100,7 @@ func Service() (c *ServiceController) {
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)
@ -100,7 +114,7 @@ func Service() (c *ServiceController) {
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 {
@ -112,7 +126,7 @@ 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
}
@ -161,12 +176,16 @@ func Service() (c *ServiceController) {
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 {
@ -213,7 +230,7 @@ func Service() (c *ServiceController) {
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 {
@ -227,7 +244,7 @@ func Service() (c *ServiceController) {
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 {
@ -240,6 +257,3 @@ func Service() (c *ServiceController) {
}
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
@ -24,7 +30,7 @@ func Setting() (c *SettingController) {
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 {
@ -32,6 +38,3 @@ func Setting() (c *SettingController) {
}
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
@ -34,7 +48,7 @@ func Stack() (c *StackController) {
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 {
@ -43,7 +57,7 @@ func Stack() (c *StackController) {
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 {
@ -65,7 +79,7 @@ func Stack() (c *StackController) {
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 {
@ -79,7 +93,7 @@ func Stack() (c *StackController) {
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 {
@ -93,7 +107,7 @@ func Stack() (c *StackController) {
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 {
@ -112,13 +126,13 @@ func Stack() (c *StackController) {
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 {
@ -151,12 +165,12 @@ func Stack() (c *StackController) {
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 {
@ -172,6 +186,3 @@ func Stack() (c *StackController) {
}
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 {
@ -25,7 +31,7 @@ func Task() (c *TaskController) {
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 {
@ -40,6 +46,3 @@ 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 {
@ -45,7 +58,7 @@ func User() (c *UserController) {
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
@ -55,7 +68,7 @@ func User() (c *UserController) {
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 {
@ -65,7 +78,7 @@ func User() (c *UserController) {
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 {
@ -97,7 +110,7 @@ func User() (c *UserController) {
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 {
@ -120,7 +133,7 @@ func User() (c *UserController) {
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 {
@ -129,23 +142,20 @@ func User() (c *UserController) {
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)
@ -38,12 +49,12 @@ func Volume() (c *VolumeController) {
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 {
@ -56,7 +67,7 @@ func Volume() (c *VolumeController) {
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 {
@ -68,7 +79,7 @@ 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)
@ -76,7 +87,7 @@ func Volume() (c *VolumeController) {
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 {
@ -86,7 +97,7 @@ func Volume() (c *VolumeController) {
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 {
@ -101,6 +112,3 @@ func Volume() (c *VolumeController) {
m := newModel(ctx).Add("Volume", name).Add("Raw", j)
return ctx.Render("volume/raw", m)
}
return
}