Skip to content
Snippets Groups Projects
Unverified Commit db1e5daf authored by Xin.Zh's avatar Xin.Zh Committed by GitHub
Browse files

Merge pull request #676 from Patrick0308/rest_methodtype

Fix: GoRestServer can't deploy the HTTP interfaces with same path and different method type
parents 272ddd59 f2261ce1
No related branches found
No related tags found
No related merge requests found
......@@ -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)
}
......
/*
* 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()
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment