You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2020/09/21 09:54:24 UTC

[GitHub] [apisix-dashboard] nic-chen opened a new pull request #497: feat: add Service and Upstream refactoring

nic-chen opened a new pull request #497:
URL: https://github.com/apache/apisix-dashboard/pull/497


   Please answer these questions before submitting a pull request
   
   - Why submit this pull request?
   
   add Service and Upstream refactoring
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix-dashboard] nic-chen commented on a change in pull request #497: feat: add Service and Upstream refactoring

Posted by GitBox <gi...@apache.org>.
nic-chen commented on a change in pull request #497:
URL: https://github.com/apache/apisix-dashboard/pull/497#discussion_r492178489



##########
File path: api/route/base.go
##########
@@ -44,16 +46,18 @@ func SetUpRouter() *gin.Engine {
 	AppendHealthCheck(r)
 	AppendAuthentication(r)
 	//AppendRoute(r)
-	AppendSsl(r)
+	//AppendSsl(r)
 	AppendPlugin(r)
-	AppendUpstream(r)
+	//AppendUpstream(r)

Review comment:
       we need them to debug or compare.will remove them once when complete the refactoring work. 
    




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix-dashboard] ShiningRush commented on a change in pull request #497: feat: add Service and Upstream refactoring

Posted by GitBox <gi...@apache.org>.
ShiningRush commented on a change in pull request #497:
URL: https://github.com/apache/apisix-dashboard/pull/497#discussion_r492439842



##########
File path: api/internal/handler/service/service.go
##########
@@ -0,0 +1,189 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package service
+
+import (
+	"reflect"
+	"strings"
+
+	"github.com/api7/go-jsonpatch"
+	"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"
+
+	"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 {
+	serviceStore *store.GenericStore
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+	s, err := store.NewGenericStore(store.GenericStoreOption{
+		BasePath: "/apisix/services",
+		ObjType:  reflect.TypeOf(entity.Service{}),
+		KeyFunc: func(obj interface{}) string {
+			r := obj.(*entity.Service)
+			return r.ID
+		},
+	})
+	if err != nil {
+		return nil, err
+	}
+	if err := s.Init(); err != nil {
+		return nil, err
+	}
+
+	utils.AppendToClosers(s.Close)
+	return &Handler{
+		serviceStore: s,
+	}, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+	r.GET("/apisix/admin/services/:id", wgin.Wraps(h.Get,
+		wrapper.InputType(reflect.TypeOf(GetInput{}))))
+	r.GET("/apisix/admin/services", wgin.Wraps(h.List,
+		wrapper.InputType(reflect.TypeOf(ListInput{}))))
+	r.POST("/apisix/admin/services", wgin.Wraps(h.Create,
+		wrapper.InputType(reflect.TypeOf(entity.Service{}))))
+	r.PUT("/apisix/admin/services/:id", wgin.Wraps(h.Update,
+		wrapper.InputType(reflect.TypeOf(UpdateInput{}))))
+	r.DELETE("/apisix/admin/services", wgin.Wraps(h.BatchDelete,
+		wrapper.InputType(reflect.TypeOf(BatchDelete{}))))
+}
+
+type GetInput struct {
+	ID string `auto_read:"id,path" validate:"required"`
+}
+
+func (h *Handler) Get(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*GetInput)
+
+	r, err := h.serviceStore.Get(input.ID)
+	if err != nil {
+		return nil, err
+	}
+	return r, nil
+}
+
+type ListInput struct {
+	ID string `auto_read:"id,query"`
+	data.Pager
+}
+
+func (h *Handler) List(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*ListInput)
+
+	ret, err := h.serviceStore.List(store.ListInput{
+		Predicate: func(obj interface{}) bool {
+			return true
+		},
+		PageSize:   input.PageSize,
+		PageNumber: input.PageNumber,
+	})
+	if err != nil {
+		return nil, err
+	}
+
+	return ret, nil
+}
+
+func (h *Handler) Create(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*entity.Service)
+
+	if err := h.serviceStore.Create(c.Context(), input); err != nil {
+		return nil, err
+	}
+
+	return nil, nil
+}
+
+type UpdateInput struct {
+	ID string `auto_read:"id,path"`
+	entity.Service
+}
+
+func (h *Handler) Update(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*UpdateInput)
+	input.Service.ID = input.ID
+
+	if err := h.serviceStore.Update(c.Context(), &input.Service); err != nil {
+		return nil, err
+	}
+
+	return nil, nil
+}
+
+type BatchDelete struct {
+	IDs string `auto_read:"ids,query"`
+}
+
+func (h *Handler) BatchDelete(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*BatchDelete)
+
+	if err := h.serviceStore.BatchDelete(c.Context(), strings.Split(input.IDs, ",")); err != nil {
+		return nil, err
+	}
+
+	return nil, nil
+}
+
+func (h *Handler) Patch(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*UpdateInput)
+	arr := strings.Split(input.ID, "/")
+	var subPath string
+	if len(arr) > 1 {
+		input.ID = arr[0]
+		subPath = arr[1]
+	}
+
+	stored, err := h.serviceStore.Get(input.ID)
+	if err != nil {
+		return nil, err
+	}
+
+	var patch jsonpatch.Patch
+	if subPath != "" {
+		patch = jsonpatch.Patch{
+			Operations: []jsonpatch.PatchOperation{
+				{Op: jsonpatch.Replace, Path: subPath, Value: c.Input()},
+			},
+		}
+	} else {
+		patch, err = jsonpatch.MakePatch(stored, input.Service)
+		if err != nil {
+			panic(err)

Review comment:
       panic is not a good practice, please using `return nil, fmt.Errorf("make json patch failed:%w", err)`

##########
File path: api/internal/handler/service/service.go
##########
@@ -0,0 +1,189 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package service
+
+import (
+	"reflect"
+	"strings"
+
+	"github.com/api7/go-jsonpatch"
+	"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"
+
+	"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 {
+	serviceStore *store.GenericStore
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+	s, err := store.NewGenericStore(store.GenericStoreOption{
+		BasePath: "/apisix/services",
+		ObjType:  reflect.TypeOf(entity.Service{}),
+		KeyFunc: func(obj interface{}) string {
+			r := obj.(*entity.Service)
+			return r.ID
+		},
+	})
+	if err != nil {
+		return nil, err
+	}
+	if err := s.Init(); err != nil {
+		return nil, err
+	}
+
+	utils.AppendToClosers(s.Close)
+	return &Handler{
+		serviceStore: s,
+	}, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+	r.GET("/apisix/admin/services/:id", wgin.Wraps(h.Get,
+		wrapper.InputType(reflect.TypeOf(GetInput{}))))
+	r.GET("/apisix/admin/services", wgin.Wraps(h.List,
+		wrapper.InputType(reflect.TypeOf(ListInput{}))))
+	r.POST("/apisix/admin/services", wgin.Wraps(h.Create,
+		wrapper.InputType(reflect.TypeOf(entity.Service{}))))
+	r.PUT("/apisix/admin/services/:id", wgin.Wraps(h.Update,
+		wrapper.InputType(reflect.TypeOf(UpdateInput{}))))
+	r.DELETE("/apisix/admin/services", wgin.Wraps(h.BatchDelete,
+		wrapper.InputType(reflect.TypeOf(BatchDelete{}))))
+}
+
+type GetInput struct {
+	ID string `auto_read:"id,path" validate:"required"`
+}
+
+func (h *Handler) Get(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*GetInput)
+
+	r, err := h.serviceStore.Get(input.ID)
+	if err != nil {
+		return nil, err
+	}
+	return r, nil
+}
+
+type ListInput struct {
+	ID string `auto_read:"id,query"`
+	data.Pager
+}
+
+func (h *Handler) List(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*ListInput)
+
+	ret, err := h.serviceStore.List(store.ListInput{
+		Predicate: func(obj interface{}) bool {
+			return true
+		},
+		PageSize:   input.PageSize,
+		PageNumber: input.PageNumber,
+	})
+	if err != nil {
+		return nil, err
+	}
+
+	return ret, nil
+}
+
+func (h *Handler) Create(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*entity.Service)
+
+	if err := h.serviceStore.Create(c.Context(), input); err != nil {
+		return nil, err
+	}
+
+	return nil, nil
+}
+
+type UpdateInput struct {
+	ID string `auto_read:"id,path"`
+	entity.Service
+}
+
+func (h *Handler) Update(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*UpdateInput)
+	input.Service.ID = input.ID
+
+	if err := h.serviceStore.Update(c.Context(), &input.Service); err != nil {
+		return nil, err
+	}
+
+	return nil, nil
+}
+
+type BatchDelete struct {
+	IDs string `auto_read:"ids,query"`
+}
+
+func (h *Handler) BatchDelete(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*BatchDelete)
+
+	if err := h.serviceStore.BatchDelete(c.Context(), strings.Split(input.IDs, ",")); err != nil {
+		return nil, err
+	}
+
+	return nil, nil
+}
+
+func (h *Handler) Patch(c droplet.Context) (interface{}, error) {

Review comment:
       Missing route applying

##########
File path: api/internal/handler/upstream/upstream.go
##########
@@ -0,0 +1,192 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package upstream
+
+import (
+	"reflect"
+	"strings"
+
+	"github.com/api7/go-jsonpatch"
+	"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"
+
+	"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 {
+	upstreamStore *store.GenericStore
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+	s, err := store.NewGenericStore(store.GenericStoreOption{
+		BasePath: "/apisix/upstreams",
+		ObjType:  reflect.TypeOf(entity.Upstream{}),
+		KeyFunc: func(obj interface{}) string {
+			r := obj.(*entity.Upstream)
+			return r.ID
+		},
+	})
+	if err != nil {
+		return nil, err
+	}
+	if err := s.Init(); err != nil {
+		return nil, err
+	}
+
+	utils.AppendToClosers(s.Close)
+	return &Handler{
+		upstreamStore: s,
+	}, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+	r.GET("/apisix/admin/upstreams/:id", wgin.Wraps(h.Get,
+		wrapper.InputType(reflect.TypeOf(GetInput{}))))
+	r.GET("/apisix/admin/upstreams", wgin.Wraps(h.List,
+		wrapper.InputType(reflect.TypeOf(ListInput{}))))
+	r.POST("/apisix/admin/upstreams", wgin.Wraps(h.Create,
+		wrapper.InputType(reflect.TypeOf(entity.Upstream{}))))
+	r.PUT("/apisix/admin/upstreams/:id", wgin.Wraps(h.Update,
+		wrapper.InputType(reflect.TypeOf(UpdateInput{}))))
+	r.DELETE("/apisix/admin/upstreams", wgin.Wraps(h.BatchDelete,
+		wrapper.InputType(reflect.TypeOf(BatchDelete{}))))
+}
+
+type GetInput struct {
+	ID string `auto_read:"id,path" validate:"required"`
+}
+
+func (h *Handler) Get(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*GetInput)
+
+	r, err := h.upstreamStore.Get(input.ID)
+	if err != nil {
+		return nil, err
+	}
+	return r, nil
+}
+
+type ListInput struct {
+	ID string `auto_read:"id,query"`
+	data.Pager
+}
+
+func (h *Handler) List(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*ListInput)
+
+	ret, err := h.upstreamStore.List(store.ListInput{
+		Predicate: func(obj interface{}) bool {
+			if input.ID != "" {
+				return strings.Index(obj.(*entity.Upstream).ID, input.ID) > 0
+			}
+			return true
+		},
+		PageSize:   input.PageSize,
+		PageNumber: input.PageNumber,
+	})
+	if err != nil {
+		return nil, err
+	}
+
+	return ret, nil
+}
+
+func (h *Handler) Create(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*entity.Upstream)
+
+	if err := h.upstreamStore.Create(c.Context(), input); err != nil {
+		return nil, err
+	}
+
+	return nil, nil
+}
+
+type UpdateInput struct {
+	ID string `auto_read:"id,path"`
+	entity.Upstream
+}
+
+func (h *Handler) Update(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*UpdateInput)
+	input.Upstream.ID = input.ID
+
+	if err := h.upstreamStore.Update(c.Context(), &input.Upstream); err != nil {
+		return nil, err
+	}
+
+	return nil, nil
+}
+
+type BatchDelete struct {
+	IDs string `auto_read:"ids,query"`
+}
+
+func (h *Handler) BatchDelete(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*BatchDelete)
+
+	if err := h.upstreamStore.BatchDelete(c.Context(), strings.Split(input.IDs, ",")); err != nil {
+		return nil, err
+	}
+
+	return nil, nil
+}
+
+func (h *Handler) Patch(c droplet.Context) (interface{}, error) {

Review comment:
       Missing route applying

##########
File path: api/internal/handler/upstream/upstream.go
##########
@@ -0,0 +1,192 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package upstream
+
+import (
+	"reflect"
+	"strings"
+
+	"github.com/api7/go-jsonpatch"
+	"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"
+
+	"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 {
+	upstreamStore *store.GenericStore
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+	s, err := store.NewGenericStore(store.GenericStoreOption{
+		BasePath: "/apisix/upstreams",
+		ObjType:  reflect.TypeOf(entity.Upstream{}),
+		KeyFunc: func(obj interface{}) string {
+			r := obj.(*entity.Upstream)
+			return r.ID
+		},
+	})
+	if err != nil {
+		return nil, err
+	}
+	if err := s.Init(); err != nil {
+		return nil, err
+	}
+
+	utils.AppendToClosers(s.Close)
+	return &Handler{
+		upstreamStore: s,
+	}, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+	r.GET("/apisix/admin/upstreams/:id", wgin.Wraps(h.Get,
+		wrapper.InputType(reflect.TypeOf(GetInput{}))))
+	r.GET("/apisix/admin/upstreams", wgin.Wraps(h.List,
+		wrapper.InputType(reflect.TypeOf(ListInput{}))))
+	r.POST("/apisix/admin/upstreams", wgin.Wraps(h.Create,
+		wrapper.InputType(reflect.TypeOf(entity.Upstream{}))))
+	r.PUT("/apisix/admin/upstreams/:id", wgin.Wraps(h.Update,
+		wrapper.InputType(reflect.TypeOf(UpdateInput{}))))
+	r.DELETE("/apisix/admin/upstreams", wgin.Wraps(h.BatchDelete,
+		wrapper.InputType(reflect.TypeOf(BatchDelete{}))))
+}
+
+type GetInput struct {
+	ID string `auto_read:"id,path" validate:"required"`
+}
+
+func (h *Handler) Get(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*GetInput)
+
+	r, err := h.upstreamStore.Get(input.ID)
+	if err != nil {
+		return nil, err
+	}
+	return r, nil
+}
+
+type ListInput struct {
+	ID string `auto_read:"id,query"`
+	data.Pager
+}
+
+func (h *Handler) List(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*ListInput)
+
+	ret, err := h.upstreamStore.List(store.ListInput{
+		Predicate: func(obj interface{}) bool {
+			if input.ID != "" {
+				return strings.Index(obj.(*entity.Upstream).ID, input.ID) > 0
+			}
+			return true
+		},
+		PageSize:   input.PageSize,
+		PageNumber: input.PageNumber,
+	})
+	if err != nil {
+		return nil, err
+	}
+
+	return ret, nil
+}
+
+func (h *Handler) Create(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*entity.Upstream)
+
+	if err := h.upstreamStore.Create(c.Context(), input); err != nil {
+		return nil, err
+	}
+
+	return nil, nil
+}
+
+type UpdateInput struct {
+	ID string `auto_read:"id,path"`
+	entity.Upstream
+}
+
+func (h *Handler) Update(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*UpdateInput)
+	input.Upstream.ID = input.ID
+
+	if err := h.upstreamStore.Update(c.Context(), &input.Upstream); err != nil {
+		return nil, err
+	}
+
+	return nil, nil
+}
+
+type BatchDelete struct {
+	IDs string `auto_read:"ids,query"`
+}
+
+func (h *Handler) BatchDelete(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*BatchDelete)
+
+	if err := h.upstreamStore.BatchDelete(c.Context(), strings.Split(input.IDs, ",")); err != nil {
+		return nil, err
+	}
+
+	return nil, nil
+}
+
+func (h *Handler) Patch(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*UpdateInput)
+	arr := strings.Split(input.ID, "/")
+	var subPath string
+	if len(arr) > 1 {
+		input.ID = arr[0]
+		subPath = arr[1]
+	}
+
+	stored, err := h.upstreamStore.Get(input.ID)
+	if err != nil {
+		return nil, err
+	}
+
+	var patch jsonpatch.Patch

Review comment:
       Hmm, all patch method looks similar, we should extract common logic to avoid repeat code.

##########
File path: api/internal/handler/service/service.go
##########
@@ -0,0 +1,189 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package service
+
+import (
+	"reflect"
+	"strings"
+
+	"github.com/api7/go-jsonpatch"
+	"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"
+
+	"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 {
+	serviceStore *store.GenericStore
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+	s, err := store.NewGenericStore(store.GenericStoreOption{
+		BasePath: "/apisix/services",
+		ObjType:  reflect.TypeOf(entity.Service{}),
+		KeyFunc: func(obj interface{}) string {
+			r := obj.(*entity.Service)
+			return r.ID
+		},
+	})
+	if err != nil {
+		return nil, err
+	}
+	if err := s.Init(); err != nil {
+		return nil, err
+	}
+
+	utils.AppendToClosers(s.Close)
+	return &Handler{
+		serviceStore: s,
+	}, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+	r.GET("/apisix/admin/services/:id", wgin.Wraps(h.Get,
+		wrapper.InputType(reflect.TypeOf(GetInput{}))))
+	r.GET("/apisix/admin/services", wgin.Wraps(h.List,
+		wrapper.InputType(reflect.TypeOf(ListInput{}))))
+	r.POST("/apisix/admin/services", wgin.Wraps(h.Create,
+		wrapper.InputType(reflect.TypeOf(entity.Service{}))))
+	r.PUT("/apisix/admin/services/:id", wgin.Wraps(h.Update,
+		wrapper.InputType(reflect.TypeOf(UpdateInput{}))))
+	r.DELETE("/apisix/admin/services", wgin.Wraps(h.BatchDelete,
+		wrapper.InputType(reflect.TypeOf(BatchDelete{}))))
+}
+
+type GetInput struct {
+	ID string `auto_read:"id,path" validate:"required"`
+}
+
+func (h *Handler) Get(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*GetInput)
+
+	r, err := h.serviceStore.Get(input.ID)
+	if err != nil {
+		return nil, err
+	}
+	return r, nil
+}
+
+type ListInput struct {
+	ID string `auto_read:"id,query"`
+	data.Pager
+}
+
+func (h *Handler) List(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*ListInput)
+
+	ret, err := h.serviceStore.List(store.ListInput{
+		Predicate: func(obj interface{}) bool {
+			return true
+		},
+		PageSize:   input.PageSize,
+		PageNumber: input.PageNumber,
+	})
+	if err != nil {
+		return nil, err
+	}
+
+	return ret, nil
+}
+
+func (h *Handler) Create(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*entity.Service)
+
+	if err := h.serviceStore.Create(c.Context(), input); err != nil {
+		return nil, err
+	}
+
+	return nil, nil
+}
+
+type UpdateInput struct {
+	ID string `auto_read:"id,path"`
+	entity.Service
+}
+
+func (h *Handler) Update(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*UpdateInput)
+	input.Service.ID = input.ID
+
+	if err := h.serviceStore.Update(c.Context(), &input.Service); err != nil {
+		return nil, err
+	}
+
+	return nil, nil
+}
+
+type BatchDelete struct {
+	IDs string `auto_read:"ids,query"`
+}
+
+func (h *Handler) BatchDelete(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*BatchDelete)
+
+	if err := h.serviceStore.BatchDelete(c.Context(), strings.Split(input.IDs, ",")); err != nil {
+		return nil, err
+	}
+
+	return nil, nil
+}
+
+func (h *Handler) Patch(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*UpdateInput)
+	arr := strings.Split(input.ID, "/")
+	var subPath string
+	if len(arr) > 1 {
+		input.ID = arr[0]
+		subPath = arr[1]
+	}
+
+	stored, err := h.serviceStore.Get(input.ID)
+	if err != nil {
+		return nil, err
+	}
+
+	var patch jsonpatch.Patch
+	if subPath != "" {
+		patch = jsonpatch.Patch{
+			Operations: []jsonpatch.PatchOperation{
+				{Op: jsonpatch.Replace, Path: subPath, Value: c.Input()},
+			},
+		}
+	} else {
+		patch, err = jsonpatch.MakePatch(stored, input.Service)
+		if err != nil {
+			panic(err)
+		}
+	}
+
+	err = patch.Apply(&stored)
+	if err != nil {
+		panic(err)

Review comment:
       `return nil, fmt.Errorf("apply json patch failed:%w", err)`




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix-dashboard] juzhiyuan commented on a change in pull request #497: feat: add Service and Upstream refactoring

Posted by GitBox <gi...@apache.org>.
juzhiyuan commented on a change in pull request #497:
URL: https://github.com/apache/apisix-dashboard/pull/497#discussion_r492106416



##########
File path: api/route/base.go
##########
@@ -44,16 +46,18 @@ func SetUpRouter() *gin.Engine {
 	AppendHealthCheck(r)
 	AppendAuthentication(r)
 	//AppendRoute(r)
-	AppendSsl(r)
+	//AppendSsl(r)
 	AppendPlugin(r)
-	AppendUpstream(r)
+	//AppendUpstream(r)

Review comment:
       Why not remove them directly?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix-dashboard] juzhiyuan commented on a change in pull request #497: feat: add Service and Upstream refactoring

Posted by GitBox <gi...@apache.org>.
juzhiyuan commented on a change in pull request #497:
URL: https://github.com/apache/apisix-dashboard/pull/497#discussion_r492106416



##########
File path: api/route/base.go
##########
@@ -44,16 +46,18 @@ func SetUpRouter() *gin.Engine {
 	AppendHealthCheck(r)
 	AppendAuthentication(r)
 	//AppendRoute(r)
-	AppendSsl(r)
+	//AppendSsl(r)
 	AppendPlugin(r)
-	AppendUpstream(r)
+	//AppendUpstream(r)

Review comment:
       Why not remove them directly?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix-dashboard] juzhiyuan merged pull request #497: feat: add Service and Upstream refactoring

Posted by GitBox <gi...@apache.org>.
juzhiyuan merged pull request #497:
URL: https://github.com/apache/apisix-dashboard/pull/497


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix-dashboard] juzhiyuan commented on pull request #497: feat: add Service and Upstream refactoring

Posted by GitBox <gi...@apache.org>.
juzhiyuan commented on pull request #497:
URL: https://github.com/apache/apisix-dashboard/pull/497#issuecomment-699582983


   cc @nic-chen 


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix-dashboard] ShiningRush commented on pull request #497: feat: add Service and Upstream refactoring

Posted by GitBox <gi...@apache.org>.
ShiningRush commented on pull request #497:
URL: https://github.com/apache/apisix-dashboard/pull/497#issuecomment-699575998


   @nic-chen The pr has merged, we can open a new pr to fix problems.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix-dashboard] nic-chen commented on a change in pull request #497: feat: add Service and Upstream refactoring

Posted by GitBox <gi...@apache.org>.
nic-chen commented on a change in pull request #497:
URL: https://github.com/apache/apisix-dashboard/pull/497#discussion_r492178489



##########
File path: api/route/base.go
##########
@@ -44,16 +46,18 @@ func SetUpRouter() *gin.Engine {
 	AppendHealthCheck(r)
 	AppendAuthentication(r)
 	//AppendRoute(r)
-	AppendSsl(r)
+	//AppendSsl(r)
 	AppendPlugin(r)
-	AppendUpstream(r)
+	//AppendUpstream(r)

Review comment:
       we need them to debug or compare.will remove them once when complete the refactoring work. 
    




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org