This is an automated email from the ASF dual-hosted git repository.
juzhiyuan pushed a commit to branch refactor
in repository https://gitbox.apache.org/repos/asf/apisix-dashboard.git
The following commit(s) were added to refs/heads/refactor by this push:
new 33623e8 feat: add consumer CURD refactoring (#486)
33623e8 is described below
commit 33623e8312af5df8a425ddebe411dd1984600287
Author: nic-chen <[email protected]>
AuthorDate: Sun Sep 20 13:42:52 2020 +0800
feat: add consumer CURD refactoring (#486)
* feat: add consumer CURD refactoring
* remove debug
* remove useless slashes
---
.../{route/route.go => consumer/consumer.go} | 63 +++++++++++-----------
api/internal/handler/route/route.go | 13 ++---
api/route/base.go | 10 ++--
3 files changed, 45 insertions(+), 41 deletions(-)
diff --git a/api/internal/handler/route/route.go
b/api/internal/handler/consumer/consumer.go
similarity index 63%
copy from api/internal/handler/route/route.go
copy to api/internal/handler/consumer/consumer.go
index 7edf64e..36180fe 100644
--- a/api/internal/handler/route/route.go
+++ b/api/internal/handler/consumer/consumer.go
@@ -1,31 +1,32 @@
-package route
+package consumer
import (
- "github.com/apisix/manager-api/internal/core/entity"
- "github.com/apisix/manager-api/internal/core/store"
- "github.com/apisix/manager-api/internal/handler"
- "github.com/apisix/manager-api/internal/utils"
+ "reflect"
+ "strings"
+
"github.com/gin-gonic/gin"
"github.com/shiningrush/droplet"
"github.com/shiningrush/droplet/data"
"github.com/shiningrush/droplet/wrapper"
wgin "github.com/shiningrush/droplet/wrapper/gin"
- "strings"
- "reflect"
+ "github.com/apisix/manager-api/internal/core/entity"
+ "github.com/apisix/manager-api/internal/core/store"
+ "github.com/apisix/manager-api/internal/handler"
+ "github.com/apisix/manager-api/internal/utils"
)
type Handler struct {
- routeStore *store.GenericStore
+ consumerStore *store.GenericStore
}
func NewHandler() (handler.RouteRegister, error) {
s, err := store.NewGenericStore(store.GenericStoreOption{
- BasePath: "/apisix/routes",
- ObjType: reflect.TypeOf(entity.Route{}),
+ BasePath: "/apisix/consumers",
+ ObjType: reflect.TypeOf(entity.Consumer{}),
KeyFunc: func(obj interface{}) string {
- r := obj.(*entity.Route)
- return r.ID
+ r := obj.(*entity.Consumer)
+ return r.Username
},
})
if err != nil {
@@ -37,20 +38,20 @@ func NewHandler() (handler.RouteRegister, error) {
utils.AppendToClosers(s.Close)
return &Handler{
- routeStore: s,
+ consumerStore: s,
}, nil
}
func (h *Handler) ApplyRoute(r *gin.Engine) {
- r.GET("/apisix/admin/routes/:id", wgin.Wraps(h.Get,
+ r.GET("/apisix/admin/consumers/:id", wgin.Wraps(h.Get,
wrapper.InputType(reflect.TypeOf(GetInput{}))))
- r.GET("/apisix/admin/routes", wgin.Wraps(h.List,
+ r.GET("/apisix/admin/consumers", wgin.Wraps(h.List,
wrapper.InputType(reflect.TypeOf(ListInput{}))))
- r.POST("/apisix/admin/routes", wgin.Wraps(h.Create,
- wrapper.InputType(reflect.TypeOf(entity.Route{}))))
- r.PUT("/apisix/admin/routes/:id", wgin.Wraps(h.Update,
+ r.POST("/apisix/admin/consumers", wgin.Wraps(h.Create,
+ wrapper.InputType(reflect.TypeOf(entity.Consumer{}))))
+ r.PUT("/apisix/admin/consumers/:id", wgin.Wraps(h.Update,
wrapper.InputType(reflect.TypeOf(UpdateInput{}))))
- r.DELETE("/apisix/admin/routes", wgin.Wraps(h.BatchDelete,
+ r.DELETE("/apisix/admin/consumers", wgin.Wraps(h.BatchDelete,
wrapper.InputType(reflect.TypeOf(BatchDelete{}))))
}
@@ -61,7 +62,7 @@ type GetInput struct {
func (h *Handler) Get(c droplet.Context) (interface{}, error) {
input := c.Input().(*GetInput)
- r, err := h.routeStore.Get(input.ID)
+ r, err := h.consumerStore.Get(input.ID)
if err != nil {
return nil, err
}
@@ -69,17 +70,17 @@ func (h *Handler) Get(c droplet.Context) (interface{},
error) {
}
type ListInput struct {
- Name string `auto_read:"name,query"`
+ Username string `auto_read:"username,query"`
data.Pager
}
func (h *Handler) List(c droplet.Context) (interface{}, error) {
input := c.Input().(*ListInput)
- ret, err := h.routeStore.List(store.ListInput{
+ ret, err := h.consumerStore.List(store.ListInput{
Predicate: func(obj interface{}) bool {
- if input.Name != "" {
- return strings.Index(obj.(*entity.Route).Name,
input.Name) > 0
+ if input.Username != "" {
+ return
strings.Index(obj.(*entity.Consumer).Username, input.Username) > 0
}
return true
},
@@ -94,9 +95,9 @@ func (h *Handler) List(c droplet.Context) (interface{},
error) {
}
func (h *Handler) Create(c droplet.Context) (interface{}, error) {
- input := c.Input().(*entity.Route)
+ input := c.Input().(*entity.Consumer)
- if err := h.routeStore.Create(c.Context(), input); err != nil {
+ if err := h.consumerStore.Create(c.Context(), input); err != nil {
return nil, err
}
@@ -105,14 +106,14 @@ func (h *Handler) Create(c droplet.Context) (interface{},
error) {
type UpdateInput struct {
ID string `auto_read:"id,path"`
- entity.Route
+ entity.Consumer
}
func (h *Handler) Update(c droplet.Context) (interface{}, error) {
input := c.Input().(*UpdateInput)
- input.Route.ID = input.ID
+ input.Consumer.ID = input.ID
- if err := h.routeStore.Update(c.Context(), &input.Route); err != nil {
+ if err := h.consumerStore.Update(c.Context(), &input.Consumer); err !=
nil {
return nil, err
}
@@ -120,13 +121,13 @@ func (h *Handler) Update(c droplet.Context) (interface{},
error) {
}
type BatchDelete struct {
- IDs string `auto_read:"ids,query"`
+ UserNames string `auto_read:"usernames,query"`
}
func (h *Handler) BatchDelete(c droplet.Context) (interface{}, error) {
input := c.Input().(*BatchDelete)
- if err := h.routeStore.BatchDelete(c.Context(),
strings.Split(input.IDs, ",")); err != nil {
+ if err := h.consumerStore.BatchDelete(c.Context(),
strings.Split(input.UserNames, ",")); err != nil {
return nil, err
}
diff --git a/api/internal/handler/route/route.go
b/api/internal/handler/route/route.go
index 7edf64e..d69263c 100644
--- a/api/internal/handler/route/route.go
+++ b/api/internal/handler/route/route.go
@@ -1,18 +1,19 @@
package route
import (
- "github.com/apisix/manager-api/internal/core/entity"
- "github.com/apisix/manager-api/internal/core/store"
- "github.com/apisix/manager-api/internal/handler"
- "github.com/apisix/manager-api/internal/utils"
+ "reflect"
+ "strings"
+
"github.com/gin-gonic/gin"
"github.com/shiningrush/droplet"
"github.com/shiningrush/droplet/data"
"github.com/shiningrush/droplet/wrapper"
wgin "github.com/shiningrush/droplet/wrapper/gin"
- "strings"
- "reflect"
+ "github.com/apisix/manager-api/internal/core/entity"
+ "github.com/apisix/manager-api/internal/core/store"
+ "github.com/apisix/manager-api/internal/handler"
+ "github.com/apisix/manager-api/internal/utils"
)
type Handler struct {
diff --git a/api/route/base.go b/api/route/base.go
index 7d8bb45..b9fe141 100644
--- a/api/route/base.go
+++ b/api/route/base.go
@@ -17,15 +17,16 @@
package route
import (
- "github.com/apisix/manager-api/filter"
- "github.com/apisix/manager-api/internal/handler"
- "github.com/apisix/manager-api/internal/handler/route"
"github.com/gin-contrib/pprof"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
"github.com/apisix/manager-api/conf"
+ "github.com/apisix/manager-api/filter"
+ "github.com/apisix/manager-api/internal/handler"
+ "github.com/apisix/manager-api/internal/handler/consumer"
+ "github.com/apisix/manager-api/internal/handler/route"
)
func SetUpRouter() *gin.Engine {
@@ -45,11 +46,12 @@ func SetUpRouter() *gin.Engine {
AppendSsl(r)
AppendPlugin(r)
AppendUpstream(r)
- AppendConsumer(r)
+ //AppendConsumer(r)
AppendRouteGroup(r)
factories := []handler.RegisterFactory{
route.NewHandler,
+ consumer.NewHandler,
}
for i := range factories {
h, err := factories[i]()