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 2021/10/20 13:06:34 UTC

[GitHub] [apisix-dashboard] starsz commented on a change in pull request #2104: feat: supports stream route API

starsz commented on a change in pull request #2104:
URL: https://github.com/apache/apisix-dashboard/pull/2104#discussion_r732749602



##########
File path: api/internal/handler/stream_route/stream_route.go
##########
@@ -0,0 +1,184 @@
+/*
+ * 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 stream_route
+
+import (
+	"fmt"
+	"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"
+	"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"
+	"net/http"
+	"reflect"
+	"strings"
+)
+
+type Handler struct {
+	streamRouteStore store.Interface
+	upstreamStore    store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+	return &Handler{
+		streamRouteStore: store.GetStore(store.HubKeyStreamRoute),
+		upstreamStore:    store.GetStore(store.HubKeyUpstream),
+	}, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+	r.GET("/apisix/admin/stream_routes/:id", wgin.Wraps(h.Get,
+		wrapper.InputType(reflect.TypeOf(GetInput{}))))
+	r.GET("/apisix/admin/stream_routes", wgin.Wraps(h.List,
+		wrapper.InputType(reflect.TypeOf(ListInput{}))))
+	r.POST("/apisix/admin/stream_routes", wgin.Wraps(h.Create,
+		wrapper.InputType(reflect.TypeOf(entity.StreamRoute{}))))
+	r.PUT("/apisix/admin/stream_routes", wgin.Wraps(h.Update,
+		wrapper.InputType(reflect.TypeOf(UpdateInput{}))))

Review comment:
       We can use `POST` to create a stream route. Right?
   
   Refer: http://apisix.apache.org/docs/apisix/admin-api#stream-route

##########
File path: api/internal/core/entity/entity.go
##########
@@ -290,3 +290,16 @@ type Proto struct {
 	Desc    string `json:"desc,omitempty"`
 	Content string `json:"content"`
 }
+
+// swagger:model StreamRoute
+type StreamRoute struct {
+	BaseInfo
+	Desc       string                 `json:"desc,omitempty"`
+	RemoteAddr string                 `json:"remote_addr,omitempty"`
+	ServerAddr string                 `json:"server_addr,omitempty"`
+	ServerPort int                    `json:"server_port,omitempty"`
+	Sni        string                 `json:"sni,omitempty"`

Review comment:
       ```suggestion
   	SNI        string                 `json:"sni,omitempty"`
   ```

##########
File path: api/test/e2enew/base/base.go
##########
@@ -81,6 +82,19 @@ func APISIXExpect() *httpexpect.Expect {
 	return httpexpect.New(t, APISIXHost)
 }
 
+func APISIXStreamProxyExpect(port uint16, isHTTPS bool) *httpexpect.Expect {
+	if port == 0 {
+		port = 10090
+	}
+	t := getTestingHandle()
+
+	if isHTTPS {
+		return httpexpect.New(t, "https://" + net.JoinHostPort("127.0.0.1", strconv.Itoa(int(port))))
+	} else {
+		return httpexpect.New(t, "http://" + net.JoinHostPort("127.0.0.1", strconv.Itoa(int(port))))
+	}
+}

Review comment:
       Better to add it. It's important.

##########
File path: api/internal/handler/stream_route/stream_route.go
##########
@@ -0,0 +1,186 @@
+/*
+ * 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 stream_route
+
+import (
+	"fmt"
+	"net/http"
+	"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"
+
+	"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 {
+	streamRouteStore store.Interface
+	upstreamStore    store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+	return &Handler{
+		streamRouteStore: store.GetStore(store.HubKeyStreamRoute),
+		upstreamStore:    store.GetStore(store.HubKeyUpstream),
+	}, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+	r.GET("/apisix/admin/stream_routes/:id", wgin.Wraps(h.Get,
+		wrapper.InputType(reflect.TypeOf(GetInput{}))))
+	r.GET("/apisix/admin/stream_routes", wgin.Wraps(h.List,
+		wrapper.InputType(reflect.TypeOf(ListInput{}))))
+	r.POST("/apisix/admin/stream_routes", wgin.Wraps(h.Create,
+		wrapper.InputType(reflect.TypeOf(entity.StreamRoute{}))))
+	r.PUT("/apisix/admin/stream_routes", wgin.Wraps(h.Update,
+		wrapper.InputType(reflect.TypeOf(UpdateInput{}))))
+	r.PUT("/apisix/admin/stream_routes/:id", wgin.Wraps(h.Update,
+		wrapper.InputType(reflect.TypeOf(UpdateInput{}))))
+	r.DELETE("/apisix/admin/stream_routes/:ids", 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)
+	streamRoute, err := h.streamRouteStore.Get(c.Context(), input.ID)
+	if err != nil {
+		return handler.SpecCodeResponse(err), err
+	}
+	return streamRoute, nil
+}
+
+type ListInput struct {
+	RemoteAddr string `auto_read:"remote_addr,query"`
+	ServerAddr string `auto_read:"server_addr,query"`
+	ServerPort int    `auto_read:"server_port,query"`
+	Sni        string `auto_read:"sni,query"`
+	store.Pagination
+}
+
+func (h *Handler) List(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*ListInput)
+	ret, err := h.streamRouteStore.List(c.Context(), store.ListInput{
+		Predicate: func(obj interface{}) bool {
+			if input.RemoteAddr != "" && !strings.Contains(obj.(*entity.StreamRoute).RemoteAddr, input.RemoteAddr) {
+				return false
+			}
+
+			if input.ServerAddr != "" && !strings.Contains(obj.(*entity.StreamRoute).ServerAddr, input.ServerAddr) {
+				return false
+			}
+
+			if input.ServerPort != 0 && obj.(*entity.StreamRoute).ServerPort != input.ServerPort {
+				return false
+			}
+
+			if input.Sni != "" && strings.Contains(obj.(*entity.StreamRoute).Sni, input.Sni) {

Review comment:
       ```suggestion
   			if input.Sni != "" && !strings.Contains(obj.(*entity.StreamRoute).Sni, input.Sni) {
   ```

##########
File path: api/internal/handler/stream_route/stream_route.go
##########
@@ -0,0 +1,186 @@
+/*
+ * 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 stream_route
+
+import (
+	"fmt"
+	"net/http"
+	"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"
+
+	"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 {
+	streamRouteStore store.Interface
+	upstreamStore    store.Interface
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+	return &Handler{
+		streamRouteStore: store.GetStore(store.HubKeyStreamRoute),
+		upstreamStore:    store.GetStore(store.HubKeyUpstream),
+	}, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+	r.GET("/apisix/admin/stream_routes/:id", wgin.Wraps(h.Get,
+		wrapper.InputType(reflect.TypeOf(GetInput{}))))
+	r.GET("/apisix/admin/stream_routes", wgin.Wraps(h.List,
+		wrapper.InputType(reflect.TypeOf(ListInput{}))))
+	r.POST("/apisix/admin/stream_routes", wgin.Wraps(h.Create,
+		wrapper.InputType(reflect.TypeOf(entity.StreamRoute{}))))
+	r.PUT("/apisix/admin/stream_routes", wgin.Wraps(h.Update,
+		wrapper.InputType(reflect.TypeOf(UpdateInput{}))))
+	r.PUT("/apisix/admin/stream_routes/:id", wgin.Wraps(h.Update,
+		wrapper.InputType(reflect.TypeOf(UpdateInput{}))))
+	r.DELETE("/apisix/admin/stream_routes/:ids", 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)
+	streamRoute, err := h.streamRouteStore.Get(c.Context(), input.ID)
+	if err != nil {
+		return handler.SpecCodeResponse(err), err
+	}
+	return streamRoute, nil
+}
+
+type ListInput struct {
+	RemoteAddr string `auto_read:"remote_addr,query"`
+	ServerAddr string `auto_read:"server_addr,query"`
+	ServerPort int    `auto_read:"server_port,query"`
+	Sni        string `auto_read:"sni,query"`
+	store.Pagination
+}
+
+func (h *Handler) List(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*ListInput)
+	ret, err := h.streamRouteStore.List(c.Context(), store.ListInput{
+		Predicate: func(obj interface{}) bool {
+			if input.RemoteAddr != "" && !strings.Contains(obj.(*entity.StreamRoute).RemoteAddr, input.RemoteAddr) {
+				return false
+			}
+
+			if input.ServerAddr != "" && !strings.Contains(obj.(*entity.StreamRoute).ServerAddr, input.ServerAddr) {
+				return false
+			}
+
+			if input.ServerPort != 0 && obj.(*entity.StreamRoute).ServerPort != input.ServerPort {
+				return false
+			}
+
+			if input.Sni != "" && strings.Contains(obj.(*entity.StreamRoute).Sni, input.Sni) {
+				return false
+			}
+
+			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) {
+	streamRoute := c.Input().(*entity.StreamRoute)
+	if streamRoute.UpstreamID != nil {
+		upstreamID := utils.InterfaceToString(streamRoute.UpstreamID)
+		_, err := h.upstreamStore.Get(c.Context(), upstreamID)
+		if err != nil {
+			if err == data.ErrNotFound {
+				return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest},
+					fmt.Errorf("upstream id: %s not found", streamRoute.UpstreamID)
+			}
+			return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest}, err

Review comment:
       ```suggestion
   			return handler.SpecCodeResponse(err), 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.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

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