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 #329

Merged
merged 27 commits into from
Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions common/constant/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
DEFAULT_FAILBACK_TIMES = "3"
DEFAULT_FAILBACK_TIMES_INT = 3
DEFAULT_FAILBACK_TASKS = 100
DEFAULT_REST_CLIENT = "resty"
)

const (
Expand Down
20 changes: 20 additions & 0 deletions common/extension/rest_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package extension

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

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

func SetRestClientFunc(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)
flycash marked this conversation as resolved.
Show resolved Hide resolved
}
24 changes: 24 additions & 0 deletions common/extension/rest_config_reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package extension

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]()
Patrick0308 marked this conversation as resolved.
Show resolved Hide resolved

}
20 changes: 20 additions & 0 deletions common/extension/rest_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package extension

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

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

func SetRestServerFunc(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 @@ -59,6 +59,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"`
}

func (c *ConsumerConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
Expand Down
1 change: 1 addition & 0 deletions config/provider_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,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"`
}

func (c *ProviderConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ require (
github.com/dubbogo/getty v1.3.2
github.com/dubbogo/gost v1.5.2
github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect
github.com/gin-gonic/gin v1.5.0
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
26 changes: 26 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ github.com/fatih/structs v0.0.0-20180123065059-ebf56d35bba7/go.mod h1:9NiDSp5zOc
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.5.0 h1:fi+bqFAx/oLK54somfCtEZs9HeH1LHVoEPUgARpTqyc=
github.com/gin-gonic/gin v1.5.0/go.mod h1:Nd6IXA8m5kNZdNEHMBd93KT+mdY3+bewLgRvmCsR2Do=
github.com/go-errors/errors v1.0.1 h1:LUHzmkK3GUKUrL/1gfBUxAHzcev3apQlezX/+O7ma6w=
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
github.com/go-ini/ini v1.25.4 h1:Mujh4R/dH6YL8bxuISne3xX2+qcQ9p0IxKAP6ExWoUo=
Expand All @@ -136,6 +140,12 @@ 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-playground/locales v0.12.1 h1:2FITxuFt/xuCNP1Acdhv62OzaCiviiE4kotfhkmOqEc=
github.com/go-playground/locales v0.12.1/go.mod h1:IUMDtCfWo/w/mtMfIE/IG2K+Ey3ygWanZIBtBW0W2TM=
github.com/go-playground/universal-translator v0.16.0 h1:X++omBR/4cE2MNg91AoC3rmGrCjJ8eAeUP/K/EKx4DM=
github.com/go-playground/universal-translator v0.16.0/go.mod h1:1AnU7NaIRDWWzGEKwgtJRd2xk99HeFyHw3yid4rvQIY=
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 @@ -311,6 +321,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/leodido/go-urn v1.1.0 h1:Sm1gr51B1kKyfD2BlRcLSiEkffoG96g6TPv6eRoEiB8=
github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw=
github.com/lestrrat/go-envload v0.0.0-20180220120943-6ed08b54a570 h1:0iQektZGS248WXmGIYOwRXSQhD4qn3icjMpuxwO7qlo=
github.com/lestrrat/go-envload v0.0.0-20180220120943-6ed08b54a570/go.mod h1:BLt8L9ld7wVsvEWQbuLrUZnCMnUmLZ+CGDzKtclrTlE=
github.com/lestrrat/go-file-rotatelogs v0.0.0-20180223000712-d3151e2a480f h1:sgUSP4zdTUZYZgAGGtN5Lxk92rK+JUFOwf+FT99EEI4=
Expand All @@ -325,6 +337,8 @@ github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRU
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.3 h1:ns/ykhmWi7G9O+8a448SecJU3nSMBXJfqQkl0upE1jI=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg=
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
Patrick0308 marked this conversation as resolved.
Show resolved Hide resolved
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/miekg/dns v1.0.14 h1:9jZdLNd/P4+SfEJ0TNyxYpsK8N4GtfylBLqtbYN1sbA=
Expand Down Expand Up @@ -457,6 +471,10 @@ github.com/toolkits/concurrent v0.0.0-20150624120057-a4371d70e3e3 h1:kF/7m/ZU+0D
github.com/toolkits/concurrent v0.0.0-20150624120057-a4371d70e3e3/go.mod h1:QDlpd3qS71vYtakd2hmdpqhJ9nwv6mD6A30bQ1BPBFE=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926 h1:G3dpKMzFDjgEh2q1Z7zUUtKa8ViPtH+ocF0bE0g00O8=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/vmware/govmomi v0.18.0 h1:f7QxSmP7meCtoAmiKZogvVbLInT+CZx6Px6K5rYsJZo=
github.com/vmware/govmomi v0.18.0/go.mod h1:URlwyTFZX72RmxtxuaFL2Uj3fD1JTvZdx59bHWk6aFU=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 h1:eY9dn8+vbi4tKz5Qo6v2eYzo7kUS51QINcR5jNpbZS8=
Expand Down Expand Up @@ -492,6 +510,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 All @@ -512,6 +532,8 @@ golang.org/x/sys v0.0.0-20190508220229-2d0786266e9c/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190523142557-0e01d883c5c5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190801041406-cbf593c0f2f3 h1:4y9KwBHBgBNwDbtu44R5o1fdOCQUEXhbk/P4A9WmJq0=
golang.org/x/sys v0.0.0-20190801041406-cbf593c0f2f3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
Expand Down Expand Up @@ -547,6 +569,10 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2/go.mod h1:Xk6kEKp8OKb+X14hQBKWaSkCsqBpgog8nAV2xsGOxlo=
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
gopkg.in/go-playground/validator.v9 v9.29.1 h1:SvGtYmN60a5CVKTOzMSyfzWDeZRxRuGvRQyEAKbw1xc=
gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ=
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/ini.v1 v1.42.0 h1:7N3gPTt50s8GuLortA00n8AqRTk75qOP98+mTPpgzRk=
Expand Down
68 changes: 68 additions & 0 deletions protocol/rest/rest_client/resty_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package rest_client

import (
"context"
"github.com/apache/dubbo-go/common/constant"
Patrick0308 marked this conversation as resolved.
Show resolved Hide resolved
"net"
"net/http"
"path"
"time"
)

import (
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/protocol/rest/rest_interface"
"github.com/go-resty/resty/v2"
Patrick0308 marked this conversation as resolved.
Show resolved Hide resolved
)

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

type RestyClient struct {
client *resty.Client
}

func NewRestyClient(restOption *rest_interface.RestOptions) *RestyClient {
if restOption.ConnectTimeout == 0 {
flycash marked this conversation as resolved.
Show resolved Hide resolved
restOption.ConnectTimeout = 3 * time.Second
}
if restOption.RequestTimeout == 0 {
restOption.RequestTimeout = 3 * time.Second
}
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*time.Second)
if err != nil {
return nil, err
}
err = c.SetDeadline(time.Now().Add(restOption.RequestTimeout * time.Second))
if err != nil {
return nil, err
}
return c, nil
},
})
return &RestyClient{
client: client,
}
}

func (rc *RestyClient) Do(restRequest *rest_interface.RestRequest, res interface{}) error {
_, err := rc.client.R().
SetHeader("Content-Type", restRequest.Consumes).
SetHeader("Accept", restRequest.Produces).
SetPathParams(restRequest.PathParams).
SetQueryParams(restRequest.QueryParams).
SetBody(restRequest.Body).
SetResult(res).
SetHeaders(restRequest.Headers).
Execute(restRequest.Method, "http://"+path.Join(restRequest.Location, restRequest.Path))
flycash marked this conversation as resolved.
Show resolved Hide resolved
return err
}

func GetRestyClient(restOptions *rest_interface.RestOptions) rest_interface.RestClient {
return NewRestyClient(restOptions)
}
133 changes: 133 additions & 0 deletions protocol/rest/rest_config_initializer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package rest

import (
"strconv"
"strings"
)

import (
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/common/logger"
"github.com/apache/dubbo-go/config"
_ "github.com/apache/dubbo-go/protocol/rest/rest_config_reader"
Patrick0308 marked this conversation as resolved.
Show resolved Hide resolved
"github.com/apache/dubbo-go/protocol/rest/rest_interface"
)

var (
restConsumerConfig *rest_interface.RestConsumerConfig
restProviderConfig *rest_interface.RestProviderConfig
restConsumerServiceConfigMap map[string]*rest_interface.RestConfig
restProviderServiceConfigMap map[string]*rest_interface.RestConfig
)

func init() {
initConsumerRestConfig()
initProviderRestConfig()
}

func initConsumerRestConfig() {
consumerConfigType := config.GetConsumerConfig().RestConfigType
consumerConfigReader := extension.GetSingletonRestConfigReader(consumerConfigType)
restConsumerConfig = consumerConfigReader.ReadConsumerConfig()
if restConsumerConfig == nil {
return
}
for _, rc := range restConsumerConfig.RestConfigMap {
rc.Client = getNotEmptyStr(rc.Client, restConsumerConfig.Client, constant.DEFAULT_REST_CLIENT)
rc.RestMethodConfigsMap = initMethodConfigMap(rc, restConsumerConfig.Consumes, restConsumerConfig.Produces)
restConsumerServiceConfigMap[rc.InterfaceName] = rc
}
}

func initProviderRestConfig() {
providerConfigType := config.GetProviderConfig().RestConfigType
providerConfigReader := extension.GetSingletonRestConfigReader(providerConfigType)
restProviderConfig = providerConfigReader.ReadProviderConfig()
if restProviderConfig == nil {
return
}
for _, rc := range restProviderConfig.RestConfigMap {
rc.Server = getNotEmptyStr(rc.Server, restProviderConfig.Server)
rc.RestMethodConfigsMap = initMethodConfigMap(rc, restProviderConfig.Consumes, restProviderConfig.Produces)
restProviderServiceConfigMap[rc.InterfaceName] = rc
}
}

func initMethodConfigMap(rc *rest_interface.RestConfig, consumes string, produces string) map[string]*rest_interface.RestMethodConfig {
mcm := make(map[string]*rest_interface.RestMethodConfig, len(rc.RestMethodConfigs))
for _, mc := range rc.RestMethodConfigs {
mc.InterfaceName = rc.InterfaceName
mc.Path = rc.Path + mc.Path
mc.Consumes = getNotEmptyStr(mc.Consumes, rc.Consumes, consumes)
mc.Produces = getNotEmptyStr(mc.Produces, rc.Produces, produces)
mc.MethodType = getNotEmptyStr(mc.MethodType, rc.MethodType)
mc = transformMethodConfig(mc)
mcm[mc.MethodName] = mc
}
return mcm
}

func getNotEmptyStr(args ...string) string {
var r string
for _, t := range args {
if len(t) > 0 {
r = t
break
}
}
return r
}

func transformMethodConfig(methodConfig *rest_interface.RestMethodConfig) *rest_interface.RestMethodConfig {
if len(methodConfig.PathParamsMap) == 0 && len(methodConfig.PathParams) > 0 {
paramsMap, err := parseParamsString2Map(methodConfig.PathParams)
if err != nil {
logger.Warnf("[Rest Config] Path Param parse error:%v", err)
} else {
methodConfig.PathParamsMap = paramsMap
}
}
if len(methodConfig.QueryParamsMap) == 0 && len(methodConfig.QueryParams) > 0 {
paramsMap, err := parseParamsString2Map(methodConfig.PathParams)
if err != nil {
logger.Warnf("[Rest Config] Argument Param parse error:%v", err)
} else {
methodConfig.QueryParamsMap = paramsMap
}
}
if len(methodConfig.BodyMap) == 0 && len(methodConfig.Body) > 0 {
paramsMap, err := parseParamsString2Map(methodConfig.Body)
if err != nil {
logger.Warnf("[Rest Config] Body Param parse error:%v", err)
} else {
methodConfig.BodyMap = paramsMap
}
}
return methodConfig
}

func parseParamsString2Map(params string) (map[int]string, error) {
m := make(map[int]string)
for _, p := range strings.Split(params, ",") {
pa := strings.Split(p, ":")
key, err := strconv.Atoi(pa[0])
if err != nil {
return nil, err
}
m[key] = pa[1]
}
return m, nil
}

func GetRestConsumerServiceConfig(service string) *rest_interface.RestConfig {
return restConsumerServiceConfigMap[service]
}

func GetRestProviderServiceConfig(service string) *rest_interface.RestConfig {
return restProviderServiceConfigMap[service]
}

func SetRestConsumerServiceConfigMap(configMap map[string]*rest_interface.RestConfig) {
restConsumerServiceConfigMap = configMap
}
Loading