This is an automated email from the ASF dual-hosted git repository. xiaoyu pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/incubator-shenyu-client-golang.git
The following commit(s) were added to refs/heads/main by this push: new 9ca316e [feature: add register metadata & go package management.] (#6) 9ca316e is described below commit 9ca316ea3a14dd96c116f54ce863b284458d4337 Author: Lisandro <lishuo5...@163.com> AuthorDate: Mon Apr 25 10:34:41 2022 +0800 [feature: add register metadata & go package management.] (#6) * [feature: add default handle logic & refactor shenyu_admin_client.go.] * [feature: fix default password.] * [feature: add register metadata & go package management.] Co-authored-by: lishuo <lis...@mesomia-tech.com> --- clients/admin_client/shenyu_admin_client.go | 2 +- clients/client_factory.go | 31 +++++++++++++++++ .../post.go => clients/http_client/http_client.go | 31 ++++++++--------- common/constants/const.go | 13 +++++--- common/http_client/post.go | 6 ++-- .../post.go => example/http_client/main.go | 39 +++++++++++----------- go.mod | 8 +++++ go.sum | 4 +++ .../post.go => model/http_meta_data_register.go | 37 +++++++------------- .../post.go => model/http_url_register.go | 34 +++++-------------- 10 files changed, 110 insertions(+), 95 deletions(-) diff --git a/clients/admin_client/shenyu_admin_client.go b/clients/admin_client/shenyu_admin_client.go index dbb5efd..df4904a 100644 --- a/clients/admin_client/shenyu_admin_client.go +++ b/clients/admin_client/shenyu_admin_client.go @@ -36,7 +36,7 @@ type ShenYuAdminClient struct { func GetShenYuAdminUser(shenYuCommonRequest *model.ShenYuCommonRequest) (adminToken model.AdminToken, err error) { var response *http.Response - response, err = shenYuCommonRequest.HttpClient.Request("GET", shenYuCommonRequest.Url, shenYuCommonRequest.Header, constants.DEFAULT_REQUEST_TIME, shenYuCommonRequest.Params) + response, err = shenYuCommonRequest.HttpClient.Request(http.MethodGet, shenYuCommonRequest.Url, shenYuCommonRequest.Header, constants.DEFAULT_REQUEST_TIME, shenYuCommonRequest.Params) if err != nil { return } diff --git a/clients/client_factory.go b/clients/client_factory.go index 90c32d8..7ba92a0 100644 --- a/clients/client_factory.go +++ b/clients/client_factory.go @@ -19,6 +19,7 @@ package clients import ( "github.com/incubator-shenyu-client-golang/clients/admin_client" + "github.com/incubator-shenyu-client-golang/clients/http_client" "github.com/incubator-shenyu-client-golang/common/constants" "github.com/incubator-shenyu-client-golang/model" "reflect" @@ -55,3 +56,33 @@ func NewShenYuAdminClient(client *model.ShenYuAdminClient) (adminToken model.Adm return model.AdminToken{}, err } } + +/** + * Register metadata to ShenYu Gateway + **/ +func RegisterMetaData(adminTokenData model.AdminTokenData) (registerResult bool, err error) { + headers := map[string][]string{} + headers[constants.DEFAULT_CONNECTION] = []string{constants.DEFAULT_CONNECTION_VALUE} + headers[constants.DEFAULT_CONTENT_TYPE] = []string{constants.DEFAULT_CONTENT_TYPE_VALUE} + headers[constants.DEFAULT_TOKEN_HEADER_KEY] = []string{adminTokenData.Token} + + params := map[string]string{} + params["appName"] = "constants.DEFAULT_ADMIN_ACCOUNT" + params["rpcType"] = constants.RPCTYPE_HTTP + params["host"] = "127.0.0.1" + params["port"] = "8080" + + tokenRequest := &model.ShenYuCommonRequest{ + Url: constants.DEFAULT_SHENYU_ADMIN_URL + constants.DEFAULT_BASE_PATH + constants.REGISTER_METADATA, + Header: headers, + Params: params, + TimeoutMs: constants.DEFAULT_REQUEST_TIME, + } + + registerResult, err = http_client.RegisterMetaData(tokenRequest) + if err == nil { + return registerResult, nil + } else { + return false, err + } +} diff --git a/common/http_client/post.go b/clients/http_client/http_client.go similarity index 57% copy from common/http_client/post.go copy to clients/http_client/http_client.go index 97a8dca..495c8f5 100644 --- a/common/http_client/post.go +++ b/clients/http_client/http_client.go @@ -14,33 +14,28 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package http_client import ( + "github.com/incubator-shenyu-client-golang/common/constants" + "github.com/incubator-shenyu-client-golang/model" + "io/ioutil" "net/http" "strings" - "time" ) /** - * The http_client post method Implement + * Register metadata to ShenYu Gateway **/ -func post(url string, header http.Header, timeoutMs uint64, params map[string]string) (response *http.Response, err error) { - client := http.Client{} - client.Timeout = time.Millisecond * time.Duration(timeoutMs) - - body := GetUrlFormedMap(params) - request, errNew := http.NewRequest(http.MethodPost, url, strings.NewReader(body)) - if errNew != nil { - err = errNew +func RegisterMetaData(shenYuCommonRequest *model.ShenYuCommonRequest) (result bool, err error) { + var response *http.Response + response, err = shenYuCommonRequest.HttpClient.Request(http.MethodPost, shenYuCommonRequest.Url, shenYuCommonRequest.Header, constants.DEFAULT_REQUEST_TIME, shenYuCommonRequest.Params) + if err != nil { return } - request.Header = header - resp, errDo := client.Do(request) - if errDo != nil { - err = errDo - } else { - response = resp - } - return + var bytes []byte + bytes, err = ioutil.ReadAll(response.Body) + defer response.Body.Close() + return strings.Contains(string(bytes), constants.DEFAULT_ADMIN_SUCCESS), err } diff --git a/common/constants/const.go b/common/constants/const.go index ffed2ab..113b480 100644 --- a/common/constants/const.go +++ b/common/constants/const.go @@ -21,13 +21,11 @@ package constants * The sdk const **/ const ( - REGISTER_URI = "register-uri" - REGISTER_METADATA = "register-metadata" - DEFAULT_SHENYU_TOKEN = "/platform/login" + DEFAULT_SHENYU_ADMIN_URL = "http://127.0.0.1:9095" DEFAULT_ADMIN_PASSWORD = "123456" DEFAULT_ADMIN_ACCOUNT = "admin" DEFAULT_REQUEST_TIME = 1000 - DEFAULT_SHENYU_ADMIN_URL = "http://127.0.0.1:9095" + DEFAULT_ADMIN_SUCCESS = "success" //System default key ADMIN_USERNAME = "userName" @@ -36,5 +34,12 @@ const ( DEFAULT_CONTENT_TYPE = "Content-Type" DEFAULT_CONNECTION_VALUE = "Keep-Alive" DEFAULT_CONTENT_TYPE_VALUE = "application/json" + DEFAULT_TOKEN_HEADER_KEY = "X-Access-Token" DEFAULT_ADMIN_TOKEN_PARAM_ERROR = 500 + RPCTYPE_HTTP = "http" + RPCTYPE_GRPC = "grpc" + DEFAULT_BASE_PATH = "/shenyu-client" + REGISTER_URI = "/register-uri" + REGISTER_METADATA = "/register-metadata" + DEFAULT_SHENYU_TOKEN = "/platform/login" ) diff --git a/common/http_client/post.go b/common/http_client/post.go index 97a8dca..f39d947 100644 --- a/common/http_client/post.go +++ b/common/http_client/post.go @@ -17,6 +17,7 @@ package http_client import ( + "encoding/json" "net/http" "strings" "time" @@ -29,8 +30,9 @@ func post(url string, header http.Header, timeoutMs uint64, params map[string]st client := http.Client{} client.Timeout = time.Millisecond * time.Duration(timeoutMs) - body := GetUrlFormedMap(params) - request, errNew := http.NewRequest(http.MethodPost, url, strings.NewReader(body)) + //body := GetUrlFormedMap(params) + body, _ := json.Marshal(params) + request, errNew := http.NewRequest(http.MethodPost, url, strings.NewReader(string(body))) if errNew != nil { err = errNew return diff --git a/common/http_client/post.go b/example/http_client/main.go similarity index 55% copy from common/http_client/post.go copy to example/http_client/main.go index 97a8dca..474597a 100644 --- a/common/http_client/post.go +++ b/example/http_client/main.go @@ -14,33 +14,32 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package http_client + +package main import ( - "net/http" - "strings" - "time" + "github.com/incubator-shenyu-client-golang/clients" + "github.com/incubator-shenyu-client-golang/model" + "github.com/wonderivan/logger" ) /** - * The http_client post method Implement + * The shenyu_http_client example **/ -func post(url string, header http.Header, timeoutMs uint64, params map[string]string) (response *http.Response, err error) { - client := http.Client{} - client.Timeout = time.Millisecond * time.Duration(timeoutMs) +func main() { + + //init ShenYuAdminClient + adminClient := &model.ShenYuAdminClient{ + UserName: "admin", //user provide + Password: "123456", //user provide + } - body := GetUrlFormedMap(params) - request, errNew := http.NewRequest(http.MethodPost, url, strings.NewReader(body)) - if errNew != nil { - err = errNew - return + adminTokenData, err := clients.NewShenYuAdminClient(adminClient) + if err == nil { + logger.Info("this is ShenYu Admin client token ->", adminTokenData.AdminTokenData.Token) } - request.Header = header - resp, errDo := client.Do(request) - if errDo != nil { - err = errDo - } else { - response = resp + result, err := clients.RegisterMetaData(adminTokenData.AdminTokenData) + if err == nil { + logger.Info("finish register metadata ,the result is->", result) } - return } diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..7668e54 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module github.com/incubator-shenyu-client-golang + +go 1.17 + +require ( + github.com/pkg/errors v0.9.1 + github.com/wonderivan/logger v1.0.0 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..29a38ff --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/wonderivan/logger v1.0.0 h1:Z6Nz+3SNcizolx3ARH11axdD4DXjFpb2J+ziGUVlv/U= +github.com/wonderivan/logger v1.0.0/go.mod h1:NObMfQ3WOLKfYEZuGeZQfuQfSPE5+QNgRddVMzsAT/k= diff --git a/common/http_client/post.go b/model/http_meta_data_register.go similarity index 57% copy from common/http_client/post.go copy to model/http_meta_data_register.go index 97a8dca..3790341 100644 --- a/common/http_client/post.go +++ b/model/http_meta_data_register.go @@ -14,33 +14,20 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package http_client -import ( - "net/http" - "strings" - "time" -) +package model /** - * The http_client post method Implement + * The ShenYu Http MetaDataRegister **/ -func post(url string, header http.Header, timeoutMs uint64, params map[string]string) (response *http.Response, err error) { - client := http.Client{} - client.Timeout = time.Millisecond * time.Duration(timeoutMs) - - body := GetUrlFormedMap(params) - request, errNew := http.NewRequest(http.MethodPost, url, strings.NewReader(body)) - if errNew != nil { - err = errNew - return - } - request.Header = header - resp, errDo := client.Do(request) - if errDo != nil { - err = errDo - } else { - response = resp - } - return +type MetaDataRegister struct { + AppName string `json:"appName"` + Path string `json:"path"` + RPCType string `json:"rpcType"` + Enabled bool `json:"enabled"` + Host string `json:"host"` + Port int `json:"port"` + PluginNames []interface{} `json:"pluginNames"` + RegisterMetaData bool `json:"registerMetaData"` + TimeMillis int64 `json:"timeMillis"` } diff --git a/common/http_client/post.go b/model/http_url_register.go similarity index 57% copy from common/http_client/post.go copy to model/http_url_register.go index 97a8dca..988efd3 100644 --- a/common/http_client/post.go +++ b/model/http_url_register.go @@ -14,33 +14,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package http_client -import ( - "net/http" - "strings" - "time" -) +package model /** - * The http_client post method Implement + * The ShenYu Http URIRegister **/ -func post(url string, header http.Header, timeoutMs uint64, params map[string]string) (response *http.Response, err error) { - client := http.Client{} - client.Timeout = time.Millisecond * time.Duration(timeoutMs) - - body := GetUrlFormedMap(params) - request, errNew := http.NewRequest(http.MethodPost, url, strings.NewReader(body)) - if errNew != nil { - err = errNew - return - } - request.Header = header - resp, errDo := client.Do(request) - if errDo != nil { - err = errDo - } else { - response = resp - } - return +type URIRegister struct { + Protocol string `json:"protocol"` + AppName string `json:"appName"` + ContextPath string `json:"contextPath"` + RPCType string `json:"rpcType"` + Host string `json:"host"` + Port int `json:"port"` }