Skip to content

Commit

Permalink
Merge pull request #352 from apache/feature/rest
Browse files Browse the repository at this point in the history
Rest protocol
  • Loading branch information
hxmhlt authored Mar 15, 2020
2 parents 7a7acef + 7e7dfdb commit cfbd042
Show file tree
Hide file tree
Showing 36 changed files with 2,073 additions and 81 deletions.
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 @@ -174,21 +174,36 @@ 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"
)

// HealthCheck Router
Expand Down
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
50 changes: 50 additions & 0 deletions common/extension/config_reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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

import (
"github.com/apache/dubbo-go/config/interfaces"
)

var (
configReaders = make(map[string]func() interfaces.ConfigReader)
defaults = make(map[string]string)
)

// SetConfigReaders set a creator of config reader.
func SetConfigReaders(name string, v func() interfaces.ConfigReader) {
configReaders[name] = v
}

// GetConfigReaders get a config reader by name.
func GetConfigReaders(name string) interfaces.ConfigReader {
if configReaders[name] == nil {
panic("config reader for " + name + " is not existing, make sure you have imported the package.")
}
return configReaders[name]()
}

// SetDefaultConfigReader set {name} to default config reader for {module}
func SetDefaultConfigReader(module, name string) {
defaults[module] = name
}

// GetDefaultConfigReader
func GetDefaultConfigReader() map[string]string {
return defaults
}
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

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

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

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

func GetNewRestClient(name string, restOptions *client.RestOptions) client.RestClient {
if restClients[name] == nil {
panic("restClient for " + name + " is not existing, make sure you have import the package.")
}
return restClients[name](restOptions)
}
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

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

var (
restServers = make(map[string]func() server.RestServer, 8)
)

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

func GetNewRestServer(name string) server.RestServer {
if restServers[name] == nil {
panic("restServer for " + name + " is not existing, make sure you have import the package.")
}
return restServers[name]()
}
7 changes: 7 additions & 0 deletions common/yaml/testdata/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

intTest: 11
booleanTest: false
strTest: "strTest"

child:
strTest: "childStrTest"
50 changes: 50 additions & 0 deletions common/yaml/yaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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 yaml

import (
"io/ioutil"
"path"
)

import (
perrors "github.com/pkg/errors"
"gopkg.in/yaml.v2"
)

// loadYMLConfig Load yml config byte from file
func LoadYMLConfig(confProFile string) ([]byte, error) {
if len(confProFile) == 0 {
return nil, perrors.Errorf("application configure(provider) file name is nil")
}

if path.Ext(confProFile) != ".yml" {
return nil, perrors.Errorf("application configure file name{%v} suffix must be .yml", confProFile)
}

return ioutil.ReadFile(confProFile)
}

// unmarshalYMLConfig Load yml config byte from file , then unmarshal to object
func UnmarshalYMLConfig(confProFile string, out interface{}) ([]byte, error) {
confFileStream, err := LoadYMLConfig(confProFile)
if err != nil {
return confFileStream, perrors.Errorf("ioutil.ReadFile(file:%s) = error:%v", confProFile, perrors.WithStack(err))
}
return confFileStream, yaml.Unmarshal(confFileStream, out)
}
58 changes: 58 additions & 0 deletions common/yaml/yaml_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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 yaml

import (
"path/filepath"
"testing"
)

import (
"github.com/stretchr/testify/assert"
)

func TestUnmarshalYMLConfig(t *testing.T) {
conPath, err := filepath.Abs("./testdata/config.yml")
assert.NoError(t, err)
c := &Config{}
_, err = UnmarshalYMLConfig(conPath, c)
assert.NoError(t, err)
assert.Equal(t, "strTest", c.StrTest)
assert.Equal(t, 11, c.IntTest)
assert.Equal(t, false, c.BooleanTest)
assert.Equal(t, "childStrTest", c.ChildConfig.StrTest)
}

func TestUnmarshalYMLConfig_Error(t *testing.T) {
c := &Config{}
_, err := UnmarshalYMLConfig("./testdata/config", c)
assert.Error(t, err)
_, err = UnmarshalYMLConfig("", c)
assert.Error(t, err)
}

type Config struct {
StrTest string `yaml:"strTest" default:"default" json:"strTest,omitempty" property:"strTest"`
IntTest int `default:"109" yaml:"intTest" json:"intTest,omitempty" property:"intTest"`
BooleanTest bool `yaml:"booleanTest" default:"true" json:"booleanTest,omitempty"`
ChildConfig ChildConfig `yaml:"child" json:"child,omitempty"`
}

type ChildConfig struct {
StrTest string `default:"strTest" default:"default" yaml:"strTest" json:"strTest,omitempty"`
}
29 changes: 3 additions & 26 deletions config/base_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@
package config

import (
"io/ioutil"
"path"
"bytes"
"reflect"
"strconv"
"strings"
)

import (
perrors "github.com/pkg/errors"
"gopkg.in/yaml.v2"
)

import (
Expand All @@ -50,6 +48,8 @@ type BaseConfig struct {
fatherConfig interface{}

MetricConfig *MetricConfig `yaml:"metrics" json:"metrics,omitempty"`

fileStream *bytes.Buffer
}

// startConfigCenter will start the config center.
Expand Down Expand Up @@ -364,27 +364,4 @@ func initializeStruct(t reflect.Type, v reflect.Value) {

}
}

}

// loadYMLConfig Load yml config byte from file
func loadYMLConfig(confProFile string) ([]byte, error) {
if len(confProFile) == 0 {
return nil, perrors.Errorf("application configure(provider) file name is nil")
}

if path.Ext(confProFile) != ".yml" {
return nil, perrors.Errorf("application configure file name{%v} suffix must be .yml", confProFile)
}

return ioutil.ReadFile(confProFile)
}

// unmarshalYMLConfig Load yml config byte from file , then unmarshal to object
func unmarshalYMLConfig(confProFile string, out interface{}) error {
confFileStream, err := loadYMLConfig(confProFile)
if err != nil {
return perrors.Errorf("ioutil.ReadFile(file:%s) = error:%v", confProFile, perrors.WithStack(err))
}
return yaml.Unmarshal(confFileStream, out)
}
Loading

0 comments on commit cfbd042

Please sign in to comment.