From dfb74deb5bcbec8965d3f43a6dbca16b1d56224b Mon Sep 17 00:00:00 2001 From: cuigh Date: Thu, 23 Dec 2021 19:28:31 +0800 Subject: [PATCH] Refactor code --- api/chart.go | 8 +-- api/dashboard.go | 6 +-- api/event.go | 6 +-- api/registry.go | 4 +- api/role.go | 4 +- api/stack.go | 6 +-- api/system.go | 4 +- api/user.go | 6 +-- biz/biz.go | 10 ++-- biz/chart.go | 21 ++++---- biz/dashboard.go | 43 ++++++++------- biz/event.go | 7 ++- biz/registry.go | 19 ++++--- biz/role.go | 21 ++++---- biz/session.go | 13 +++-- biz/setting.go | 9 ++-- biz/stack.go | 29 +++++----- biz/user.go | 33 ++++++------ config/app.yml | 8 --- dao/bolt/bolt.go | 7 ++- dao/bolt/chart.go | 26 ++++----- dao/bolt/event.go | 8 +-- dao/bolt/registry.go | 20 +++---- dao/bolt/role.go | 16 +++--- dao/bolt/session.go | 16 +++--- dao/bolt/setting.go | 12 ++--- dao/bolt/stack.go | 16 +++--- dao/bolt/user.go | 32 +++++------ dao/dao.go | 96 +++++++++++++++++---------------- model/model.go => dao/entity.go | 6 +-- dao/mongo/chart.go | 24 ++++----- dao/mongo/event.go | 8 +-- dao/mongo/mongo.go | 7 ++- dao/mongo/registry.go | 18 +++---- dao/mongo/role.go | 14 ++--- dao/mongo/session.go | 10 ++-- dao/mongo/setting.go | 12 ++--- dao/mongo/stack.go | 14 ++--- dao/mongo/user.go | 24 ++++----- main.go | 5 +- security/auth.go | 18 +++---- ui/src/pages/event/List.vue | 44 ++++++++------- 42 files changed, 354 insertions(+), 356 deletions(-) rename model/model.go => dao/entity.go (98%) diff --git a/api/chart.go b/api/chart.go index bd8235a..949bcc2 100644 --- a/api/chart.go +++ b/api/chart.go @@ -4,7 +4,7 @@ import ( "github.com/cuigh/auxo/data" "github.com/cuigh/auxo/net/web" "github.com/cuigh/swirl/biz" - "github.com/cuigh/swirl/model" + "github.com/cuigh/swirl/dao" ) // ChartHandler encapsulates chart related handlers. @@ -28,8 +28,8 @@ func NewChart(b biz.ChartBiz) *ChartHandler { func chartSearch(b biz.ChartBiz) web.HandlerFunc { return func(ctx web.Context) (err error) { var ( - args = &model.ChartSearchArgs{} - charts []*model.Chart + args = &dao.ChartSearchArgs{} + charts []*dao.Chart total int ) @@ -75,7 +75,7 @@ func chartDelete(b biz.ChartBiz) web.HandlerFunc { func chartSave(b biz.ChartBiz) web.HandlerFunc { return func(ctx web.Context) error { - r := &model.Chart{} + r := &dao.Chart{} err := ctx.Bind(r, true) if err == nil { if r.ID == "" { diff --git a/api/dashboard.go b/api/dashboard.go index 564f569..f5554e2 100644 --- a/api/dashboard.go +++ b/api/dashboard.go @@ -8,7 +8,7 @@ import ( "github.com/cuigh/auxo/ext/times" "github.com/cuigh/auxo/net/web" "github.com/cuigh/swirl/biz" - "github.com/cuigh/swirl/model" + "github.com/cuigh/swirl/dao" ) // DashboardHandler encapsulates dashboard related handlers. @@ -30,7 +30,7 @@ func NewDashboard(b biz.DashboardBiz) *DashboardHandler { func dashboardFind(b biz.DashboardBiz) web.HandlerFunc { return func(ctx web.Context) (err error) { var ( - d *model.Dashboard + d *dao.Dashboard name = ctx.Query("name") key = ctx.Query("key") ) @@ -44,7 +44,7 @@ func dashboardFind(b biz.DashboardBiz) web.HandlerFunc { func dashboardSave(b biz.DashboardBiz) web.HandlerFunc { return func(ctx web.Context) error { - dashboard := &model.Dashboard{} + dashboard := &dao.Dashboard{} err := ctx.Bind(dashboard) if err != nil { return err diff --git a/api/event.go b/api/event.go index 67c49bc..6eb3490 100644 --- a/api/event.go +++ b/api/event.go @@ -5,7 +5,7 @@ import ( "github.com/cuigh/auxo/errors" "github.com/cuigh/auxo/net/web" "github.com/cuigh/swirl/biz" - "github.com/cuigh/swirl/model" + "github.com/cuigh/swirl/dao" ) // EventHandler encapsulates event related handlers. @@ -25,8 +25,8 @@ func NewEvent(b biz.EventBiz) *EventHandler { func eventSearch(b biz.EventBiz) web.HandlerFunc { return func(ctx web.Context) (err error) { var ( - args = &model.EventSearchArgs{} - events []*model.Event + args = &dao.EventSearchArgs{} + events []*dao.Event total int ) diff --git a/api/registry.go b/api/registry.go index 5d06b74..b3c050a 100644 --- a/api/registry.go +++ b/api/registry.go @@ -3,7 +3,7 @@ package api import ( "github.com/cuigh/auxo/net/web" "github.com/cuigh/swirl/biz" - "github.com/cuigh/swirl/model" + "github.com/cuigh/swirl/dao" ) // RegistryHandler encapsulates registry related handlers. @@ -61,7 +61,7 @@ func registryDelete(b biz.RegistryBiz) web.HandlerFunc { func registrySave(b biz.RegistryBiz) web.HandlerFunc { return func(ctx web.Context) error { - r := &model.Registry{} + r := &dao.Registry{} err := ctx.Bind(r, true) if err == nil { if r.ID == "" { diff --git a/api/role.go b/api/role.go index b4f86ff..ba0ff9d 100644 --- a/api/role.go +++ b/api/role.go @@ -3,7 +3,7 @@ package api import ( "github.com/cuigh/auxo/net/web" "github.com/cuigh/swirl/biz" - "github.com/cuigh/swirl/model" + "github.com/cuigh/swirl/dao" ) // RoleHandler encapsulates role related handlers. @@ -62,7 +62,7 @@ func roleDelete(b biz.RoleBiz) web.HandlerFunc { func roleSave(b biz.RoleBiz) web.HandlerFunc { return func(ctx web.Context) error { - r := &model.Role{} + r := &dao.Role{} err := ctx.Bind(r, true) if err == nil { if r.ID == "" { diff --git a/api/stack.go b/api/stack.go index a28e2ff..791a773 100644 --- a/api/stack.go +++ b/api/stack.go @@ -5,8 +5,8 @@ import ( "github.com/cuigh/auxo/net/web" "github.com/cuigh/swirl/biz" + "github.com/cuigh/swirl/dao" "github.com/cuigh/swirl/docker/compose" - "github.com/cuigh/swirl/model" ) // StackHandler encapsulates stack related handlers. @@ -54,7 +54,7 @@ func stackSearch(b biz.StackBiz) web.HandlerFunc { return func(ctx web.Context) (err error) { var ( args = &Args{} - stacks []*model.Stack + stacks []*dao.Stack ) if err = ctx.Bind(args); err == nil { @@ -122,7 +122,7 @@ func stackDeploy(b biz.StackBiz) web.HandlerFunc { func stackSave(b biz.StackBiz) web.HandlerFunc { type Args struct { ID string `json:"id"` - model.Stack + dao.Stack } return func(ctx web.Context) error { diff --git a/api/system.go b/api/system.go index 6f38521..984701f 100644 --- a/api/system.go +++ b/api/system.go @@ -9,9 +9,9 @@ import ( "github.com/cuigh/auxo/errors" "github.com/cuigh/auxo/net/web" "github.com/cuigh/swirl/biz" + "github.com/cuigh/swirl/dao" "github.com/cuigh/swirl/docker" "github.com/cuigh/swirl/misc" - "github.com/cuigh/swirl/model" ) // SystemHandler encapsulates system related handlers. @@ -77,7 +77,7 @@ func systemSummarize(d *docker.Docker) web.HandlerFunc { func systemCreateAdmin(ub biz.UserBiz) web.HandlerFunc { return func(c web.Context) (err error) { - user := &model.User{} + user := &dao.User{} if err = c.Bind(user, true); err != nil { return err } diff --git a/api/user.go b/api/user.go index fc2a298..02b2208 100644 --- a/api/user.go +++ b/api/user.go @@ -4,7 +4,7 @@ import ( "github.com/cuigh/auxo/data" "github.com/cuigh/auxo/net/web" "github.com/cuigh/swirl/biz" - "github.com/cuigh/swirl/model" + "github.com/cuigh/swirl/dao" "github.com/cuigh/swirl/security" ) @@ -66,7 +66,7 @@ func userSignIn(auth *security.Identifier, eb biz.EventBiz) web.HandlerFunc { func userSave(b biz.UserBiz) web.HandlerFunc { return func(ctx web.Context) error { - user := &model.User{} + user := &dao.User{} err := ctx.Bind(user, true) if err == nil { if user.ID == "" { @@ -167,7 +167,7 @@ func userModifyPassword(b biz.UserBiz) web.HandlerFunc { func userModifyProfile(b biz.UserBiz) web.HandlerFunc { return func(ctx web.Context) error { - u := &model.User{} + u := &dao.User{} err := ctx.Bind(u, true) if err == nil { err = b.ModifyProfile(u, ctx.User()) diff --git a/biz/biz.go b/biz/biz.go index d92d14a..c4c33ba 100644 --- a/biz/biz.go +++ b/biz/biz.go @@ -12,7 +12,7 @@ import ( "github.com/cuigh/auxo/app/container" "github.com/cuigh/auxo/data" "github.com/cuigh/auxo/net/web" - "github.com/cuigh/swirl/model" + "github.com/cuigh/swirl/dao" "go.mongodb.org/mongo-driver/bson/primitive" ) @@ -103,12 +103,12 @@ func indentJSON(raw []byte) (s string, err error) { return } -func now() model.Time { - return model.Time(time.Now()) +func now() dao.Time { + return dao.Time(time.Now()) } -func newOperator(user web.User) model.Operator { - return model.Operator{ID: user.ID(), Name: user.Name()} +func newOperator(user web.User) dao.Operator { + return dao.Operator{ID: user.ID(), Name: user.Name()} } func init() { diff --git a/biz/chart.go b/biz/chart.go index fff959e..c789827 100644 --- a/biz/chart.go +++ b/biz/chart.go @@ -5,16 +5,15 @@ import ( "github.com/cuigh/auxo/net/web" "github.com/cuigh/swirl/dao" - "github.com/cuigh/swirl/model" ) type ChartBiz interface { - Search(args *model.ChartSearchArgs) (charts []*model.Chart, total int, err error) + Search(args *dao.ChartSearchArgs) (charts []*dao.Chart, total int, err error) Delete(id, title string, user web.User) (err error) - Find(id string) (chart *model.Chart, err error) - Batch(ids ...string) (charts []*model.Chart, err error) - Create(chart *model.Chart, user web.User) (err error) - Update(chart *model.Chart, user web.User) (err error) + Find(id string) (chart *dao.Chart, err error) + Batch(ids ...string) (charts []*dao.Chart, err error) + Create(chart *dao.Chart, user web.User) (err error) + Update(chart *dao.Chart, user web.User) (err error) } func NewChart(d dao.Interface, mb MetricBiz, eb EventBiz) ChartBiz { @@ -31,11 +30,11 @@ type chartBiz struct { eb EventBiz } -func (b *chartBiz) Search(args *model.ChartSearchArgs) (charts []*model.Chart, total int, err error) { +func (b *chartBiz) Search(args *dao.ChartSearchArgs) (charts []*dao.Chart, total int, err error) { return b.d.ChartSearch(context.TODO(), args) } -func (b *chartBiz) Create(chart *model.Chart, user web.User) (err error) { +func (b *chartBiz) Create(chart *dao.Chart, user web.User) (err error) { chart.ID = createId() chart.CreatedAt = now() chart.CreatedBy = newOperator(user) @@ -56,16 +55,16 @@ func (b *chartBiz) Delete(id, title string, user web.User) (err error) { return } -func (b *chartBiz) Find(id string) (chart *model.Chart, err error) { +func (b *chartBiz) Find(id string) (chart *dao.Chart, err error) { return b.d.ChartGet(context.TODO(), id) } -func (b *chartBiz) Batch(ids ...string) (charts []*model.Chart, err error) { +func (b *chartBiz) Batch(ids ...string) (charts []*dao.Chart, err error) { charts, err = b.d.ChartGetBatch(context.TODO(), ids...) return } -func (b *chartBiz) Update(chart *model.Chart, user web.User) (err error) { +func (b *chartBiz) Update(chart *dao.Chart, user web.User) (err error) { chart.UpdatedAt = now() chart.UpdatedBy = newOperator(user) err = b.d.ChartUpdate(context.TODO(), chart) diff --git a/biz/dashboard.go b/biz/dashboard.go index b1310a2..8368bb9 100644 --- a/biz/dashboard.go +++ b/biz/dashboard.go @@ -10,21 +10,20 @@ import ( "github.com/cuigh/auxo/log" "github.com/cuigh/auxo/net/web" "github.com/cuigh/swirl/dao" - "github.com/cuigh/swirl/model" "github.com/jinzhu/copier" ) -var builtins = []*model.Chart{ - model.NewChart("service", "$cpu", "CPU", "${name}", `rate(container_cpu_user_seconds_total{container_label_com_docker_swarm_service_name="${service}"}[5m]) * 100`, "percent:100", 60), - model.NewChart("service", "$memory", "Memory", "${name}", `container_memory_usage_bytes{container_label_com_docker_swarm_service_name="${service}"}`, "size:bytes", 60), - model.NewChart("service", "$network_in", "Network Receive", "${name}", `sum(irate(container_network_receive_bytes_total{container_label_com_docker_swarm_service_name="${service}"}[5m])) by(name)`, "size:bytes", 60), - model.NewChart("service", "$network_out", "Network Send", "${name}", `sum(irate(container_network_transmit_bytes_total{container_label_com_docker_swarm_service_name="${service}"}[5m])) by(name)`, "size:bytes", 60), +var builtins = []*dao.Chart{ + dao.NewChart("service", "$cpu", "CPU", "${name}", `rate(container_cpu_user_seconds_total{container_label_com_docker_swarm_service_name="${service}"}[5m]) * 100`, "percent:100", 60), + dao.NewChart("service", "$memory", "Memory", "${name}", `container_memory_usage_bytes{container_label_com_docker_swarm_service_name="${service}"}`, "size:bytes", 60), + dao.NewChart("service", "$network_in", "Network Receive", "${name}", `sum(irate(container_network_receive_bytes_total{container_label_com_docker_swarm_service_name="${service}"}[5m])) by(name)`, "size:bytes", 60), + dao.NewChart("service", "$network_out", "Network Send", "${name}", `sum(irate(container_network_transmit_bytes_total{container_label_com_docker_swarm_service_name="${service}"}[5m])) by(name)`, "size:bytes", 60), } type DashboardBiz interface { FetchData(key string, ids []string, period time.Duration) (data.Map, error) - FindDashboard(name, key string) (dashboard *model.Dashboard, err error) - UpdateDashboard(dashboard *model.Dashboard, user web.User) (err error) + FindDashboard(name, key string) (dashboard *dao.Dashboard, err error) + UpdateDashboard(dashboard *dao.Dashboard, user web.User) (err error) } func NewDashboard(d dao.Interface, mb MetricBiz, eb EventBiz) DashboardBiz { @@ -42,7 +41,7 @@ type dashboardBiz struct { eb EventBiz } -func (b *dashboardBiz) FindDashboard(name, key string) (dashboard *model.Dashboard, err error) { +func (b *dashboardBiz) FindDashboard(name, key string) (dashboard *dao.Dashboard, err error) { if dashboard, err = b.d.DashboardGet(context.TODO(), name, key); err != nil { return } @@ -53,7 +52,7 @@ func (b *dashboardBiz) FindDashboard(name, key string) (dashboard *model.Dashboa return } -func (b *dashboardBiz) UpdateDashboard(dashboard *model.Dashboard, user web.User) (err error) { +func (b *dashboardBiz) UpdateDashboard(dashboard *dao.Dashboard, user web.User) (err error) { dashboard.UpdatedAt = now() dashboard.UpdatedBy = newOperator(user) return b.d.DashboardUpdate(context.TODO(), dashboard) @@ -79,7 +78,7 @@ func (b *dashboardBiz) FetchData(key string, ids []string, period time.Duration) end := time.Now() start := end.Add(-period) for _, chart := range charts { - go func(c *model.Chart) { + go func(c *dao.Chart) { d := Data{id: c.ID} switch c.Type { case "line", "bar": @@ -108,7 +107,7 @@ func (b *dashboardBiz) FetchData(key string, ids []string, period time.Duration) return ds, nil } -func (b *dashboardBiz) fetchMatrixData(chart *model.Chart, key string, start, end time.Time) (md *MatrixData, err error) { +func (b *dashboardBiz) fetchMatrixData(chart *dao.Chart, key string, start, end time.Time) (md *MatrixData, err error) { var ( q string d *MatrixData @@ -131,7 +130,7 @@ func (b *dashboardBiz) fetchMatrixData(chart *model.Chart, key string, start, en return md, nil } -func (b *dashboardBiz) fetchVectorData(chart *model.Chart, key string, end time.Time) (cvd *VectorData, err error) { +func (b *dashboardBiz) fetchVectorData(chart *dao.Chart, key string, end time.Time) (cvd *VectorData, err error) { var ( q string d *VectorData @@ -154,7 +153,7 @@ func (b *dashboardBiz) fetchVectorData(chart *model.Chart, key string, end time. return cvd, nil } -func (b *dashboardBiz) fetchScalarData(chart *model.Chart, key string, end time.Time) (*VectorValue, error) { +func (b *dashboardBiz) fetchScalarData(chart *dao.Chart, key string, end time.Time) (*VectorValue, error) { query, err := b.formatQuery(chart.Metrics[0].Query, chart.Dashboard, key) if err != nil { return nil, err @@ -191,13 +190,13 @@ func (b *dashboardBiz) formatQuery(query, dashboard, key string) (string, error) return "", errs[0] } -func (b *dashboardBiz) getCharts(ids []string) (charts map[string]*model.Chart, err error) { +func (b *dashboardBiz) getCharts(ids []string) (charts map[string]*dao.Chart, err error) { var ( customIds []string - customCharts []*model.Chart + customCharts []*dao.Chart ) - charts = make(map[string]*model.Chart) + charts = make(map[string]*dao.Chart) for _, id := range ids { if id[0] == '$' { for _, c := range builtins { @@ -220,13 +219,13 @@ func (b *dashboardBiz) getCharts(ids []string) (charts map[string]*model.Chart, return } -func (b *dashboardBiz) fillCharts(d *model.Dashboard) (err error) { +func (b *dashboardBiz) fillCharts(d *dao.Dashboard) (err error) { if len(d.Charts) == 0 { return } var ( - m map[string]*model.Chart + m map[string]*dao.Chart ids = make([]string, len(d.Charts)) ) @@ -247,15 +246,15 @@ func (b *dashboardBiz) fillCharts(d *model.Dashboard) (err error) { return nil } -func (b *dashboardBiz) defaultDashboard(name, key string) *model.Dashboard { - d := &model.Dashboard{ +func (b *dashboardBiz) defaultDashboard(name, key string) *dao.Dashboard { + d := &dao.Dashboard{ Name: name, Key: key, Period: 30, Interval: 15, } if name == "service" { - d.Charts = []model.ChartInfo{ + d.Charts = []dao.ChartInfo{ {ID: "$cpu"}, {ID: "$memory"}, {ID: "$network_in"}, diff --git a/biz/event.go b/biz/event.go index 837cef1..61cdaef 100644 --- a/biz/event.go +++ b/biz/event.go @@ -6,7 +6,6 @@ import ( "github.com/cuigh/auxo/log" "github.com/cuigh/auxo/net/web" "github.com/cuigh/swirl/dao" - "github.com/cuigh/swirl/model" "go.mongodb.org/mongo-driver/bson/primitive" ) @@ -47,7 +46,7 @@ const ( ) type EventBiz interface { - Search(args *model.EventSearchArgs) (events []*model.Event, total int, err error) + Search(args *dao.EventSearchArgs) (events []*dao.Event, total int, err error) CreateRegistry(action EventAction, id, name string, user web.User) CreateNode(action EventAction, id, name string, user web.User) CreateNetwork(action EventAction, id, name string, user web.User) @@ -73,12 +72,12 @@ type eventBiz struct { d dao.Interface } -func (b *eventBiz) Search(args *model.EventSearchArgs) (events []*model.Event, total int, err error) { +func (b *eventBiz) Search(args *dao.EventSearchArgs) (events []*dao.Event, total int, err error) { return b.d.EventSearch(context.TODO(), args) } func (b *eventBiz) create(et EventType, ea EventAction, code, name string, user web.User) { - event := &model.Event{ + event := &dao.Event{ ID: primitive.NewObjectID(), Type: string(et), Action: string(ea), diff --git a/biz/registry.go b/biz/registry.go index 24341ce..a89043f 100644 --- a/biz/registry.go +++ b/biz/registry.go @@ -7,17 +7,16 @@ import ( "github.com/cuigh/auxo/net/web" "github.com/cuigh/swirl/dao" - "github.com/cuigh/swirl/model" "github.com/docker/docker/api/types" ) type RegistryBiz interface { - Search() ([]*model.Registry, error) - Find(id string) (*model.Registry, error) + Search() ([]*dao.Registry, error) + Find(id string) (*dao.Registry, error) GetAuth(url string) (auth string, err error) Delete(id, name string, user web.User) (err error) - Create(registry *model.Registry, user web.User) (err error) - Update(registry *model.Registry, user web.User) (err error) + Create(registry *dao.Registry, user web.User) (err error) + Update(registry *dao.Registry, user web.User) (err error) } func NewRegistry(d dao.Interface, eb EventBiz) RegistryBiz { @@ -29,7 +28,7 @@ type registryBiz struct { eb EventBiz } -func (b *registryBiz) Create(r *model.Registry, user web.User) (err error) { +func (b *registryBiz) Create(r *dao.Registry, user web.User) (err error) { r.ID = createId() r.CreatedAt = now() r.UpdatedAt = r.CreatedAt @@ -43,7 +42,7 @@ func (b *registryBiz) Create(r *model.Registry, user web.User) (err error) { return } -func (b *registryBiz) Update(r *model.Registry, user web.User) (err error) { +func (b *registryBiz) Update(r *dao.Registry, user web.User) (err error) { r.UpdatedAt = now() r.UpdatedBy = newOperator(user) err = b.d.RegistryUpdate(context.TODO(), r) @@ -53,7 +52,7 @@ func (b *registryBiz) Update(r *model.Registry, user web.User) (err error) { return } -func (b *registryBiz) Search() (registries []*model.Registry, err error) { +func (b *registryBiz) Search() (registries []*dao.Registry, err error) { registries, err = b.d.RegistryGetAll(context.TODO()) if err == nil { for _, r := range registries { @@ -63,7 +62,7 @@ func (b *registryBiz) Search() (registries []*model.Registry, err error) { return } -func (b *registryBiz) Find(id string) (registry *model.Registry, err error) { +func (b *registryBiz) Find(id string) (registry *dao.Registry, err error) { registry, err = b.d.RegistryGet(context.TODO(), id) if err == nil { registry.Password = "" @@ -73,7 +72,7 @@ func (b *registryBiz) Find(id string) (registry *model.Registry, err error) { func (b *registryBiz) GetAuth(url string) (auth string, err error) { var ( - r *model.Registry + r *dao.Registry buf []byte ) if r, err = b.d.RegistryGetByURL(context.TODO(), url); err == nil && r != nil { diff --git a/biz/role.go b/biz/role.go index f3e3f67..d94d7b3 100644 --- a/biz/role.go +++ b/biz/role.go @@ -5,15 +5,14 @@ import ( "github.com/cuigh/auxo/net/web" "github.com/cuigh/swirl/dao" - "github.com/cuigh/swirl/model" ) type RoleBiz interface { - Search(name string) ([]*model.Role, error) - Find(id string) (role *model.Role, err error) - Create(role *model.Role, user web.User) (err error) + Search(name string) ([]*dao.Role, error) + Find(id string) (role *dao.Role, err error) + Create(role *dao.Role, user web.User) (err error) Delete(id, name string, user web.User) (err error) - Update(r *model.Role, user web.User) (err error) + Update(r *dao.Role, user web.User) (err error) GetPerms(ids []string) ([]string, error) } @@ -26,12 +25,12 @@ type roleBiz struct { eb EventBiz } -func (b *roleBiz) Search(name string) (roles []*model.Role, err error) { +func (b *roleBiz) Search(name string) (roles []*dao.Role, err error) { return b.d.RoleSearch(context.TODO(), name) } -func (b *roleBiz) Create(role *model.Role, user web.User) (err error) { - r := &model.Role{ +func (b *roleBiz) Create(role *dao.Role, user web.User) (err error) { + r := &dao.Role{ ID: createId(), Name: role.Name, Description: role.Description, @@ -56,12 +55,12 @@ func (b *roleBiz) Delete(id, name string, user web.User) (err error) { return } -func (b *roleBiz) Find(id string) (role *model.Role, err error) { +func (b *roleBiz) Find(id string) (role *dao.Role, err error) { return b.d.RoleGet(context.TODO(), id) } -func (b *roleBiz) Update(role *model.Role, user web.User) (err error) { - r := &model.Role{ +func (b *roleBiz) Update(role *dao.Role, user web.User) (err error) { + r := &dao.Role{ ID: role.ID, Name: role.Name, Description: role.Description, diff --git a/biz/session.go b/biz/session.go index 6f60bc2..8486d96 100644 --- a/biz/session.go +++ b/biz/session.go @@ -5,13 +5,12 @@ import ( "time" "github.com/cuigh/swirl/dao" - "github.com/cuigh/swirl/model" ) type SessionBiz interface { - Find(token string) (session *model.Session, err error) - Create(session *model.Session) (err error) - Update(session *model.Session) (err error) + Find(token string) (session *dao.Session, err error) + Create(session *dao.Session) (err error) + Update(session *dao.Session) (err error) UpdateExpiry(id string, expiry time.Time) (err error) } @@ -24,17 +23,17 @@ type sessionBiz struct { rb RoleBiz } -func (b *sessionBiz) Find(token string) (session *model.Session, err error) { +func (b *sessionBiz) Find(token string) (session *dao.Session, err error) { return b.d.SessionGet(context.TODO(), token) } -func (b *sessionBiz) Create(session *model.Session) (err error) { +func (b *sessionBiz) Create(session *dao.Session) (err error) { session.CreatedAt = time.Now() session.UpdatedAt = session.CreatedAt return b.d.SessionCreate(context.TODO(), session) } -func (b *sessionBiz) Update(session *model.Session) (err error) { +func (b *sessionBiz) Update(session *dao.Session) (err error) { session.Dirty = false session.UpdatedAt = time.Now() return b.d.SessionUpdate(context.TODO(), session) diff --git a/biz/setting.go b/biz/setting.go index 768e236..5ef95fe 100644 --- a/biz/setting.go +++ b/biz/setting.go @@ -9,7 +9,6 @@ import ( "github.com/cuigh/auxo/data" "github.com/cuigh/auxo/net/web" "github.com/cuigh/swirl/dao" - "github.com/cuigh/swirl/model" ) type SettingBiz interface { @@ -28,7 +27,7 @@ type settingBiz struct { } func (b *settingBiz) Find(id string) (options interface{}, err error) { - var setting *model.Setting + var setting *dao.Setting setting, err = b.d.SettingGet(context.TODO(), id) if err == nil && setting != nil { return b.unmarshal(setting.Options) @@ -38,7 +37,7 @@ func (b *settingBiz) Find(id string) (options interface{}, err error) { // Load returns settings of swirl. If not found, default settings will be returned. func (b *settingBiz) Load() (options data.Map, err error) { - var settings []*model.Setting + var settings []*dao.Setting settings, err = b.d.SettingGetAll(context.TODO()) if err != nil { return @@ -56,12 +55,12 @@ func (b *settingBiz) Load() (options data.Map, err error) { } func (b *settingBiz) Save(id string, options interface{}, user web.User) (err error) { - setting := &model.Setting{ + setting := &dao.Setting{ ID: id, UpdatedAt: time.Now(), } if user != nil { - setting.UpdatedBy = model.Operator{ID: user.ID(), Name: user.Name()} + setting.UpdatedBy = dao.Operator{ID: user.ID(), Name: user.Name()} } setting.Options, err = b.marshal(options) diff --git a/biz/stack.go b/biz/stack.go index d4fbac8..5316bad 100644 --- a/biz/stack.go +++ b/biz/stack.go @@ -10,17 +10,16 @@ import ( "github.com/cuigh/swirl/docker" "github.com/cuigh/swirl/docker/compose" "github.com/cuigh/swirl/misc" - "github.com/cuigh/swirl/model" ) type StackBiz interface { - Search(name, filter string) (stacks []*model.Stack, err error) - Find(name string) (stack *model.Stack, err error) + Search(name, filter string) (stacks []*dao.Stack, err error) + Find(name string) (stack *dao.Stack, err error) Delete(name string, user web.User) (err error) Shutdown(name string, user web.User) (err error) Deploy(name string, user web.User) (err error) - Create(s *model.Stack, user web.User) (err error) - Update(s *model.Stack, user web.User) (err error) + Create(s *dao.Stack, user web.User) (err error) + Update(s *dao.Stack, user web.User) (err error) } func NewStack(d *docker.Docker, s dao.Interface, eb EventBiz) StackBiz { @@ -33,10 +32,10 @@ type stackBiz struct { eb EventBiz } -func (b *stackBiz) Search(name, filter string) (stacks []*model.Stack, err error) { +func (b *stackBiz) Search(name, filter string) (stacks []*dao.Stack, err error) { var ( activeStacks map[string][]string - internalStacks []*model.Stack + internalStacks []*dao.Stack ) // load real stacks @@ -63,7 +62,7 @@ func (b *stackBiz) Search(name, filter string) (stacks []*model.Stack, err error } } for n, services := range activeStacks { - stack := &model.Stack{Name: n, Services: services} + stack := &dao.Stack{Name: n, Services: services} if !b.filter(stack, name, filter) { stacks = append(stacks, stack) } @@ -71,17 +70,17 @@ func (b *stackBiz) Search(name, filter string) (stacks []*model.Stack, err error return } -func (b *stackBiz) Find(name string) (s *model.Stack, err error) { +func (b *stackBiz) Find(name string) (s *dao.Stack, err error) { s, err = b.s.StackGet(context.TODO(), name) if err != nil { return nil, err } else if s == nil { - s = &model.Stack{Name: name} + s = &dao.Stack{Name: name} } return } -func (b *stackBiz) filter(stack *model.Stack, name, filter string) bool { +func (b *stackBiz) filter(stack *dao.Stack, name, filter string) bool { if name != "" { if !strings.Contains(strings.ToLower(stack.Name), strings.ToLower(name)) { return true @@ -106,8 +105,8 @@ func (b *stackBiz) filter(stack *model.Stack, name, filter string) bool { return false } -func (b *stackBiz) Create(s *model.Stack, user web.User) (err error) { - stack := &model.Stack{ +func (b *stackBiz) Create(s *dao.Stack, user web.User) (err error) { + stack := &dao.Stack{ Name: s.Name, Content: s.Content, CreatedAt: now(), @@ -122,8 +121,8 @@ func (b *stackBiz) Create(s *model.Stack, user web.User) (err error) { return } -func (b *stackBiz) Update(s *model.Stack, user web.User) (err error) { - stack := &model.Stack{ +func (b *stackBiz) Update(s *dao.Stack, user web.User) (err error) { + stack := &dao.Stack{ Name: s.Name, Content: s.Content, UpdatedAt: now(), diff --git a/biz/user.go b/biz/user.go index a1e084f..20b85e1 100644 --- a/biz/user.go +++ b/biz/user.go @@ -8,7 +8,6 @@ import ( "github.com/cuigh/auxo/security/passwd" "github.com/cuigh/swirl/dao" "github.com/cuigh/swirl/misc" - "github.com/cuigh/swirl/model" ) const ( @@ -26,17 +25,17 @@ const ( ) type UserBiz interface { - Search(name, loginName, filter string, pageIndex, pageSize int) (users []*model.User, total int, err error) - Create(user *model.User, ctxUser web.User) (id string, err error) - Update(user *model.User, ctxUser web.User) (err error) - FindByID(id string) (user *model.User, err error) - FindByName(loginName string) (user *model.User, err error) + Search(name, loginName, filter string, pageIndex, pageSize int) (users []*dao.User, total int, err error) + Create(user *dao.User, ctxUser web.User) (id string, err error) + Update(user *dao.User, ctxUser web.User) (err error) + FindByID(id string) (user *dao.User, err error) + FindByName(loginName string) (user *dao.User, err error) FindPrivacy(loginName string) (privacy *UserPrivacy, err error) Count() (count int, err error) Delete(id, name string, user web.User) (err error) SetStatus(id string, status int32, user web.User) (err error) ModifyPassword(oldPwd, newPwd string, user web.User) (err error) - ModifyProfile(user *model.User, ctxUser web.User) (err error) + ModifyProfile(user *dao.User, ctxUser web.User) (err error) } func NewUser(d dao.Interface, eb EventBiz) UserBiz { @@ -48,8 +47,8 @@ type userBiz struct { eb EventBiz } -func (b *userBiz) Search(name, loginName, filter string, pageIndex, pageSize int) (users []*model.User, total int, err error) { - var args = &model.UserSearchArgs{ +func (b *userBiz) Search(name, loginName, filter string, pageIndex, pageSize int) (users []*dao.User, total int, err error) { + var args = &dao.UserSearchArgs{ Name: name, LoginName: loginName, Status: -1, @@ -69,16 +68,16 @@ func (b *userBiz) Search(name, loginName, filter string, pageIndex, pageSize int return b.d.UserSearch(context.TODO(), args) } -func (b *userBiz) FindByID(id string) (user *model.User, err error) { +func (b *userBiz) FindByID(id string) (user *dao.User, err error) { return b.d.UserGet(context.TODO(), id) } -func (b *userBiz) FindByName(loginName string) (user *model.User, err error) { +func (b *userBiz) FindByName(loginName string) (user *dao.User, err error) { return b.d.UserGetByName(context.TODO(), loginName) } func (b *userBiz) FindPrivacy(loginName string) (privacy *UserPrivacy, err error) { - var u *model.User + var u *dao.User u, err = b.d.UserGetByName(context.TODO(), loginName) if u != nil { privacy = &UserPrivacy{ @@ -93,7 +92,7 @@ func (b *userBiz) FindPrivacy(loginName string) (privacy *UserPrivacy, err error return } -func (b *userBiz) Create(user *model.User, ctxUser web.User) (id string, err error) { +func (b *userBiz) Create(user *dao.User, ctxUser web.User) (id string, err error) { user.ID = createId() user.Status = UserStatusActive user.CreatedAt = now() @@ -116,7 +115,7 @@ func (b *userBiz) Create(user *model.User, ctxUser web.User) (id string, err err return } -func (b *userBiz) Update(user *model.User, ctxUser web.User) (err error) { +func (b *userBiz) Update(user *dao.User, ctxUser web.User) (err error) { user.UpdatedAt = now() user.UpdatedBy = newOperator(ctxUser) if err = b.d.UserUpdate(context.TODO(), user); err == nil { @@ -126,7 +125,7 @@ func (b *userBiz) Update(user *model.User, ctxUser web.User) (err error) { } func (b *userBiz) SetStatus(id string, status int32, user web.User) (err error) { - u := &model.User{ + u := &dao.User{ ID: id, Status: status, UpdatedAt: now(), @@ -144,7 +143,7 @@ func (b *userBiz) Delete(id, name string, user web.User) (err error) { } func (b *userBiz) ModifyPassword(oldPwd, newPwd string, user web.User) (err error) { - var u *model.User + var u *dao.User u, err = b.d.UserGet(context.TODO(), user.ID()) if err != nil { return err @@ -165,7 +164,7 @@ func (b *userBiz) ModifyPassword(oldPwd, newPwd string, user web.User) (err erro return b.d.UserUpdatePassword(context.TODO(), u) } -func (b *userBiz) ModifyProfile(u *model.User, user web.User) (err error) { +func (b *userBiz) ModifyProfile(u *dao.User, user web.User) (err error) { u.ID = user.ID() u.UpdatedAt = now() u.UpdatedBy = newOperator(user) diff --git a/config/app.yml b/config/app.yml index 754262c..c3d96ab 100644 --- a/config/app.yml +++ b/config/app.yml @@ -22,11 +22,3 @@ log: - name: console type: console layout: '[{L}]{T}: {M}{N}' - -cache: -- provider: memory - enabled: true - error: log - time: 10m - keys: -# auth_user: 3m diff --git a/dao/bolt/bolt.go b/dao/bolt/bolt.go index f143f72..c73d4c9 100644 --- a/dao/bolt/bolt.go +++ b/dao/bolt/bolt.go @@ -9,6 +9,7 @@ import ( "github.com/cuigh/auxo/errors" "github.com/cuigh/auxo/log" "github.com/cuigh/auxo/util/run" + "github.com/cuigh/swirl/dao" "go.mongodb.org/mongo-driver/bson" ) @@ -29,7 +30,7 @@ type Dao struct { } // New creates a Dao instance. -func New(addr string) (*Dao, error) { +func New(addr string) (dao.Interface, error) { if addr == "" { addr = "/data/swirl" } @@ -171,3 +172,7 @@ func matchAny(s string, list ...string) bool { } return false } + +func init() { + dao.Register("bolt", New) +} diff --git a/dao/bolt/chart.go b/dao/bolt/chart.go index 122070b..076b241 100644 --- a/dao/bolt/chart.go +++ b/dao/bolt/chart.go @@ -5,8 +5,8 @@ import ( "sort" "time" + "github.com/cuigh/swirl/dao" "github.com/cuigh/swirl/misc" - "github.com/cuigh/swirl/model" ) const ( @@ -14,9 +14,9 @@ const ( Dashboard = "dashboard" ) -func (d *Dao) ChartSearch(ctx context.Context, args *model.ChartSearchArgs) (charts []*model.Chart, count int, err error) { +func (d *Dao) ChartSearch(ctx context.Context, args *dao.ChartSearchArgs) (charts []*dao.Chart, count int, err error) { err = d.each(Chart, func(v []byte) error { - chart := &model.Chart{} + chart := &dao.Chart{} if err = decode(v, chart); err == nil { match := true if args.Title != "" { @@ -42,12 +42,12 @@ func (d *Dao) ChartSearch(ctx context.Context, args *model.ChartSearchArgs) (cha return } -func (d *Dao) ChartCreate(ctx context.Context, chart *model.Chart) (err error) { +func (d *Dao) ChartCreate(ctx context.Context, chart *dao.Chart) (err error) { return d.replace(Chart, chart.ID, chart) } -func (d *Dao) ChartGet(ctx context.Context, name string) (chart *model.Chart, err error) { - chart = &model.Chart{} +func (d *Dao) ChartGet(ctx context.Context, name string) (chart *dao.Chart, err error) { + chart = &dao.Chart{} err = d.get(Chart, name, chart) if err == ErrNoRecords { return nil, nil @@ -57,9 +57,9 @@ func (d *Dao) ChartGet(ctx context.Context, name string) (chart *model.Chart, er return } -func (d *Dao) ChartGetBatch(ctx context.Context, ids ...string) (charts []*model.Chart, err error) { +func (d *Dao) ChartGetBatch(ctx context.Context, ids ...string) (charts []*dao.Chart, err error) { err = d.slice(Chart, func(v []byte) error { - chart := &model.Chart{} + chart := &dao.Chart{} if err = decode(v, chart); err == nil { charts = append(charts, chart) } @@ -68,8 +68,8 @@ func (d *Dao) ChartGetBatch(ctx context.Context, ids ...string) (charts []*model return } -func (d *Dao) ChartUpdate(ctx context.Context, chart *model.Chart) (err error) { - old := &model.Chart{} +func (d *Dao) ChartUpdate(ctx context.Context, chart *dao.Chart) (err error) { + old := &dao.Chart{} return d.update(Chart, chart.ID, old, func() interface{} { chart.CreatedAt = old.CreatedAt chart.CreatedBy = old.CreatedBy @@ -81,8 +81,8 @@ func (d *Dao) ChartDelete(ctx context.Context, name string) (err error) { return d.delete(Chart, name) } -func (d *Dao) DashboardGet(ctx context.Context, name, key string) (dashboard *model.Dashboard, err error) { - dashboard = &model.Dashboard{ +func (d *Dao) DashboardGet(ctx context.Context, name, key string) (dashboard *dao.Dashboard, err error) { + dashboard = &dao.Dashboard{ Name: name, Key: key, } @@ -95,6 +95,6 @@ func (d *Dao) DashboardGet(ctx context.Context, name, key string) (dashboard *mo return } -func (d *Dao) DashboardUpdate(ctx context.Context, dashboard *model.Dashboard) (err error) { +func (d *Dao) DashboardUpdate(ctx context.Context, dashboard *dao.Dashboard) (err error) { return d.replace(Dashboard, dashboard.ID(), dashboard) } diff --git a/dao/bolt/event.go b/dao/bolt/event.go index 8255aa3..409ca36 100644 --- a/dao/bolt/event.go +++ b/dao/bolt/event.go @@ -5,15 +5,15 @@ import ( "sort" "time" + "github.com/cuigh/swirl/dao" "github.com/cuigh/swirl/misc" - "github.com/cuigh/swirl/model" ) const Event = "event" -func (d *Dao) EventSearch(ctx context.Context, args *model.EventSearchArgs) (events []*model.Event, count int, err error) { +func (d *Dao) EventSearch(ctx context.Context, args *dao.EventSearchArgs) (events []*dao.Event, count int, err error) { err = d.each(Event, func(v []byte) error { - event := &model.Event{} + event := &dao.Event{} err = decode(v, event) if err != nil { return err @@ -43,6 +43,6 @@ func (d *Dao) EventSearch(ctx context.Context, args *model.EventSearchArgs) (eve return } -func (d *Dao) EventCreate(ctx context.Context, event *model.Event) (err error) { +func (d *Dao) EventCreate(ctx context.Context, event *dao.Event) (err error) { return d.replace(Event, event.ID.Hex(), event) } diff --git a/dao/bolt/registry.go b/dao/bolt/registry.go index 24f16c3..7a65603 100644 --- a/dao/bolt/registry.go +++ b/dao/bolt/registry.go @@ -3,17 +3,17 @@ package bolt import ( "context" - "github.com/cuigh/swirl/model" + "github.com/cuigh/swirl/dao" ) const Registry = "registry" -func (d *Dao) RegistryCreate(ctx context.Context, registry *model.Registry) (err error) { +func (d *Dao) RegistryCreate(ctx context.Context, registry *dao.Registry) (err error) { return d.replace(Registry, registry.ID, registry) } -func (d *Dao) RegistryUpdate(ctx context.Context, registry *model.Registry) (err error) { - old := &model.Registry{} +func (d *Dao) RegistryUpdate(ctx context.Context, registry *dao.Registry) (err error) { + old := &dao.Registry{} return d.update(Registry, registry.ID, old, func() interface{} { registry.CreatedAt = old.CreatedAt registry.CreatedBy = old.CreatedBy @@ -24,9 +24,9 @@ func (d *Dao) RegistryUpdate(ctx context.Context, registry *model.Registry) (err }) } -func (d *Dao) RegistryGetAll(ctx context.Context) (registries []*model.Registry, err error) { +func (d *Dao) RegistryGetAll(ctx context.Context) (registries []*dao.Registry, err error) { err = d.each(Registry, func(v []byte) error { - r := &model.Registry{} + r := &dao.Registry{} err = decode(v, r) if err != nil { return err @@ -37,8 +37,8 @@ func (d *Dao) RegistryGetAll(ctx context.Context) (registries []*model.Registry, return } -func (d *Dao) RegistryGet(ctx context.Context, id string) (registry *model.Registry, err error) { - registry = &model.Registry{} +func (d *Dao) RegistryGet(ctx context.Context, id string) (registry *dao.Registry, err error) { + registry = &dao.Registry{} err = d.get(Registry, id, registry) if err == ErrNoRecords { return nil, nil @@ -48,8 +48,8 @@ func (d *Dao) RegistryGet(ctx context.Context, id string) (registry *model.Regis return } -func (d *Dao) RegistryGetByURL(ctx context.Context, url string) (registry *model.Registry, err error) { - r := &model.Registry{} +func (d *Dao) RegistryGetByURL(ctx context.Context, url string) (registry *dao.Registry, err error) { + r := &dao.Registry{} found, err := d.find(Registry, r, func() bool { return r.URL == url }) if found { return r, nil diff --git a/dao/bolt/role.go b/dao/bolt/role.go index a8e272c..041e080 100644 --- a/dao/bolt/role.go +++ b/dao/bolt/role.go @@ -3,14 +3,14 @@ package bolt import ( "context" - "github.com/cuigh/swirl/model" + "github.com/cuigh/swirl/dao" ) const Role = "role" -func (d *Dao) RoleSearch(ctx context.Context, name string) (roles []*model.Role, err error) { +func (d *Dao) RoleSearch(ctx context.Context, name string) (roles []*dao.Role, err error) { err = d.each(Role, func(v []byte) error { - role := &model.Role{} + role := &dao.Role{} err = decode(v, role) if err != nil { return err @@ -24,12 +24,12 @@ func (d *Dao) RoleSearch(ctx context.Context, name string) (roles []*model.Role, return } -func (d *Dao) RoleCreate(ctx context.Context, role *model.Role) (err error) { +func (d *Dao) RoleCreate(ctx context.Context, role *dao.Role) (err error) { return d.replace(Role, role.ID, role) } -func (d *Dao) RoleGet(ctx context.Context, id string) (role *model.Role, err error) { - role = &model.Role{} +func (d *Dao) RoleGet(ctx context.Context, id string) (role *dao.Role, err error) { + role = &dao.Role{} err = d.get(Role, id, role) if err == ErrNoRecords { return nil, nil @@ -39,8 +39,8 @@ func (d *Dao) RoleGet(ctx context.Context, id string) (role *model.Role, err err return } -func (d *Dao) RoleUpdate(ctx context.Context, role *model.Role) (err error) { - old := &model.Role{} +func (d *Dao) RoleUpdate(ctx context.Context, role *dao.Role) (err error) { + old := &dao.Role{} return d.update(Role, role.ID, old, func() interface{} { role.CreatedAt = old.CreatedAt role.CreatedBy = old.CreatedBy diff --git a/dao/bolt/session.go b/dao/bolt/session.go index fee8ee6..5ffed71 100644 --- a/dao/bolt/session.go +++ b/dao/bolt/session.go @@ -5,13 +5,13 @@ import ( "time" "github.com/boltdb/bolt" - "github.com/cuigh/swirl/model" + "github.com/cuigh/swirl/dao" ) const Session = "session" -func (d *Dao) SessionGet(ctx context.Context, id string) (session *model.Session, err error) { - s := &model.Session{} +func (d *Dao) SessionGet(ctx context.Context, id string) (session *dao.Session, err error) { + s := &dao.Session{} err = d.get(Session, id, s) if err == ErrNoRecords { return nil, nil @@ -21,16 +21,16 @@ func (d *Dao) SessionGet(ctx context.Context, id string) (session *model.Session return } -func (d *Dao) SessionCreate(ctx context.Context, session *model.Session) (err error) { +func (d *Dao) SessionCreate(ctx context.Context, session *dao.Session) (err error) { return d.replace(Session, session.ID, session) } -func (d *Dao) SessionUpdate(ctx context.Context, session *model.Session) (err error) { +func (d *Dao) SessionUpdate(ctx context.Context, session *dao.Session) (err error) { return d.replace(Session, session.UserID, session) } func (d *Dao) SessionUpdateExpiry(ctx context.Context, id string, expiry time.Time) (err error) { - old := &model.Session{} + old := &dao.Session{} return d.update(Session, id, old, func() interface{} { old.Expiry = expiry old.UpdatedAt = time.Now() @@ -55,7 +55,7 @@ func (d *Dao) SessionUpdateDirty(ctx context.Context, userID string, roleID stri return d.db.Update(func(tx *bolt.Tx) (err error) { b := tx.Bucket([]byte(Session)) return b.ForEach(func(k, v []byte) error { - session := &model.Session{} + session := &dao.Session{} if err = decode(v, session); err != nil { return err } @@ -77,7 +77,7 @@ func (d *Dao) SessionPrune() { err := d.db.Update(func(tx *bolt.Tx) (err error) { b := tx.Bucket([]byte(Session)) return b.ForEach(func(k, v []byte) error { - session := &model.Session{} + session := &dao.Session{} if err = decode(v, session); err == nil && session.Expiry.Add(time.Hour).Before(time.Now()) { err = b.Delete(k) } diff --git a/dao/bolt/setting.go b/dao/bolt/setting.go index 7c2ef61..6286ffc 100644 --- a/dao/bolt/setting.go +++ b/dao/bolt/setting.go @@ -3,14 +3,14 @@ package bolt import ( "context" - "github.com/cuigh/swirl/model" + "github.com/cuigh/swirl/dao" ) const Setting = "setting" -func (d *Dao) SettingGetAll(ctx context.Context) (settings []*model.Setting, err error) { +func (d *Dao) SettingGetAll(ctx context.Context) (settings []*dao.Setting, err error) { err = d.each(Setting, func(v []byte) error { - s := &model.Setting{} + s := &dao.Setting{} err = decode(v, s) if err != nil { return err @@ -22,8 +22,8 @@ func (d *Dao) SettingGetAll(ctx context.Context) (settings []*model.Setting, err return } -func (d *Dao) SettingGet(ctx context.Context, id string) (setting *model.Setting, err error) { - setting = &model.Setting{} +func (d *Dao) SettingGet(ctx context.Context, id string) (setting *dao.Setting, err error) { + setting = &dao.Setting{} err = d.get(Setting, id, setting) if err == ErrNoRecords { return nil, nil @@ -33,6 +33,6 @@ func (d *Dao) SettingGet(ctx context.Context, id string) (setting *model.Setting return } -func (d *Dao) SettingUpdate(ctx context.Context, setting *model.Setting) (err error) { +func (d *Dao) SettingUpdate(ctx context.Context, setting *dao.Setting) (err error) { return d.replace(Setting, setting.ID, setting) } diff --git a/dao/bolt/stack.go b/dao/bolt/stack.go index d2a212a..c1e4fa9 100644 --- a/dao/bolt/stack.go +++ b/dao/bolt/stack.go @@ -3,14 +3,14 @@ package bolt import ( "context" - "github.com/cuigh/swirl/model" + "github.com/cuigh/swirl/dao" ) const Stack = "stack" -func (d *Dao) StackGetAll(ctx context.Context) (stacks []*model.Stack, err error) { +func (d *Dao) StackGetAll(ctx context.Context) (stacks []*dao.Stack, err error) { err = d.each(Stack, func(v []byte) error { - stack := &model.Stack{} + stack := &dao.Stack{} err = decode(v, stack) if err == nil { stacks = append(stacks, stack) @@ -20,12 +20,12 @@ func (d *Dao) StackGetAll(ctx context.Context) (stacks []*model.Stack, err error return } -func (d *Dao) StackCreate(ctx context.Context, stack *model.Stack) (err error) { +func (d *Dao) StackCreate(ctx context.Context, stack *dao.Stack) (err error) { return d.replace(Stack, stack.Name, stack) } -func (d *Dao) StackGet(ctx context.Context, name string) (stack *model.Stack, err error) { - stack = &model.Stack{} +func (d *Dao) StackGet(ctx context.Context, name string) (stack *dao.Stack, err error) { + stack = &dao.Stack{} err = d.get(Stack, name, stack) if err == ErrNoRecords { return nil, nil @@ -35,8 +35,8 @@ func (d *Dao) StackGet(ctx context.Context, name string) (stack *model.Stack, er return } -func (d *Dao) StackUpdate(ctx context.Context, stack *model.Stack) (err error) { - old := &model.Stack{} +func (d *Dao) StackUpdate(ctx context.Context, stack *dao.Stack) (err error) { + old := &dao.Stack{} return d.update(Role, stack.Name, old, func() interface{} { stack.CreatedAt = old.CreatedAt stack.CreatedBy = old.CreatedBy diff --git a/dao/bolt/user.go b/dao/bolt/user.go index 5d272c7..f7af04b 100644 --- a/dao/bolt/user.go +++ b/dao/bolt/user.go @@ -3,8 +3,8 @@ package bolt import ( "context" + "github.com/cuigh/swirl/dao" "github.com/cuigh/swirl/misc" - "github.com/cuigh/swirl/model" ) const User = "user" @@ -13,12 +13,12 @@ func (d *Dao) UserCount(ctx context.Context) (count int, err error) { return d.count(User) } -func (d *Dao) UserCreate(ctx context.Context, user *model.User) (err error) { +func (d *Dao) UserCreate(ctx context.Context, user *dao.User) (err error) { return d.replace(User, user.ID, user) } -func (d *Dao) UserUpdate(ctx context.Context, user *model.User) (err error) { - old := &model.User{} +func (d *Dao) UserUpdate(ctx context.Context, user *dao.User) (err error) { + old := &dao.User{} return d.update(User, user.ID, old, func() interface{} { old.Name = user.Name old.LoginName = user.LoginName @@ -32,8 +32,8 @@ func (d *Dao) UserUpdate(ctx context.Context, user *model.User) (err error) { }) } -func (d *Dao) UserUpdateStatus(ctx context.Context, user *model.User) (err error) { - old := &model.User{} +func (d *Dao) UserUpdateStatus(ctx context.Context, user *dao.User) (err error) { + old := &dao.User{} return d.update(User, user.ID, old, func() interface{} { old.Status = user.Status old.UpdatedAt = user.UpdatedAt @@ -46,9 +46,9 @@ func (d *Dao) UserDelete(ctx context.Context, id string) (err error) { return d.delete(User, id) } -func (d *Dao) UserSearch(ctx context.Context, args *model.UserSearchArgs) (users []*model.User, count int, err error) { +func (d *Dao) UserSearch(ctx context.Context, args *dao.UserSearchArgs) (users []*dao.User, count int, err error) { err = d.each(User, func(v []byte) error { - user := &model.User{} + user := &dao.User{} err = decode(v, user) if err != nil { return err @@ -81,8 +81,8 @@ func (d *Dao) UserSearch(ctx context.Context, args *model.UserSearchArgs) (users return } -func (d *Dao) UserGet(ctx context.Context, id string) (user *model.User, err error) { - user = &model.User{} +func (d *Dao) UserGet(ctx context.Context, id string) (user *dao.User, err error) { + user = &dao.User{} err = d.get(User, id, user) if err == ErrNoRecords { return nil, nil @@ -92,8 +92,8 @@ func (d *Dao) UserGet(ctx context.Context, id string) (user *model.User, err err return } -func (d *Dao) UserGetByName(ctx context.Context, loginName string) (user *model.User, err error) { - u := &model.User{} +func (d *Dao) UserGetByName(ctx context.Context, loginName string) (user *dao.User, err error) { + u := &dao.User{} found, err := d.find(User, u, func() bool { return u.LoginName == loginName }) if found { return u, nil @@ -101,8 +101,8 @@ func (d *Dao) UserGetByName(ctx context.Context, loginName string) (user *model. return nil, err } -func (d *Dao) UserUpdateProfile(ctx context.Context, user *model.User) (err error) { - old := &model.User{} +func (d *Dao) UserUpdateProfile(ctx context.Context, user *dao.User) (err error) { + old := &dao.User{} return d.update(User, user.ID, old, func() interface{} { old.Name = user.Name old.LoginName = user.LoginName @@ -113,8 +113,8 @@ func (d *Dao) UserUpdateProfile(ctx context.Context, user *model.User) (err erro }) } -func (d *Dao) UserUpdatePassword(ctx context.Context, user *model.User) (err error) { - old := &model.User{} +func (d *Dao) UserUpdatePassword(ctx context.Context, user *dao.User) (err error) { + old := &dao.User{} return d.update(User, user.ID, old, func() interface{} { old.Password = user.Password old.Salt = user.Salt diff --git a/dao/dao.go b/dao/dao.go index 0f107e1..01c5125 100644 --- a/dao/dao.go +++ b/dao/dao.go @@ -6,87 +6,89 @@ import ( "github.com/cuigh/auxo/app/container" "github.com/cuigh/auxo/errors" - "github.com/cuigh/swirl/dao/bolt" - "github.com/cuigh/swirl/dao/mongo" "github.com/cuigh/swirl/misc" - "github.com/cuigh/swirl/model" ) +var builders = make(map[string]Builder) + +// Builder creates an Interface instance. +type Builder func(addr string) (Interface, error) + +func Register(name string, builder Builder) { + builders[name] = builder +} + // Interface is the interface that wraps all dao methods. type Interface interface { Init() error - RoleGet(ctx context.Context, id string) (*model.Role, error) - RoleSearch(ctx context.Context, name string) (roles []*model.Role, err error) - RoleCreate(ctx context.Context, role *model.Role) error - RoleUpdate(ctx context.Context, role *model.Role) error + RoleGet(ctx context.Context, id string) (*Role, error) + RoleSearch(ctx context.Context, name string) (roles []*Role, err error) + RoleCreate(ctx context.Context, role *Role) error + RoleUpdate(ctx context.Context, role *Role) error RoleDelete(ctx context.Context, id string) error - UserGet(ctx context.Context, id string) (*model.User, error) - UserGetByName(ctx context.Context, loginName string) (*model.User, error) - UserSearch(ctx context.Context, args *model.UserSearchArgs) (users []*model.User, count int, err error) + UserGet(ctx context.Context, id string) (*User, error) + UserGetByName(ctx context.Context, loginName string) (*User, error) + UserSearch(ctx context.Context, args *UserSearchArgs) (users []*User, count int, err error) UserCount(ctx context.Context) (int, error) - UserCreate(ctx context.Context, user *model.User) error - UserUpdate(ctx context.Context, user *model.User) error - UserUpdateStatus(ctx context.Context, user *model.User) error - UserUpdateProfile(ctx context.Context, user *model.User) error - UserUpdatePassword(ctx context.Context, user *model.User) error + UserCreate(ctx context.Context, user *User) error + UserUpdate(ctx context.Context, user *User) error + UserUpdateStatus(ctx context.Context, user *User) error + UserUpdateProfile(ctx context.Context, user *User) error + UserUpdatePassword(ctx context.Context, user *User) error UserDelete(ctx context.Context, id string) error - SessionGet(ctx context.Context, id string) (*model.Session, error) - SessionCreate(ctx context.Context, session *model.Session) error - SessionUpdate(ctx context.Context, session *model.Session) error + SessionGet(ctx context.Context, id string) (*Session, error) + SessionCreate(ctx context.Context, session *Session) error + SessionUpdate(ctx context.Context, session *Session) error SessionUpdateExpiry(ctx context.Context, id string, expiry time.Time) (err error) SessionUpdateDirty(ctx context.Context, userID string, roleID string) (err error) - RegistryGet(ctx context.Context, id string) (*model.Registry, error) - RegistryGetByURL(ctx context.Context, url string) (registry *model.Registry, err error) - RegistryGetAll(ctx context.Context) (registries []*model.Registry, err error) - RegistryCreate(ctx context.Context, registry *model.Registry) error - RegistryUpdate(ctx context.Context, registry *model.Registry) error + RegistryGet(ctx context.Context, id string) (*Registry, error) + RegistryGetByURL(ctx context.Context, url string) (registry *Registry, err error) + RegistryGetAll(ctx context.Context) (registries []*Registry, err error) + RegistryCreate(ctx context.Context, registry *Registry) error + RegistryUpdate(ctx context.Context, registry *Registry) error RegistryDelete(ctx context.Context, id string) error - StackGet(ctx context.Context, name string) (*model.Stack, error) - StackGetAll(ctx context.Context) (stacks []*model.Stack, err error) - StackCreate(ctx context.Context, stack *model.Stack) error - StackUpdate(ctx context.Context, stack *model.Stack) error + StackGet(ctx context.Context, name string) (*Stack, error) + StackGetAll(ctx context.Context) (stacks []*Stack, err error) + StackCreate(ctx context.Context, stack *Stack) error + StackUpdate(ctx context.Context, stack *Stack) error StackDelete(ctx context.Context, name string) error - EventSearch(ctx context.Context, args *model.EventSearchArgs) (events []*model.Event, count int, err error) - EventCreate(ctx context.Context, event *model.Event) error + EventSearch(ctx context.Context, args *EventSearchArgs) (events []*Event, count int, err error) + EventCreate(ctx context.Context, event *Event) error - SettingGet(ctx context.Context, id string) (*model.Setting, error) - SettingGetAll(ctx context.Context) (settings []*model.Setting, err error) - SettingUpdate(ctx context.Context, setting *model.Setting) error + SettingGet(ctx context.Context, id string) (*Setting, error) + SettingGetAll(ctx context.Context) (settings []*Setting, err error) + SettingUpdate(ctx context.Context, setting *Setting) error - ChartGet(ctx context.Context, id string) (*model.Chart, error) - ChartGetBatch(ctx context.Context, ids ...string) ([]*model.Chart, error) - ChartSearch(ctx context.Context, args *model.ChartSearchArgs) (charts []*model.Chart, count int, err error) - ChartCreate(ctx context.Context, chart *model.Chart) error - ChartUpdate(ctx context.Context, chart *model.Chart) error + ChartGet(ctx context.Context, id string) (*Chart, error) + ChartGetBatch(ctx context.Context, ids ...string) ([]*Chart, error) + ChartSearch(ctx context.Context, args *ChartSearchArgs) (charts []*Chart, count int, err error) + ChartCreate(ctx context.Context, chart *Chart) error + ChartUpdate(ctx context.Context, chart *Chart) error ChartDelete(ctx context.Context, id string) error - DashboardGet(ctx context.Context, name, key string) (dashboard *model.Dashboard, err error) - DashboardUpdate(ctx context.Context, dashboard *model.Dashboard) error + DashboardGet(ctx context.Context, name, key string) (dashboard *Dashboard, err error) + DashboardUpdate(ctx context.Context, dashboard *Dashboard) error } func newInterface() (i Interface) { var err error - switch misc.Options.DBType { - case "", "mongo": - i, err = mongo.New(misc.Options.DBAddress) - case "bolt": - i, err = bolt.New(misc.Options.DBAddress) - default: + if b, ok := builders[misc.Options.DBType]; ok { + i, err = b(misc.Options.DBAddress) + } else { err = errors.New("unknown database type: " + misc.Options.DBType) } if err != nil { panic(err) } - - return i + return } func init() { diff --git a/model/model.go b/dao/entity.go similarity index 98% rename from model/model.go rename to dao/entity.go index 58911a0..9536491 100644 --- a/model/model.go +++ b/dao/entity.go @@ -1,4 +1,4 @@ -package model +package dao import ( "encoding/base64" @@ -47,10 +47,6 @@ func (t Time) String() string { return time.Time(t).String() } -func (t Time) Format(layout string) string { - return time.Time(t).Format(layout) -} - type Operator struct { ID string `json:"id,omitempty" bson:"_id,omitempty"` Name string `json:"name,omitempty" bson:"name,omitempty"` diff --git a/dao/mongo/chart.go b/dao/mongo/chart.go index e3058dd..a136301 100644 --- a/dao/mongo/chart.go +++ b/dao/mongo/chart.go @@ -3,7 +3,7 @@ package mongo import ( "context" - "github.com/cuigh/swirl/model" + "github.com/cuigh/swirl/dao" "go.mongodb.org/mongo-driver/bson" ) @@ -12,7 +12,7 @@ const ( Dashboard = "dashboard" ) -func (d *Dao) ChartSearch(ctx context.Context, args *model.ChartSearchArgs) (charts []*model.Chart, count int, err error) { +func (d *Dao) ChartSearch(ctx context.Context, args *dao.ChartSearchArgs) (charts []*dao.Chart, count int, err error) { filter := bson.M{} if args.Title != "" { filter["title"] = args.Title @@ -21,17 +21,17 @@ func (d *Dao) ChartSearch(ctx context.Context, args *model.ChartSearchArgs) (cha filter["dashboard"] = bson.M{"$in": []string{"", args.Dashboard}} } opts := searchOptions{filter: filter, sorter: bson.M{"updated_at": -1}, pageIndex: args.PageIndex, pageSize: args.PageSize} - charts = []*model.Chart{} + charts = []*dao.Chart{} count, err = d.search(ctx, Chart, opts, &charts) return } -func (d *Dao) ChartCreate(ctx context.Context, chart *model.Chart) (err error) { +func (d *Dao) ChartCreate(ctx context.Context, chart *dao.Chart) (err error) { return d.create(ctx, Chart, chart) } -func (d *Dao) ChartGet(ctx context.Context, id string) (chart *model.Chart, err error) { - chart = &model.Chart{} +func (d *Dao) ChartGet(ctx context.Context, id string) (chart *dao.Chart, err error) { + chart = &dao.Chart{} found, err := d.find(ctx, Chart, id, chart) if !found { return nil, err @@ -39,13 +39,13 @@ func (d *Dao) ChartGet(ctx context.Context, id string) (chart *model.Chart, err return } -func (d *Dao) ChartGetBatch(ctx context.Context, names ...string) (charts []*model.Chart, err error) { - charts = []*model.Chart{} +func (d *Dao) ChartGetBatch(ctx context.Context, names ...string) (charts []*dao.Chart, err error) { + charts = []*dao.Chart{} err = d.fetch(ctx, Chart, bson.M{"_id": bson.M{"$in": names}}, &charts) return } -func (d *Dao) ChartUpdate(ctx context.Context, chart *model.Chart) (err error) { +func (d *Dao) ChartUpdate(ctx context.Context, chart *dao.Chart) (err error) { update := bson.M{ "$set": bson.M{ "title": chart.Title, @@ -68,8 +68,8 @@ func (d *Dao) ChartDelete(ctx context.Context, id string) (err error) { return d.delete(ctx, Chart, id) } -func (d *Dao) DashboardGet(ctx context.Context, name, key string) (dashboard *model.Dashboard, err error) { - dashboard = &model.Dashboard{ +func (d *Dao) DashboardGet(ctx context.Context, name, key string) (dashboard *dao.Dashboard, err error) { + dashboard = &dao.Dashboard{ Name: name, Key: key, } @@ -80,7 +80,7 @@ func (d *Dao) DashboardGet(ctx context.Context, name, key string) (dashboard *mo return } -func (d *Dao) DashboardUpdate(ctx context.Context, dashboard *model.Dashboard) (err error) { +func (d *Dao) DashboardUpdate(ctx context.Context, dashboard *dao.Dashboard) (err error) { update := bson.M{ "$set": dashboard, } diff --git a/dao/mongo/event.go b/dao/mongo/event.go index dd45066..5127a2f 100644 --- a/dao/mongo/event.go +++ b/dao/mongo/event.go @@ -3,13 +3,13 @@ package mongo import ( "context" - "github.com/cuigh/swirl/model" + "github.com/cuigh/swirl/dao" "go.mongodb.org/mongo-driver/bson" ) const Event = "event" -func (d *Dao) EventSearch(ctx context.Context, args *model.EventSearchArgs) (events []*model.Event, count int, err error) { +func (d *Dao) EventSearch(ctx context.Context, args *dao.EventSearchArgs) (events []*dao.Event, count int, err error) { filter := bson.M{} if args.Type != "" { filter["type"] = args.Type @@ -18,11 +18,11 @@ func (d *Dao) EventSearch(ctx context.Context, args *model.EventSearchArgs) (eve filter["name"] = args.Name } opts := searchOptions{filter: filter, sorter: bson.M{"_id": -1}, pageIndex: args.PageIndex, pageSize: args.PageSize} - events = []*model.Event{} + events = []*dao.Event{} count, err = d.search(ctx, Event, opts, &events) return } -func (d *Dao) EventCreate(ctx context.Context, event *model.Event) (err error) { +func (d *Dao) EventCreate(ctx context.Context, event *dao.Event) (err error) { return d.create(ctx, Event, event) } diff --git a/dao/mongo/mongo.go b/dao/mongo/mongo.go index 46980db..6cb35b2 100644 --- a/dao/mongo/mongo.go +++ b/dao/mongo/mongo.go @@ -7,6 +7,7 @@ import ( "github.com/cuigh/auxo/app" "github.com/cuigh/auxo/log" + "github.com/cuigh/swirl/dao" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" @@ -47,7 +48,7 @@ type Dao struct { logger log.Logger } -func New(addr string) (*Dao, error) { +func New(addr string) (dao.Interface, error) { db, err := open(addr) if err != nil { return nil, err @@ -175,3 +176,7 @@ type searchOptions struct { pageIndex int pageSize int } + +func init() { + dao.Register("mongo", New) +} diff --git a/dao/mongo/registry.go b/dao/mongo/registry.go index cec0d06..13ce2eb 100644 --- a/dao/mongo/registry.go +++ b/dao/mongo/registry.go @@ -3,18 +3,18 @@ package mongo import ( "context" - "github.com/cuigh/swirl/model" + "github.com/cuigh/swirl/dao" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" ) const Registry = "registry" -func (d *Dao) RegistryCreate(ctx context.Context, registry *model.Registry) (err error) { +func (d *Dao) RegistryCreate(ctx context.Context, registry *dao.Registry) (err error) { return d.create(ctx, Registry, registry) } -func (d *Dao) RegistryUpdate(ctx context.Context, registry *model.Registry) (err error) { +func (d *Dao) RegistryUpdate(ctx context.Context, registry *dao.Registry) (err error) { update := bson.M{ "name": registry.Name, "url": registry.URL, @@ -28,14 +28,14 @@ func (d *Dao) RegistryUpdate(ctx context.Context, registry *model.Registry) (err return d.update(ctx, Registry, registry.ID, bson.M{"$set": update}) } -func (d *Dao) RegistryGetAll(ctx context.Context) (registries []*model.Registry, err error) { - registries = []*model.Registry{} +func (d *Dao) RegistryGetAll(ctx context.Context) (registries []*dao.Registry, err error) { + registries = []*dao.Registry{} err = d.fetch(ctx, Registry, bson.M{}, ®istries) return } -func (d *Dao) RegistryGet(ctx context.Context, id string) (registry *model.Registry, err error) { - registry = &model.Registry{} +func (d *Dao) RegistryGet(ctx context.Context, id string) (registry *dao.Registry, err error) { + registry = &dao.Registry{} found, err := d.find(ctx, Registry, id, registry) if !found { return nil, err @@ -43,8 +43,8 @@ func (d *Dao) RegistryGet(ctx context.Context, id string) (registry *model.Regis return } -func (d *Dao) RegistryGetByURL(ctx context.Context, url string) (registry *model.Registry, err error) { - registry = &model.Registry{} +func (d *Dao) RegistryGetByURL(ctx context.Context, url string) (registry *dao.Registry, err error) { + registry = &dao.Registry{} err = d.db.Collection(Registry).FindOne(ctx, bson.M{"url": url}).Decode(registry) if err == mongo.ErrNoDocuments { return nil, nil diff --git a/dao/mongo/role.go b/dao/mongo/role.go index a276b9e..dd620fc 100644 --- a/dao/mongo/role.go +++ b/dao/mongo/role.go @@ -3,28 +3,28 @@ package mongo import ( "context" - "github.com/cuigh/swirl/model" + "github.com/cuigh/swirl/dao" "go.mongodb.org/mongo-driver/bson" ) const Role = "role" -func (d *Dao) RoleSearch(ctx context.Context, name string) (roles []*model.Role, err error) { +func (d *Dao) RoleSearch(ctx context.Context, name string) (roles []*dao.Role, err error) { filter := bson.M{} if name != "" { filter["name"] = name } - roles = []*model.Role{} + roles = []*dao.Role{} err = d.fetch(ctx, Role, filter, &roles) return } -func (d *Dao) RoleCreate(ctx context.Context, role *model.Role) (err error) { +func (d *Dao) RoleCreate(ctx context.Context, role *dao.Role) (err error) { return d.create(ctx, Role, role) } -func (d *Dao) RoleGet(ctx context.Context, id string) (role *model.Role, err error) { - role = &model.Role{} +func (d *Dao) RoleGet(ctx context.Context, id string) (role *dao.Role, err error) { + role = &dao.Role{} found, err := d.find(ctx, Role, id, role) if !found { return nil, err @@ -32,7 +32,7 @@ func (d *Dao) RoleGet(ctx context.Context, id string) (role *model.Role, err err return } -func (d *Dao) RoleUpdate(ctx context.Context, role *model.Role) (err error) { +func (d *Dao) RoleUpdate(ctx context.Context, role *dao.Role) (err error) { update := bson.M{ "$set": bson.M{ "name": role.Name, diff --git a/dao/mongo/session.go b/dao/mongo/session.go index 6221a7c..e933d4b 100644 --- a/dao/mongo/session.go +++ b/dao/mongo/session.go @@ -4,14 +4,14 @@ import ( "context" "time" - "github.com/cuigh/swirl/model" + "github.com/cuigh/swirl/dao" "go.mongodb.org/mongo-driver/bson" ) const Session = "session" -func (d *Dao) SessionGet(ctx context.Context, id string) (session *model.Session, err error) { - session = &model.Session{} +func (d *Dao) SessionGet(ctx context.Context, id string) (session *dao.Session, err error) { + session = &dao.Session{} found, err := d.find(ctx, Session, id, session) if !found { return nil, err @@ -19,11 +19,11 @@ func (d *Dao) SessionGet(ctx context.Context, id string) (session *model.Session return } -func (d *Dao) SessionCreate(ctx context.Context, session *model.Session) (err error) { +func (d *Dao) SessionCreate(ctx context.Context, session *dao.Session) (err error) { return d.create(ctx, Session, session) } -func (d *Dao) SessionUpdate(ctx context.Context, session *model.Session) (err error) { +func (d *Dao) SessionUpdate(ctx context.Context, session *dao.Session) (err error) { return d.update(ctx, Session, session.ID, session) } diff --git a/dao/mongo/setting.go b/dao/mongo/setting.go index 7b85c5a..f6f4865 100644 --- a/dao/mongo/setting.go +++ b/dao/mongo/setting.go @@ -3,20 +3,20 @@ package mongo import ( "context" - "github.com/cuigh/swirl/model" + "github.com/cuigh/swirl/dao" "go.mongodb.org/mongo-driver/bson" ) const Setting = "setting" -func (d *Dao) SettingGetAll(ctx context.Context) (settings []*model.Setting, err error) { - settings = []*model.Setting{} +func (d *Dao) SettingGetAll(ctx context.Context) (settings []*dao.Setting, err error) { + settings = []*dao.Setting{} err = d.fetch(ctx, Setting, bson.M{}, &settings) return } -func (d *Dao) SettingGet(ctx context.Context, id string) (setting *model.Setting, err error) { - setting = &model.Setting{} +func (d *Dao) SettingGet(ctx context.Context, id string) (setting *dao.Setting, err error) { + setting = &dao.Setting{} found, err := d.find(ctx, Setting, id, setting) if !found { return nil, err @@ -24,7 +24,7 @@ func (d *Dao) SettingGet(ctx context.Context, id string) (setting *model.Setting return } -func (d *Dao) SettingUpdate(ctx context.Context, setting *model.Setting) (err error) { +func (d *Dao) SettingUpdate(ctx context.Context, setting *dao.Setting) (err error) { update := bson.M{ "$set": bson.M{ "options": setting.Options, diff --git a/dao/mongo/stack.go b/dao/mongo/stack.go index 6144028..1af6cce 100644 --- a/dao/mongo/stack.go +++ b/dao/mongo/stack.go @@ -3,24 +3,24 @@ package mongo import ( "context" - "github.com/cuigh/swirl/model" + "github.com/cuigh/swirl/dao" "go.mongodb.org/mongo-driver/bson" ) const Stack = "stack" -func (d *Dao) StackGetAll(ctx context.Context) (stacks []*model.Stack, err error) { - stacks = []*model.Stack{} +func (d *Dao) StackGetAll(ctx context.Context) (stacks []*dao.Stack, err error) { + stacks = []*dao.Stack{} err = d.fetch(ctx, Stack, bson.M{}, &stacks) return } -func (d *Dao) StackCreate(ctx context.Context, stack *model.Stack) (err error) { +func (d *Dao) StackCreate(ctx context.Context, stack *dao.Stack) (err error) { return d.create(ctx, Stack, stack) } -func (d *Dao) StackGet(ctx context.Context, name string) (stack *model.Stack, err error) { - stack = &model.Stack{} +func (d *Dao) StackGet(ctx context.Context, name string) (stack *dao.Stack, err error) { + stack = &dao.Stack{} found, err := d.find(ctx, Stack, name, stack) if !found { return nil, err @@ -28,7 +28,7 @@ func (d *Dao) StackGet(ctx context.Context, name string) (stack *model.Stack, er return } -func (d *Dao) StackUpdate(ctx context.Context, stack *model.Stack) (err error) { +func (d *Dao) StackUpdate(ctx context.Context, stack *dao.Stack) (err error) { update := bson.M{ "$set": bson.M{ "content": stack.Content, diff --git a/dao/mongo/user.go b/dao/mongo/user.go index a2efbf8..d9c2e65 100644 --- a/dao/mongo/user.go +++ b/dao/mongo/user.go @@ -3,7 +3,7 @@ package mongo import ( "context" - "github.com/cuigh/swirl/model" + "github.com/cuigh/swirl/dao" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" ) @@ -15,11 +15,11 @@ func (d *Dao) UserCount(ctx context.Context) (int, error) { return int(count), err } -func (d *Dao) UserCreate(ctx context.Context, user *model.User) (err error) { +func (d *Dao) UserCreate(ctx context.Context, user *dao.User) (err error) { return d.create(ctx, User, user) } -func (d *Dao) UserUpdate(ctx context.Context, user *model.User) (err error) { +func (d *Dao) UserUpdate(ctx context.Context, user *dao.User) (err error) { update := bson.M{ "$set": bson.M{ "name": user.Name, @@ -35,7 +35,7 @@ func (d *Dao) UserUpdate(ctx context.Context, user *model.User) (err error) { return d.update(ctx, User, user.ID, update) } -func (d *Dao) UserUpdateStatus(ctx context.Context, user *model.User) (err error) { +func (d *Dao) UserUpdateStatus(ctx context.Context, user *dao.User) (err error) { update := bson.M{ "$set": bson.M{ "status": user.Status, @@ -50,7 +50,7 @@ func (d *Dao) UserDelete(ctx context.Context, id string) (err error) { return d.delete(ctx, User, id) } -func (d *Dao) UserSearch(ctx context.Context, args *model.UserSearchArgs) (users []*model.User, count int, err error) { +func (d *Dao) UserSearch(ctx context.Context, args *dao.UserSearchArgs) (users []*dao.User, count int, err error) { filter := bson.M{} if args.Name != "" { filter["name"] = args.Name @@ -65,13 +65,13 @@ func (d *Dao) UserSearch(ctx context.Context, args *model.UserSearchArgs) (users filter["status"] = args.Status } opts := searchOptions{filter: filter, pageIndex: args.PageIndex, pageSize: args.PageSize} - users = []*model.User{} + users = []*dao.User{} count, err = d.search(ctx, User, opts, &users) return } -func (d *Dao) UserGet(ctx context.Context, id string) (user *model.User, err error) { - user = &model.User{} +func (d *Dao) UserGet(ctx context.Context, id string) (user *dao.User, err error) { + user = &dao.User{} found, err := d.find(ctx, User, id, user) if !found { return nil, err @@ -79,8 +79,8 @@ func (d *Dao) UserGet(ctx context.Context, id string) (user *model.User, err err return } -func (d *Dao) UserGetByName(ctx context.Context, loginName string) (user *model.User, err error) { - user = &model.User{} +func (d *Dao) UserGetByName(ctx context.Context, loginName string) (user *dao.User, err error) { + user = &dao.User{} err = d.db.Collection(User).FindOne(ctx, bson.M{"login_name": loginName}).Decode(user) if err == mongo.ErrNoDocuments { return nil, nil @@ -90,7 +90,7 @@ func (d *Dao) UserGetByName(ctx context.Context, loginName string) (user *model. return } -func (d *Dao) UserUpdateProfile(ctx context.Context, user *model.User) (err error) { +func (d *Dao) UserUpdateProfile(ctx context.Context, user *dao.User) (err error) { update := bson.M{ "$set": bson.M{ "name": user.Name, @@ -103,7 +103,7 @@ func (d *Dao) UserUpdateProfile(ctx context.Context, user *model.User) (err erro return d.update(ctx, User, user.ID, update) } -func (d *Dao) UserUpdatePassword(ctx context.Context, user *model.User) (err error) { +func (d *Dao) UserUpdatePassword(ctx context.Context, user *dao.User) (err error) { update := bson.M{ "$set": bson.M{ "password": user.Password, diff --git a/main.go b/main.go index 81a2182..4171740 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,6 @@ import ( "github.com/cuigh/auxo/app" "github.com/cuigh/auxo/app/container" "github.com/cuigh/auxo/app/flag" - _ "github.com/cuigh/auxo/cache/memory" "github.com/cuigh/auxo/data" "github.com/cuigh/auxo/data/valid" "github.com/cuigh/auxo/errors" @@ -20,6 +19,8 @@ import ( "github.com/cuigh/auxo/util/run" _ "github.com/cuigh/swirl/api" "github.com/cuigh/swirl/biz" + _ "github.com/cuigh/swirl/dao/bolt" + _ "github.com/cuigh/swirl/dao/mongo" "github.com/cuigh/swirl/misc" "github.com/cuigh/swirl/scaler" ) @@ -31,7 +32,7 @@ var ( func main() { app.Name = "Swirl" - app.Version = "1.0.0beta6" + app.Version = "1.0.0beta7" app.Desc = "A web management UI for Docker, focused on swarm cluster" app.Action = func(ctx *app.Context) error { return run.Pipeline(misc.LoadOptions, initSystem, scaler.Start, startServer) diff --git a/security/auth.go b/security/auth.go index c5d44e1..3b73a5e 100644 --- a/security/auth.go +++ b/security/auth.go @@ -12,8 +12,8 @@ import ( "github.com/cuigh/auxo/security/certify/ldap" "github.com/cuigh/auxo/security/passwd" "github.com/cuigh/swirl/biz" + "github.com/cuigh/swirl/dao" "github.com/cuigh/swirl/misc" - "github.com/cuigh/swirl/model" "go.mongodb.org/mongo-driver/bson/primitive" ) @@ -52,7 +52,7 @@ func (c *Identifier) Apply(next web.HandlerFunc) web.HandlerFunc { func (c *Identifier) Identify(loginName, password string) (identify Identity, err error) { var ( u security.User - s *model.Session + s *dao.Session ) u, err = c.signIn(loginName, password) @@ -126,7 +126,7 @@ func (c *Identifier) identifyUser(token string) web.User { return c.createUser(session) } -func (c *Identifier) createUser(s *model.Session) web.User { +func (c *Identifier) createUser(s *dao.Session) web.User { return &User{ token: s.ID, id: s.UserID, @@ -136,8 +136,8 @@ func (c *Identifier) createUser(s *model.Session) web.User { } } -func (c *Identifier) createSession(user security.User) (s *model.Session, err error) { - s = &model.Session{ +func (c *Identifier) createSession(user security.User) (s *dao.Session, err error) { + s = &dao.Session{ ID: primitive.NewObjectID().Hex(), UserID: user.ID(), Username: user.Name(), @@ -150,14 +150,14 @@ func (c *Identifier) createSession(user security.User) (s *model.Session, err er return } -func (c *Identifier) updateSession(s *model.Session) (err error) { +func (c *Identifier) updateSession(s *dao.Session) (err error) { if err = c.fillSession(s); err == nil { err = c.sb.Update(s) } return } -func (c *Identifier) fillSession(s *model.Session) (err error) { +func (c *Identifier) fillSession(s *dao.Session) (err error) { u, err := c.ub.FindByID(s.UserID) if err != nil { return err @@ -181,7 +181,7 @@ func (c *Identifier) fillSession(s *model.Session) (err error) { return nil } -func (c *Identifier) renewSession(s *model.Session) { +func (c *Identifier) renewSession(s *dao.Session) { expiry := time.Now().Add(misc.Options.TokenExpiry) if expiry.After(s.MaxExpiry) { expiry = s.MaxExpiry @@ -253,7 +253,7 @@ func ldapRealm(s *misc.Setting, ub biz.UserBiz) RealmFunc { lu = user.(*ldap.User) ) if u == nil { - id, err = ub.Create(&model.User{ + id, err = ub.Create(&dao.User{ Type: biz.UserTypeLDAP, LoginName: loginName, Name: lu.Name(), diff --git a/ui/src/pages/event/List.vue b/ui/src/pages/event/List.vue index e1c1391..2842324 100644 --- a/ui/src/pages/event/List.vue +++ b/ui/src/pages/event/List.vue @@ -61,6 +61,7 @@ import type { Event } from "@/api/event"; import { useDataTable } from "@/utils/data-table"; import { renderLink, renderTag, renderTime } from "@/utils/render"; import { useI18n } from 'vue-i18n' +import type { RouteLocationRaw } from "vue-router"; const { t } = useI18n() const filter = reactive({ @@ -173,10 +174,7 @@ const columns = [ key: "name", render(e: Event) { const u = url(e) - if (u === '') { - return e.name - } - return renderLink(u, e.name) + return u ? renderLink(u, e.name) : e.name }, }, { @@ -192,34 +190,42 @@ const columns = [ ]; const { state, pagination, fetchData, changePageSize } = useDataTable(eventApi.search, filter) -function url(e: Event): string { +function url(e: Event): RouteLocationRaw | null { + if (e.type === 'Setting') { + return { name: 'setting' } + } else if (!e.code) { + return null + } + switch (e.type) { case "User": - return `/system/users/${e.code}` + return { name: 'user_detail', params: { id: e.code } } case "Role": - return `/system/roles/${e.code}` + return { name: 'role_detail', params: { id: e.code } } case "Chart": - return `/system/charts/${e.code}` - case "Setting": - return '/system/settings' + return { name: 'chart_detail', params: { id: e.code } } case "Registry": - return `/swarm/registries/${e.code}` + return { name: 'registry_detail', params: { id: e.code } } case "Node": - return `/swarm/nodes/${e.code}` + return { name: 'node_detail', params: { id: e.code } } case "Network": - return `/swarm/networks/${e.code}` + return { name: 'network_detail', params: { name: e.code } } case "Service": - return `/swarm/services/${e.code}` + return { name: 'service_detail', params: { name: e.code } } case "Stack": - return `/swarm/stacks/${e.code}` + return { name: 'stack_detail', params: { name: e.code } } case "Config": - return `/swarm/configs/${e.code}` + return { name: 'config_detail', params: { id: e.code } } case "Secret": - return `/swarm/secrets/${e.code}` + return { name: 'secret_detail', params: { id: e.code } } + case "Image": + return { name: 'image_detail', params: { node: '-', id: e.code } } + case "Container": + return { name: 'container_detail', params: { node: '-', id: e.code } } case "Volume": - return `/local/volumes/${e.code}` + return { name: 'volume_detail', params: { node: '-', name: e.code } } } - return '' + return null } function prune() {