Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rest protocol #352

Merged
merged 85 commits into from
Mar 15, 2020
Merged
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
85 commits
Select commit Hold shift + click to select a range
64b9439
init rest protocol
Patrick0308 Jan 11, 2020
5cbba12
rest configs and invoker
Patrick0308 Jan 19, 2020
2d3a6f7
fix
Patrick0308 Jan 21, 2020
69020c0
fixed
Patrick0308 Jan 21, 2020
b3fabce
Merge branch 'develop' into rest_protocol
Patrick0308 Jan 21, 2020
547372f
init rest exporter and modify rest invoker
Patrick0308 Jan 22, 2020
5120254
header params
Patrick0308 Jan 24, 2020
d5fccd6
resty add header params
Patrick0308 Jan 24, 2020
c31030f
fix
Patrick0308 Jan 24, 2020
cbc59d3
go restful server and some unit tests
Patrick0308 Jan 31, 2020
f1908a0
modify
Patrick0308 Jan 31, 2020
567acb1
modify test
Patrick0308 Jan 31, 2020
e899345
Merge pull request #347 from apache/develop
flycash Feb 2, 2020
9acc7aa
Merge branch 'develop' into rest_protocol
Patrick0308 Feb 3, 2020
6a6717f
Merge branch 'feature/rest' into rest_protocol
Patrick0308 Feb 3, 2020
f48a644
fix
Patrick0308 Feb 3, 2020
eb1d850
fix tests
Patrick0308 Feb 3, 2020
756f87f
use "default" constant and remove default timeout options
Patrick0308 Feb 3, 2020
88300f4
add licenses
Patrick0308 Feb 3, 2020
d9c61c4
fmt imports
Patrick0308 Feb 3, 2020
9538a34
fix config initializer
Patrick0308 Feb 4, 2020
f51ae5d
add some unit tests
Patrick0308 Feb 4, 2020
8d1dc02
add testdata about rest config file
Patrick0308 Feb 4, 2020
a957054
add licenses
Patrick0308 Feb 4, 2020
d93a2a3
Merge pull request #329 from Patrick0308/rest_protocol
flycash Feb 5, 2020
114fe40
change service key to path key
Patrick0308 Feb 6, 2020
01361a6
fix serviceKey
Patrick0308 Feb 6, 2020
8b884cc
support req[]
Patrick0308 Feb 6, 2020
fd0a076
fix when args is req[]
Patrick0308 Feb 7, 2020
b67b76a
fix path bug
Patrick0308 Feb 7, 2020
b692796
modify server default consumes and produces
Patrick0308 Feb 8, 2020
bc64aed
Merge pull request #353 from Patrick0308/rest_protocol
flycash Feb 19, 2020
5f90f48
Merge develop
Feb 22, 2020
fd61cc2
Fix review and test
Feb 22, 2020
cd58dfa
Fix review and test
Feb 22, 2020
5af6942
Fix review and test
Feb 22, 2020
eb22fbd
Fix review and test
Feb 22, 2020
b8ac514
Merge pull request #368 from flycash/rest
AlexStocks Feb 22, 2020
a9c5f8f
split import
Patrick0308 Feb 23, 2020
5b76b7c
split import
Patrick0308 Feb 23, 2020
c8a5d15
Merge branch 'rest_protocol' of https://github.com/Patrick0308/dubbo-…
Patrick0308 Feb 23, 2020
4738480
Merge pull request #370 from Patrick0308/rest_protocol
AlexStocks Feb 23, 2020
4e6aa04
add licenses
Patrick0308 Mar 3, 2020
3b46dce
split import
Patrick0308 Mar 3, 2020
5add5e2
remove x/tools
Patrick0308 Mar 3, 2020
4b01d83
Merge pull request #387 from Patrick0308/rest_protocol
flycash Mar 4, 2020
b40361e
fix haven't read error msg
Patrick0308 Mar 6, 2020
182a367
Merge pull request #392 from Patrick0308/rest_protocol
flycash Mar 8, 2020
1829072
modify rest
Patrick0308 Mar 8, 2020
3a46e04
add rest unit tests
Patrick0308 Mar 9, 2020
fa27aa2
modify rest protocol
Patrick0308 Mar 9, 2020
84a2b65
Merge pull request #394 from Patrick0308/rest
flycash Mar 9, 2020
fa5ee68
rest protocol modify
Patrick0308 Mar 9, 2020
f3b5be5
Merge remote-tracking branch 'upstream/develop' into rest
Patrick0308 Mar 10, 2020
47e3150
change yaml unmarshal function to common package
Patrick0308 Mar 10, 2020
f184ee9
modify yaml
Patrick0308 Mar 10, 2020
f8f2b56
Merge pull request #395 from Patrick0308/rest
zouyx Mar 10, 2020
1fd0ddb
modify rest
Patrick0308 Mar 10, 2020
73119b4
Merge pull request #396 from Patrick0308/rest
zouyx Mar 10, 2020
0c55d99
add yaml unit test
Patrick0308 Mar 12, 2020
46af97f
Merge pull request #399 from Patrick0308/rest
zouyx Mar 12, 2020
fea9106
modify rest config
Patrick0308 Mar 14, 2020
c77e8ff
modify rest interface dir
Patrick0308 Mar 14, 2020
f5fdc0e
modify rest
Patrick0308 Mar 14, 2020
007dd79
modify default reader
Patrick0308 Mar 14, 2020
3271f08
modify error info
Patrick0308 Mar 14, 2020
3364d36
modify
Patrick0308 Mar 14, 2020
e9fc1f8
modify
Patrick0308 Mar 14, 2020
dafc2f4
Merge pull request #402 from Patrick0308/rest
flycash Mar 14, 2020
2d8a5cc
Merge remote-tracking branch 'apache/develop' into feature/rest
fangyincheng Mar 14, 2020
a1ff189
fix rest
Patrick0308 Mar 15, 2020
e81095f
add license
Patrick0308 Mar 15, 2020
72fac64
fix test
Patrick0308 Mar 15, 2020
8c53128
Merge pull request #408 from Patrick0308/rest
zouyx Mar 15, 2020
69de204
Ref: refact config loader
fangyincheng Mar 15, 2020
b988571
Merge remote-tracking branch 'apache/develop' into feature/rest
fangyincheng Mar 15, 2020
57a2a8f
Mrg: merge upstream
fangyincheng Mar 15, 2020
66d8a1a
Del: delete config/rest
fangyincheng Mar 15, 2020
4c50edc
Fix: resolve travis
fangyincheng Mar 15, 2020
1e9392a
Imp: improve config reader
fangyincheng Mar 15, 2020
17c29b5
Fix: fix nil point
fangyincheng Mar 15, 2020
d7cc3af
Fix: reviews
fangyincheng Mar 15, 2020
a4c9d8c
Merge pull request #410 from fangyincheng/feature/rest
fangyincheng Mar 15, 2020
639ddef
Fix: fomat import
fangyincheng Mar 15, 2020
7e7dfdb
Mod: rename file name
fangyincheng Mar 15, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions common/constant/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ const (
DEFAULT_FAILBACK_TIMES = "3"
DEFAULT_FAILBACK_TIMES_INT = 3
DEFAULT_FAILBACK_TASKS = 100
DEFAULT_REST_CLIENT = "resty"
DEFAULT_REST_SERVER = "go-restful"
)

const (
Expand Down
43 changes: 29 additions & 14 deletions common/constant/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,34 @@ const (
)

const (
CONSUMER_SIGN_FILTER = "sign"
PROVIDER_AUTH_FILTER = "auth"
SERVICE_AUTH_KEY = "auth"
AUTHENTICATOR_KEY = "authenticator"
DEFAULT_AUTHENTICATOR = "accesskeys"
DEFAULT_ACCESS_KEY_STORAGE = "urlstorage"
ACCESS_KEY_STORAGE_KEY = "accessKey.storage"
REQUEST_TIMESTAMP_KEY = "timestamp"
REQUEST_SIGNATURE_KEY = "signature"
AK_KEY = "ak"
SIGNATURE_STRING_FORMAT = "%s#%s#%s#%s"
// name of consumer sign filter
CONSUMER_SIGN_FILTER = "sign"
// name of consumer sign filter
PROVIDER_AUTH_FILTER = "auth"
// name of service filter
SERVICE_AUTH_KEY = "auth"
// key of authenticator
AUTHENTICATOR_KEY = "authenticator"
// name of default authenticator
DEFAULT_AUTHENTICATOR = "accesskeys"
// name of default url storage
DEFAULT_ACCESS_KEY_STORAGE = "urlstorage"
// key of storage
ACCESS_KEY_STORAGE_KEY = "accessKey.storage"
// key of request timestamp
REQUEST_TIMESTAMP_KEY = "timestamp"
// key of request signature
REQUEST_SIGNATURE_KEY = "signature"
// AK key
AK_KEY = "ak"
// signature format
SIGNATURE_STRING_FORMAT = "%s#%s#%s#%s"
// key whether enable signature
PARAMTER_SIGNATURE_ENABLE_KEY = "param.sign"
CONSUMER = "consumer"
ACCESS_KEY_ID_KEY = "accessKeyId"
SECRET_ACCESS_KEY_KEY = "secretAccessKey"
// consumer
CONSUMER = "consumer"
// key of access key id
ACCESS_KEY_ID_KEY = "accessKeyId"
// key of secret access key
SECRET_ACCESS_KEY_KEY = "secretAccessKey"
)
2 changes: 2 additions & 0 deletions common/constant/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ import (
)

var (
// The value will be 10^6
// 1ms = 10^6ns
MsToNanoRate = int64(time.Millisecond / time.Nanosecond)
)
6 changes: 6 additions & 0 deletions common/extension/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,27 @@ var (
accesskeyStorages = make(map[string]func() filter.AccessKeyStorage)
)

// SetAuthenticator put the fcn into map with name
func SetAuthenticator(name string, fcn func() filter.Authenticator) {
authenticators[name] = fcn
}

// GetAuthenticator find the Authenticator with name
// if not found, it will panic
func GetAuthenticator(name string) filter.Authenticator {
if authenticators[name] == nil {
panic("authenticator for " + name + " is not existing, make sure you have import the package.")
}
return authenticators[name]()
}

// SetAccesskeyStorages will set the fcn into map with this name
func SetAccesskeyStorages(name string, fcn func() filter.AccessKeyStorage) {
accesskeyStorages[name] = fcn
}

// GetAccesskeyStorages find the storage with the name.
// If not found, it will panic.
func GetAccesskeyStorages(name string) filter.AccessKeyStorage {
if accesskeyStorages[name] == nil {
panic("accesskeyStorages for " + name + " is not existing, make sure you have import the package.")
Expand Down
37 changes: 37 additions & 0 deletions common/extension/rest_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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 extension
AlexStocks marked this conversation as resolved.
Show resolved Hide resolved

import (
"github.com/apache/dubbo-go/protocol/rest/rest_interface"
)

var (
restClients = make(map[string]func(restOptions *rest_interface.RestOptions) rest_interface.RestClient)
)

func SetRestClient(name string, fun func(restOptions *rest_interface.RestOptions) rest_interface.RestClient) {
restClients[name] = fun
}

func GetNewRestClient(name string, restOptions *rest_interface.RestOptions) rest_interface.RestClient {
if restClients[name] == nil {
panic("restClient for " + name + " is not existing, make sure you have import the package.")
}
return restClients[name](restOptions)
}
41 changes: 41 additions & 0 deletions common/extension/rest_config_reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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 extension
flycash marked this conversation as resolved.
Show resolved Hide resolved

import (
"github.com/apache/dubbo-go/protocol/rest/rest_interface"
)

var (
restConfigReaders = make(map[string]func() rest_interface.RestConfigReader)
)

func SetRestConfigReader(name string, fun func() rest_interface.RestConfigReader) {
restConfigReaders[name] = fun
}

func GetSingletonRestConfigReader(name string) rest_interface.RestConfigReader {
if name == "" {
name = "default"
}
if restConfigReaders[name] == nil {
panic("restConfigReaders for " + name + " is not existing, make sure you have import the package.")
}
return restConfigReaders[name]()

}
37 changes: 37 additions & 0 deletions common/extension/rest_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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 extension
flycash marked this conversation as resolved.
Show resolved Hide resolved

import (
"github.com/apache/dubbo-go/protocol/rest/rest_interface"
)

var (
restServers = make(map[string]func() rest_interface.RestServer)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

restServers = make(map[string]func() rest_interface.RestServer, 32)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8 is enough, it have one always.

)

func SetRestServer(name string, fun func() rest_interface.RestServer) {
restServers[name] = fun
}

func GetNewRestServer(name string) rest_interface.RestServer {
if restServers[name] == nil {
panic("restServer for " + name + " is not existing, make sure you have import the package.")
}
return restServers[name]()
}
1 change: 1 addition & 0 deletions config/consumer_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type ConsumerConfig struct {
ProtocolConf interface{} `yaml:"protocol_conf" json:"protocol_conf,omitempty" property:"protocol_conf"`
FilterConf interface{} `yaml:"filter_conf" json:"filter_conf,omitempty" property:"filter_conf" `
ShutdownConfig *ShutdownConfig `yaml:"shutdown_conf" json:"shutdown_conf,omitempty" property:"shutdown_conf" `
RestConfigType string `default:"default" yaml:"rest_config_type" json:"rest_config_type,omitempty" property:"rest_config_type"`
}

// UnmarshalYAML ...
Expand Down
1 change: 1 addition & 0 deletions config/provider_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type ProviderConfig struct {
ProtocolConf interface{} `yaml:"protocol_conf" json:"protocol_conf,omitempty" property:"protocol_conf" `
FilterConf interface{} `yaml:"filter_conf" json:"filter_conf,omitempty" property:"filter_conf" `
ShutdownConfig *ShutdownConfig `yaml:"shutdown_conf" json:"shutdown_conf,omitempty" property:"shutdown_conf" `
RestConfigType string `default:"default" yaml:"rest_config_type" json:"rest_config_type,omitempty" property:"rest_config_type"`
}

// UnmarshalYAML ...
Expand Down
2 changes: 1 addition & 1 deletion config_center/nacos/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func newNacosClient(name string, nacosAddrs []string, timeout time.Duration) (*N
},
}

svrConfList := []nacosconst.ServerConfig{}
svrConfList := make([]nacosconst.ServerConfig, 0, len(n.NacosAddrs))
for _, nacosAddr := range n.NacosAddrs {
split := strings.Split(nacosAddr, ":")
port, err := strconv.ParseUint(split[1], 10, 64)
Expand Down
9 changes: 2 additions & 7 deletions config_center/nacos/facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,10 @@ type nacosClientFacade interface {
common.Node
}

func timeSecondDuration(sec int) time.Duration {
return time.Duration(sec) * time.Second
}

// HandleClientRestart Restart client handler
func HandleClientRestart(r nacosClientFacade) {
var (
err error

err error
failTimes int
)

Expand All @@ -79,7 +74,7 @@ LOOP:
case <-r.GetDone():
logger.Warnf("(NacosProviderRegistry)reconnectZkRegistry goroutine exit now...")
break LOOP
case <-getty.GetTimeWheel().After(timeSecondDuration(failTimes * connDelay)): // Prevent crazy reconnection nacos.
case <-getty.GetTimeWheel().After(time.Duration(failTimes*connDelay) * time.Second): // Prevent crazy reconnection nacos.
}
err = ValidateNacosClient(r, WithNacosName(nacosName))
logger.Infof("NacosProviderRegistry.validateNacosClient(nacosAddr{%s}) = error{%#v}",
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ require (
github.com/dubbogo/getty v1.3.2
github.com/dubbogo/go-zookeeper v1.0.0
github.com/dubbogo/gost v1.5.2
github.com/emicklei/go-restful/v3 v3.0.0
github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect
github.com/go-errors/errors v1.0.1 // indirect
github.com/go-resty/resty/v2 v2.1.0
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 // indirect
github.com/golang/mock v1.3.1
github.com/golang/protobuf v1.3.2
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ github.com/duosecurity/duo_api_golang v0.0.0-20190308151101-6c680f768e74 h1:2MIh
github.com/duosecurity/duo_api_golang v0.0.0-20190308151101-6c680f768e74/go.mod h1:UqXY1lYT/ERa4OEAywUqdok1T4RCRdArkhic1Opuavo=
github.com/elazarl/go-bindata-assetfs v0.0.0-20160803192304-e1a2a7ec64b0 h1:ZoRgc53qJCfSLimXqJDrmBhnt5GChDsExMCK7t48o0Y=
github.com/elazarl/go-bindata-assetfs v0.0.0-20160803192304-e1a2a7ec64b0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=
github.com/emicklei/go-restful/v3 v3.0.0 h1:Duxxa4x0WIHW3bYEDmoAPNjmy8Rbqn+utcF74dlF/G8=
github.com/emicklei/go-restful/v3 v3.0.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/envoyproxy/go-control-plane v0.8.0 h1:uE6Fp4fOcAJdc1wTQXLJ+SYistkbG1dNoi6Zs1+Ybvk=
github.com/envoyproxy/go-control-plane v0.8.0/go.mod h1:GSSbY9P1neVhdY7G4wu+IK1rk/dqhiCC/4ExuWJZVuk=
github.com/envoyproxy/protoc-gen-validate v0.0.14 h1:YBW6/cKy9prEGRYLnaGa4IDhzxZhRCtKsax8srGKDnM=
Expand All @@ -138,6 +140,8 @@ github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-ole/go-ole v1.2.1 h1:2lOsA72HgjxAuMlKpFiCbHTvu44PIVkZ5hqm3RSdI/E=
github.com/go-ole/go-ole v1.2.1/go.mod h1:7FAglXiTm7HKlQRDeOQ6ZNUHidzCWXuZWq/1dTyBNF8=
github.com/go-resty/resty/v2 v2.1.0 h1:Z6IefCpUMfnvItVJaJXWv/pMiiD11So35QgwEELsldE=
github.com/go-resty/resty/v2 v2.1.0/go.mod h1:dZGr0i9PLlaaTD4H/hoZIDjQ+r6xq8mgbRzHZf7f2J8=
github.com/go-sql-driver/mysql v0.0.0-20180618115901-749ddf1598b4 h1:1LlmVz15APoKz9dnm5j2ePptburJlwEH+/v/pUuoxck=
github.com/go-sql-driver/mysql v0.0.0-20180618115901-749ddf1598b4/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
Expand Down Expand Up @@ -494,6 +498,8 @@ golang.org/x/net v0.0.0-20190320064053-1272bf9dcd53/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980 h1:dfGZHvZk057jK2MCeWus/TowKpJ8y4AmooUzdBSR9GU=
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190628185345-da137c7871d7 h1:rTIdg5QFRR7XCaK4LCjBiPbx8j4DQRpdYMnGn/bJUEU=
golang.org/x/net v0.0.0-20190628185345-da137c7871d7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/oauth2 v0.0.0-20170807180024-9a379c6b3e95/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be h1:vEDujvNQGv4jgYKudGeI/+DAX4Jffq6hpD55MmoEvKs=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand Down
92 changes: 92 additions & 0 deletions protocol/rest/rest_client/resty_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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 rest_client

import (
"context"
"net"
"net/http"
"path"
"time"
)

import (
perrors "github.com/pkg/errors"
)

import (
"github.com/go-resty/resty/v2"
zouyx marked this conversation as resolved.
Show resolved Hide resolved
)

import (
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/protocol/rest/rest_interface"
)

func init() {
extension.SetRestClient(constant.DEFAULT_REST_CLIENT, GetRestyClient)
}

type RestyClient struct {
client *resty.Client
}

func NewRestyClient(restOption *rest_interface.RestOptions) *RestyClient {
zouyx marked this conversation as resolved.
Show resolved Hide resolved
client := resty.New()
client.SetTransport(
&http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
c, err := net.DialTimeout(network, addr, restOption.ConnectTimeout)
if err != nil {
return nil, err
}
err = c.SetDeadline(time.Now().Add(restOption.RequestTimeout))
if err != nil {
return nil, err
}
return c, nil
},
})
return &RestyClient{
client: client,
}
}

func (rc *RestyClient) Do(restRequest *rest_interface.RestRequest, res interface{}) error {
r, err := rc.client.R().
SetHeader("Content-Type", restRequest.Consumes).
SetHeader("Accept", restRequest.Produces).
SetPathParams(restRequest.PathParams).
SetQueryParams(restRequest.QueryParams).
SetHeaders(restRequest.Headers).
SetBody(restRequest.Body).
SetResult(res).
Execute(restRequest.Method, "http://"+path.Join(restRequest.Location, restRequest.Path))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is http suitable to hard code , what about https?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Https will supported in the future.

if err != nil {
return perrors.WithStack(err)
}
if r.IsError() {
return perrors.New(r.String())
}
return nil
}

func GetRestyClient(restOptions *rest_interface.RestOptions) rest_interface.RestClient {
return NewRestyClient(restOptions)
}
Loading