Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
2
22a7f0099
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Summer2022
22a7f0099
Commits
a7174c39
Commit
a7174c39
authored
4 years ago
by
Patrick
Browse files
Options
Downloads
Patches
Plain Diff
let go_restful_server support same url and different methodType
parent
ca2538a7
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
protocol/rest/server/server_impl/go_restful_server.go
+13
-11
13 additions, 11 deletions
protocol/rest/server/server_impl/go_restful_server.go
protocol/rest/server/server_impl/go_restful_server_test.go
+57
-0
57 additions, 0 deletions
protocol/rest/server/server_impl/go_restful_server_test.go
with
70 additions
and
11 deletions
protocol/rest/server/server_impl/go_restful_server.go
+
13
−
11
View file @
a7174c39
...
...
@@ -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
)
}
...
...
This diff is collapsed.
Click to expand it.
protocol/rest/server/server_impl/go_restful_server_test.go
0 → 100644
+
57
−
0
View file @
a7174c39
/*
* 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
()
}
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment