重新初始化仓库
This commit is contained in:
156
client/curl.go
Normal file
156
client/curl.go
Normal file
@ -0,0 +1,156 @@
|
||||
// Package client ...
|
||||
//
|
||||
// Description : 使用的客户端
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2021-11-30 10:42 上午
|
||||
package client
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/gopkg/util"
|
||||
|
||||
"github.com/ddliu/go-httpclient"
|
||||
)
|
||||
|
||||
// CenterClient 客户端实例
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 11:01 上午 2021/11/30
|
||||
type CenterClient struct {
|
||||
cfg *Config
|
||||
}
|
||||
|
||||
// CreateConfig 生成配置
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 11:44 上午 2021/11/30
|
||||
func (cc *CenterClient) CreateConfig(ctx context.Context, key string, value string, operateUserID string, description string) error {
|
||||
parameter := map[string]string{
|
||||
"key": key,
|
||||
"value": value,
|
||||
"operate_user_id": operateUserID,
|
||||
"description": description,
|
||||
}
|
||||
_, err := cc.request(ctx, SetConfigURI, parameter, PostHeader)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetConfig 读取配置
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 12:09 下午 2021/11/30
|
||||
func (cc *CenterClient) GetConfig(ctx context.Context, key string, receiver interface{}) ([]byte, error) {
|
||||
var (
|
||||
result []byte
|
||||
err error
|
||||
)
|
||||
parameter := map[string]string{
|
||||
"key": key,
|
||||
}
|
||||
if result, err = cc.request(ctx, GetConfigURI, parameter, GetHeader); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
if nil != receiver {
|
||||
return result, util.JSONUnmarshalWithNumber(result, result)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// UpdateConfig 更新配置
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 3:34 下午 2021/11/30
|
||||
func (cc *CenterClient) UpdateConfig(ctx context.Context, key string, value string, operateUserID string, description string) error {
|
||||
parameter := map[string]string{
|
||||
"key": key,
|
||||
"value": value,
|
||||
"operate_user_id": operateUserID,
|
||||
"description": description,
|
||||
}
|
||||
_, err := cc.request(nil, UpdateConfigURI, parameter, GetHeader)
|
||||
return err
|
||||
}
|
||||
|
||||
// CreateNamespace 创建命名空间
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 12:48 下午 2021/11/30
|
||||
func (cc *CenterClient) CreateNamespace(ctx context.Context, namespace string, createUserID string, name string, description string) error {
|
||||
parameter := map[string]string{
|
||||
"namespace": namespace,
|
||||
"create_user_id": createUserID,
|
||||
"name": name,
|
||||
"description": description,
|
||||
}
|
||||
_, err := cc.request(ctx, CreateNamespaceURI, parameter, PostHeader)
|
||||
return err
|
||||
}
|
||||
|
||||
// ActiveNamespace ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 12:54 下午 2021/11/30
|
||||
func (cc *CenterClient) ActiveNamespace(ctx context.Context, namespace string, updateUserID string) error {
|
||||
parameter := map[string]string{
|
||||
"namespace": namespace,
|
||||
"update_user_id": updateUserID,
|
||||
}
|
||||
_, err := cc.request(ctx, ActiveNamespaceURI, parameter, GetHeader)
|
||||
return err
|
||||
}
|
||||
|
||||
// request 发送请求
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:51 上午 2021/11/30
|
||||
func (cc *CenterClient) request(ctx context.Context, uriConfig *RequestURIConfig, parameter map[string]string, header map[string]string) ([]byte, error) {
|
||||
var (
|
||||
client *httpclient.HttpClient
|
||||
response *httpclient.Response
|
||||
responseData []byte
|
||||
err error
|
||||
)
|
||||
if nil == parameter {
|
||||
parameter = make(map[string]string)
|
||||
}
|
||||
if nil == ctx {
|
||||
ctx = context.TODO()
|
||||
}
|
||||
if _, exist := parameter["namespace"]; !exist {
|
||||
parameter["namespace"] = cc.cfg.Namespace
|
||||
}
|
||||
parameter["app_id"] = cc.cfg.AppID
|
||||
fullRequestURL := cc.cfg.Domain + uriConfig.URI
|
||||
if uriConfig.Method == http.MethodGet {
|
||||
urlParam := url.Values{}
|
||||
for k, v := range parameter {
|
||||
urlParam.Set(k, v)
|
||||
}
|
||||
fullRequestURL += "?" + urlParam.Encode()
|
||||
}
|
||||
client = httpclient.NewHttpClient()
|
||||
inputByteData, _ := json.Marshal(parameter)
|
||||
requestData := bytes.NewReader(inputByteData)
|
||||
if response, err = client.Do(uriConfig.Method, fullRequestURL, header, requestData); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
if responseData, err = ioutil.ReadAll(response.Body); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
return responseData, nil
|
||||
}
|
68
client/curl_test.go
Normal file
68
client/curl_test.go
Normal file
@ -0,0 +1,68 @@
|
||||
// Package client ...
|
||||
//
|
||||
// Description : client ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2021-11-30 2:04 下午
|
||||
package client
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func init() {
|
||||
_ = NewClient(&Config{
|
||||
Domain: "http://localhost:10998",
|
||||
Namespace: "test",
|
||||
AppID: "test",
|
||||
LocalCacheEnable: false,
|
||||
})
|
||||
}
|
||||
|
||||
// TestCenterClient_CreateNamespace ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2:04 下午 2021/11/30
|
||||
func TestCenterClient_CreateNamespace(t *testing.T) {
|
||||
fmt.Println(CenterConfig.CreateNamespace(nil, "test", "123456", "测试", "测试专用命名空间"))
|
||||
}
|
||||
|
||||
// TestCenterClient_ActiveNamespace ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2:10 下午 2021/11/30
|
||||
func TestCenterClient_ActiveNamespace(t *testing.T) {
|
||||
fmt.Println(CenterConfig.ActiveNamespace(nil, "test", "123456"))
|
||||
}
|
||||
|
||||
// TestCenterClient_CreateConfig ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 3:08 下午 2021/11/30
|
||||
func TestCenterClient_CreateConfig(t *testing.T) {
|
||||
fmt.Println(CenterConfig.CreateConfig(nil, "name", "zhangdeman", "123456", "测试key"))
|
||||
}
|
||||
|
||||
// TestCenterClient_GetConfig ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 3:11 下午 2021/11/30
|
||||
func TestCenterClient_GetConfig(t *testing.T) {
|
||||
r, e := CenterConfig.GetConfig(nil, "name", nil)
|
||||
fmt.Println(string(r), e)
|
||||
}
|
||||
|
||||
// TestCenterClient_UpdateConfig ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 3:38 下午 2021/11/30
|
||||
func TestCenterClient_UpdateConfig(t *testing.T) {
|
||||
fmt.Println(CenterConfig.UpdateConfig(nil, "name", "zhangdeman++++", "123456789", "测试更新"))
|
||||
}
|
80
client/define.go
Normal file
80
client/define.go
Normal file
@ -0,0 +1,80 @@
|
||||
// Package client ...
|
||||
//
|
||||
// Description : client ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2021-11-30 10:52 上午
|
||||
package client
|
||||
|
||||
import "net/http"
|
||||
|
||||
// Config 配置
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:54 上午 2021/11/30
|
||||
type Config struct {
|
||||
Domain string `json:"domain" yaml:"domain"` // 配置中心域名
|
||||
Namespace string `json:"namespace" yaml:"namespace"` // 命名空间
|
||||
AppID string `json:"app_id" yaml:"app-id"` // 授权的APP_ID
|
||||
LocalCacheEnable bool `json:"local_cache_enable" yaml:"local-cache-enable"` // 是否启用本地缓存
|
||||
}
|
||||
|
||||
// RequestURIConfig 请求URI的配置
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 11:08 上午 2021/11/30
|
||||
type RequestURIConfig struct {
|
||||
URI string `json:"uri"`
|
||||
Method string `json:"method"`
|
||||
Timeout int `json:"timeout"`
|
||||
}
|
||||
|
||||
var (
|
||||
// SetConfigURI 设置配置
|
||||
SetConfigURI = &RequestURIConfig{
|
||||
URI: "/config/set",
|
||||
Method: http.MethodPost,
|
||||
Timeout: 100,
|
||||
}
|
||||
// GetConfigURI 读取配置
|
||||
GetConfigURI = &RequestURIConfig{
|
||||
URI: "/config/read",
|
||||
Method: http.MethodGet,
|
||||
Timeout: 100,
|
||||
}
|
||||
// UpdateConfigURI 更新配置
|
||||
UpdateConfigURI = &RequestURIConfig{
|
||||
URI: "/config/update",
|
||||
Method: http.MethodPost,
|
||||
Timeout: 100,
|
||||
}
|
||||
// DeleteConfigURI 删除配置
|
||||
DeleteConfigURI = &RequestURIConfig{}
|
||||
// CreateNamespaceURI 创建命名空间
|
||||
CreateNamespaceURI = &RequestURIConfig{
|
||||
URI: "/namespace/create",
|
||||
Method: http.MethodPost,
|
||||
Timeout: 100,
|
||||
}
|
||||
// ActiveNamespaceURI 激活命名空间
|
||||
ActiveNamespaceURI = &RequestURIConfig{
|
||||
URI: "/namespace/active",
|
||||
Method: http.MethodPost,
|
||||
Timeout: 100,
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
// PostHeader 请求header
|
||||
PostHeader = map[string]string{
|
||||
"Content-Type": "application/json",
|
||||
"X-Request-Flag": "center-config-client",
|
||||
}
|
||||
// GetHeader 请求header
|
||||
GetHeader = map[string]string{
|
||||
"X-Request-Flag": "center-config-client",
|
||||
}
|
||||
)
|
32
client/init.go
Normal file
32
client/init.go
Normal file
@ -0,0 +1,32 @@
|
||||
// Package client ...
|
||||
//
|
||||
// Description : client ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2021-11-30 10:51 上午
|
||||
package client
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
// CenterConfig ...
|
||||
CenterConfig *CenterClient
|
||||
)
|
||||
|
||||
// NewClient ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:56 上午 2021/11/30
|
||||
func NewClient(cfg *Config) error {
|
||||
if nil == cfg || len(cfg.Namespace) == 0 || len(cfg.AppID) == 0 || len(cfg.Domain) == 0 {
|
||||
return errors.New("client cfg is error")
|
||||
}
|
||||
CenterConfig = &CenterClient{
|
||||
cfg: cfg,
|
||||
}
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user