This is an automated email from the ASF dual-hosted git repository.

baze pushed a commit to branch 1.4
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git

commit a7174c3922ca3deb2a8c5515ea5468efd7aaac21
Author: Patrick <[email protected]>
AuthorDate: Tue Jul 28 21:49:59 2020 +0800

    let go_restful_server support same url and different methodType
---
 .../rest/server/server_impl/go_restful_server.go   | 24 ++++-----
 .../server/server_impl/go_restful_server_test.go   | 57 ++++++++++++++++++++++
 2 files changed, 70 insertions(+), 11 deletions(-)

diff --git a/protocol/rest/server/server_impl/go_restful_server.go 
b/protocol/rest/server/server_impl/go_restful_server.go
index c7d971f..6fb9ee8 100644
--- a/protocol/rest/server/server_impl/go_restful_server.go
+++ b/protocol/rest/server/server_impl/go_restful_server.go
@@ -48,8 +48,8 @@ var filterSlice []restful.FilterFunction
 
 // GoRestfulServer a rest server implement by go-restful
 type GoRestfulServer struct {
-       srv       *http.Server
-       container *restful.Container
+       srv *http.Server
+       ws  *restful.WebService
 }
 
 // NewGoRestfulServer a constructor of GoRestfulServer
@@ -60,13 +60,17 @@ func NewGoRestfulServer() server.RestServer {
 // Start go-restful server
 // It will add all go-restful filters
 func (grs *GoRestfulServer) Start(url common.URL) {
-       grs.container = restful.NewContainer()
+       container := restful.NewContainer()
        for _, filter := range filterSlice {
-               grs.container.Filter(filter)
+               container.Filter(filter)
        }
        grs.srv = &http.Server{
-               Handler: grs.container,
+               Handler: container,
        }
+       grs.ws = &restful.WebService{}
+       grs.ws.Path("/")
+       grs.ws.SetDynamicRoutes(true)
+       container.Add(grs.ws)
        ln, err := net.Listen("tcp", url.Location)
        if err != nil {
                panic(perrors.New(fmt.Sprintf("Restful Server start error:%v", 
err)))
@@ -83,23 +87,21 @@ func (grs *GoRestfulServer) Start(url common.URL) {
 // Publish a http api in go-restful server
 // The routeFunc should be invoked when the server receive a request
 func (grs *GoRestfulServer) Deploy(restMethodConfig *config.RestMethodConfig, 
routeFunc func(request server.RestServerRequest, response 
server.RestServerResponse)) {
-       ws := &restful.WebService{}
+
        rf := func(req *restful.Request, resp *restful.Response) {
                routeFunc(NewGoRestfulRequestAdapter(req), resp)
        }
-       ws.Path(restMethodConfig.Path).
+       grs.ws.Route(grs.ws.Method(restMethodConfig.MethodType).
                Produces(strings.Split(restMethodConfig.Produces, ",")...).
                Consumes(strings.Split(restMethodConfig.Consumes, ",")...).
-               Route(ws.Method(restMethodConfig.MethodType).To(rf))
-       grs.container.Add(ws)
-
+               Path(restMethodConfig.Path).To(rf))
 }
 
 // Delete a http api in go-restful server
 func (grs *GoRestfulServer) UnDeploy(restMethodConfig 
*config.RestMethodConfig) {
        ws := new(restful.WebService)
        ws.Path(restMethodConfig.Path)
-       err := grs.container.Remove(ws)
+       err := grs.ws.RemoveRoute(restMethodConfig.Path, 
restMethodConfig.MethodType)
        if err != nil {
                logger.Warnf("[Go restful] Remove web service error:%v", err)
        }
diff --git a/protocol/rest/server/server_impl/go_restful_server_test.go 
b/protocol/rest/server/server_impl/go_restful_server_test.go
new file mode 100644
index 0000000..29a9ef8
--- /dev/null
+++ b/protocol/rest/server/server_impl/go_restful_server_test.go
@@ -0,0 +1,57 @@
+/*
+ * 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 server_impl
+
+import (
+       "testing"
+)
+
+import (
+       "github.com/stretchr/testify/assert"
+)
+
+import (
+       "github.com/apache/dubbo-go/common"
+       "github.com/apache/dubbo-go/protocol/rest/config"
+       "github.com/apache/dubbo-go/protocol/rest/server"
+)
+
+func TestGoRestfulServerDeploySameUrl(t *testing.T) {
+       grs := NewGoRestfulServer()
+       url, err := common.NewURL("http://127.0.0.1:43121";)
+       assert.NoError(t, err)
+       grs.Start(url)
+       rmc := &config.RestMethodConfig{
+               Produces:       "*/*",
+               Consumes:       "*/*",
+               MethodType:     "POST",
+               Path: "/test",
+       }
+       f := func(request server.RestServerRequest, response 
server.RestServerResponse) {}
+       grs.Deploy(rmc, f)
+       rmc1 := &config.RestMethodConfig{
+               Produces:       "*/*",
+               Consumes:       "*/*",
+               MethodType:     "GET",
+               Path: "/test",
+       }
+       grs.Deploy(rmc1, f)
+       grs.UnDeploy(rmc)
+       grs.UnDeploy(rmc1)
+       grs.Destroy()
+}

Reply via email to