From e5922dbfac528b1a63bd8941b7b2cdeb2e555115 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Mon, 19 Jul 2021 18:04:08 +0800 Subject: [PATCH] update * --- .codechina-ci.yml | 37 -------- .codeeui.sh | 13 --- .drone.yml | 10 -- .gitignore | 5 +- .gitlab-ci.yml | 29 ------ .travis.yml | 8 -- CHANGELOG.md | 43 --------- FUNDING.yml | 12 --- README.md | 57 +---------- codeql-analysis.yml | 19 ---- go.mod | 2 +- helper/http/http.go | 2 +- helper/uuid/uuid_test.go | 2 +- library/go-library.go | 5 + service/baidubce/DrCdn.go | 195 ++++++++++++++++++++++++++++++++++++++ service/ddk/Ddk.go | 2 +- service/jdk/Jdk.go | 2 +- service/tbk/Tbk.go | 2 +- 18 files changed, 213 insertions(+), 232 deletions(-) delete mode 100644 .codechina-ci.yml delete mode 100644 .codeeui.sh delete mode 100644 .drone.yml delete mode 100644 .gitlab-ci.yml delete mode 100644 .travis.yml delete mode 100644 CHANGELOG.md delete mode 100644 FUNDING.yml delete mode 100644 codeql-analysis.yml create mode 100644 library/go-library.go diff --git a/.codechina-ci.yml b/.codechina-ci.yml deleted file mode 100644 index 25ead360..00000000 --- a/.codechina-ci.yml +++ /dev/null @@ -1,37 +0,0 @@ -# This file is a template, and might need editing before it works on your project. -image: golang:latest - -variables: - # Please edit to your GitLab project - REPO_NAME: gitlab.com/namespace/project - -# The problem is that to be able to use go get, one needs to put -# the repository in the $GOPATH. So for example if your gitlab domain -# is gitlab.com, and that your repository is namespace/project, and -# the default GOPATH being /go, then you'd need to have your -# repository in /go/src/gitlab.com/namespace/project -# Thus, making a symbolic link corrects this. -before_script: - - mkdir -p $GOPATH/src/$(dirname $REPO_NAME) - - ln -svf $CI_PROJECT_DIR $GOPATH/src/$REPO_NAME - - cd $GOPATH/src/$REPO_NAME - -stages: - - test - - build - - deploy - -format: - stage: test - script: - - go fmt $(go list ./... | grep -v /vendor/) - - go vet $(go list ./... | grep -v /vendor/) - - go test -race $(go list ./... | grep -v /vendor/) - -compile: - stage: build - script: - - go build -race -ldflags "-extldflags '-static'" -o $CI_PROJECT_DIR/mybinary - artifacts: - paths: - - mybinary diff --git a/.codeeui.sh b/.codeeui.sh deleted file mode 100644 index ae373716..00000000 --- a/.codeeui.sh +++ /dev/null @@ -1,13 +0,0 @@ -# 使用vendor文件夹构建 -export GO15VENDOREXPERIMENT=1 -export GOPROXY=https://goproxy.cn,direct -# 在工作目录创建源文件夹 -mkdir -p $GOPATH/src/dtapps/ -# 拷贝代码到创建好的目录 -cp -rf . $GOPATH/src/dtapps/ -# 进入项目 -cd $GOPATH/src/dtapps/ -# 构建,在GOPATH下生成构建包 -go mod tidy -# 列出文件 -ls -lh \ No newline at end of file diff --git a/.drone.yml b/.drone.yml deleted file mode 100644 index 49ec17f0..00000000 --- a/.drone.yml +++ /dev/null @@ -1,10 +0,0 @@ -kind: pipeline -type: docker # 使用docker -name: default - -steps: - - name: go-library - image: golang - commands: - - go mod tidy - - ls -lh diff --git a/.gitignore b/.gitignore index a0fe1095..73efb2e1 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,5 @@ .idea .vscode *.log -git.sh -gittag.sh -*_test.go \ No newline at end of file +*_test.go +gittag.sh \ No newline at end of file diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml deleted file mode 100644 index 5a0b406e..00000000 --- a/.gitlab-ci.yml +++ /dev/null @@ -1,29 +0,0 @@ ---- -stages: - - ".pre" - - build - - test - - deploy - - ".post" -build-job: - stage: build - script: - - echo "Compiling the code..." - - echo "Compile complete." -unit-test-job: - stage: test - script: - - echo "Running unit tests... This will take about 60 seconds." - - sleep 60 - - echo "Code coverage is 90%" -lint-test-job: - stage: test - script: - - echo "Linting code... This will take about 10 seconds." - - sleep 10 - - echo "No lint issues found." -deploy-job: - stage: deploy - script: - - echo "Deploying application..." - - echo "Application successfully deployed." diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index d63b29ab..00000000 --- a/.travis.yml +++ /dev/null @@ -1,8 +0,0 @@ -language: go - -go: - - master - -script: - - go mod tidy - - ls -lh \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md deleted file mode 100644 index 9a6b3e66..00000000 --- a/CHANGELOG.md +++ /dev/null @@ -1,43 +0,0 @@ -## v1.0.16 / 2021-06-07 - -- 增加文件/文件夹函数 - -## v1.0.14 / 2021-06-04 - -- 优化 -- 增加go.mod -- 修复代码 - -## v1.0.11 / 2021-05-31 - -- 【优化】 - -## v1.0.10 / 2021-05-31 - -- 【优化】http - -## v1.0.9 / 2021-05-31 - -- 【助手】增加http函数 - -## v1.0.8 / 2021-05-31 - -- 【助手】增加url函数 -- 【助手】增加规则验证函数 -- 【助手】优化md5函数 - -## v0.0.7 / 2021-05-31 - -- 【助手】增加请求函数 - -## v0.0.6 / 2021-05-29 - -- 【服务】修复拼多多参数 - -## v0.0.5 / 2021-05-29 - -- 增加拼多多联盟服务 - -## v0.0.4 / 2021-05-29 - -- 优化 diff --git a/FUNDING.yml b/FUNDING.yml deleted file mode 100644 index 4b16f597..00000000 --- a/FUNDING.yml +++ /dev/null @@ -1,12 +0,0 @@ -# These are supported funding model platforms - -github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] -patreon: # Replace with a single Patreon username -open_collective: # Replace with a single Open Collective username -ko_fi: # Replace with a single Ko-fi username -tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel -community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry -liberapay: # Replace with a single Liberapay username -issuehunt: # Replace with a single IssueHunt username -otechie: # Replace with a single Otechie username -custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] diff --git a/README.md b/README.md index 95ecbd1f..e2b1c23c 100644 --- a/README.md +++ b/README.md @@ -1,62 +1,15 @@ -www.dtapp.net -

Golang扩展包

📦 Golang扩展包 -[comment]: <> (dtapps) -[![GitHub Org's stars](https://img.shields.io/github/stars/dtapps)](https://github.com/dtapps) - [comment]: <> (go) -[![golang version](https://img.shields.io/badge/golang-%3E%3D1.6-8892BF.svg)](https://pkg.go.dev/github.com/dtapps/go-library) -[![godoc](https://pkg.go.dev/badge/github.com/dtapps/go-library?status.svg)](https://pkg.go.dev/github.com/dtapps/go-library) - -[comment]: <> (goproxy.cn) -[![goproxy](https://goproxy.cn/stats/github.com/dtapps/go-library/badges/download-count.svg)](https://goproxy.cn/stats/github.com/dtapps/go-library) - -[comment]: <> (goreportcard.com) -[![go report card](https://goreportcard.com/badge/github.com/dtapps/go-library)](https://goreportcard.com/report/github.com/dtapps/go-library) - -[comment]: <> (github.com) -[![watchers](https://badgen.net/github/watchers/dtapps/go-library)](https://github.com/dtapps/go-library/watchers) -[![stars](https://badgen.net/github/stars/dtapps/go-library)](https://github.com/dtapps/go-library/stargazers) -[![forks](https://badgen.net/github/forks/dtapps/go-library)](https://github.com/dtapps/go-library/network/members) -[![issues](https://badgen.net/github/issues/dtapps/go-library)](https://github.com/dtapps/go-library/issues) -[![branches](https://badgen.net/github/branches/dtapps/go-library)](https://github.com/dtapps/go-library/branches) -[![releases](https://badgen.net/github/releases/dtapps/go-library)](https://github.com/dtapps/go-library/releases) -[![tags](https://badgen.net/github/tags/dtapps/go-library)](https://github.com/dtapps/go-library/tags) -[![license](https://badgen.net/github/license/dtapps/go-library)](https://github.com/dtapps/go-library/blob/master/LICENSE) -[![contributors](https://badgen.net/github/contributors/dtapps/go-library)](https://github.com/dtapps/go-library/CONTRIBUTING.md) -[![GitHub go.mod Go version (subdirectory of monorepo)](https://img.shields.io/github/go-mod/go-version/dtapps/go-library)](https://github.com/dtapps/go-library) -[![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/dtapps/go-library)](https://github.com/dtapps/go-library/releases) -[![GitHub tag (latest SemVer)](https://img.shields.io/github/v/tag/dtapps/go-library)](https://github.com/dtapps/go-library/tags) -[![GitHub pull requests](https://img.shields.io/github/issues-pr/dtapps/go-library)](https://github.com/dtapps/go-library/pulls) -[![GitHub issues](https://img.shields.io/github/issues/dtapps/go-library)](https://github.com/dtapps/go-library/issues) -[![GitHub Sponsors](https://img.shields.io/github/sponsors/dtapps)](https://github.com/dtapps/go-library/FUNDING.yml) -[![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/dtapps/go-library)](https://github.com/dtapps/go-library) -[![GitHub language count](https://img.shields.io/github/languages/count/dtapps/go-library)](https://github.com/dtapps/go-library) -[![GitHub search hit counter](https://img.shields.io/github/search/dtapps/go-library/go)](https://github.com/dtapps/go-library) -[![GitHub top language](https://img.shields.io/github/languages/top/dtapps/go-library)](https://github.com/dtapps/go-library) - -[comment]: <> (scrutinizer-ci.com) -[![Scrutinizer build (GitHub/Bitbucket)](https://img.shields.io/scrutinizer/build/g/dtapps/go-library/master)](https://scrutinizer-ci.com/g/dtapps/go-library) -[![Scrutinizer coverage (GitHub/BitBucket)](https://img.shields.io/scrutinizer/coverage/g/dtapps/go-library/master)](https://scrutinizer-ci.com/g/dtapps/go-library) -[![Scrutinizer code quality (GitHub/Bitbucket)](https://img.shields.io/scrutinizer/quality/g/dtapps/go-library/master)](https://scrutinizer-ci.com/g/dtapps/go-library) - -[comment]: <> (www.travis-ci.com) -[![Travis (.com) branch](https://img.shields.io/travis/com/dtapps/go-library/master)](https://www.travis-ci.com/github/dtapps/go-library) - -[comment]: <> (app.codecov.io) -[![Codecov branch](https://img.shields.io/codecov/c/github/dtapps/go-library/master)](https://app.codecov.io/gh/dtapps/go-library) - -[comment]: <> (gitlab.com) -[![gitlab (.com)](https://gitlab.com/dtapps/go-library/badges/master/pipeline.svg)](https://gitlab.com/dtapps/go-library) - -[comment]: <> (codechina.csdn.net) -[![codechina.csdn (.net)](https://codechina.csdn.net/dtapps/go-library/badges/master/pipeline.svg)](https://codechina.csdn.net/dtapps/go-library) +[![godoc](https://pkg.go.dev/badge/gitee.com/dtapps/go-library?status.svg)](https://pkg.go.dev/gitee.com/dtapps/go-library) +[![goproxy.cn](https://goproxy.cn/stats/gitee.com/dtapps/go-library/badges/download-count.svg)](https://goproxy.cn/stats/gitee.com/dtapps/go-library) +[![goreportcard.com](https://goreportcard.com/badge/gitee.com/dtapps/go-library)](https://goreportcard.com/report/gitee.com/dtapps/go-library) +[![deps.dev](https://img.shields.io/badge/deps-go-red.svg)](https://deps.dev/go/gitee.com%2Fdtapps%2Fgo-library) ## Install ```Importing -go get github.com/dtapps/go-library +go get gitee.com/dtapps/go-library ``` diff --git a/codeql-analysis.yml b/codeql-analysis.yml deleted file mode 100644 index 2375558f..00000000 --- a/codeql-analysis.yml +++ /dev/null @@ -1,19 +0,0 @@ -- name: GoLang Test - # You may pin to the exact commit or the version. - # uses: n8maninger/action-golang-test@a8263fc1a85b63937b1b365946518675ceab5740 - uses: n8maninger/action-golang-test@v1 - with: - # the package to run the tests for - package: # optional, default is ./... - # additional go test command line arguments, individual arguments should be separated by ';' - args: # optional, default is - # outputs a warning for long running tests, -1 to disable - show-long-running-tests: # optional, default is 30 - # includes package output in the parsed output - show-package-output: # optional - # shows tests that were run and passed - show-passed-tests: # optional - # shows the unparsed std-out from go test instead of the parsed output - show-stdout: # optional - # skips installing and setting up Go, useful for defining additional parameters for actions/setup-go - skip-go-install: # optional \ No newline at end of file diff --git a/go.mod b/go.mod index 5fe66656..0e895f65 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/dtapps/go-library +module gitee.com/dtapps/go-library go 1.16 diff --git a/helper/http/http.go b/helper/http/http.go index 206f864c..3de524d7 100644 --- a/helper/http/http.go +++ b/helper/http/http.go @@ -2,8 +2,8 @@ package http import ( "encoding/json" + "gitee.com/dtapps/go-library/helper/request" "github.com/bitly/go-simplejson" - "github.com/dtapps/go-library/helper/request" "gopkg.in/h2non/gentleman.v2" "gopkg.in/h2non/gentleman.v2/plugins/body" ) diff --git a/helper/uuid/uuid_test.go b/helper/uuid/uuid_test.go index f89205b8..a66f280a 100644 --- a/helper/uuid/uuid_test.go +++ b/helper/uuid/uuid_test.go @@ -2,7 +2,7 @@ package uuid_test import ( "fmt" - "github.com/dtapps/go-library/helper/uuid" + "gitee.com/dtapps/go-library/helper/uuid" "testing" ) diff --git a/library/go-library.go b/library/go-library.go new file mode 100644 index 00000000..a179f95d --- /dev/null +++ b/library/go-library.go @@ -0,0 +1,5 @@ +package library + +func Version() string { + return "1.0.18" +} diff --git a/service/baidubce/DrCdn.go b/service/baidubce/DrCdn.go index 8ffb4ddd..57740ac9 100644 --- a/service/baidubce/DrCdn.go +++ b/service/baidubce/DrCdn.go @@ -1 +1,196 @@ package baidubce + +import ( + "bytes" + "crypto/md5" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + time2 "gitee.com/dtapps/go-library/helper/time" + "gitee.com/dtapps/go-library/helper/uuid" + "gitee.com/dtapps/go-library/service" + "github.com/bitly/go-simplejson" + "github.com/nilorg/sdk/convert" + "io" + "io/ioutil" + "net/http" + "net/url" + "sort" + "strconv" + "strings" + "time" +) + +var ( + // AccessKeyId 应用Key + AccessKeyId string + // SecretAccessKey 秘密 + SecretAccessKey string + // Router 环境请求地址 + Router = "http://cdn.baidubce.com" + // Timeout ... + Timeout time.Duration +) + +// Parameter 参数 +type Parameter map[string]interface{} + +// ParameterJsonEncode 参数 +type ParameterJsonEncode []string + +// copyParameter 复制参数 +func copyParameter(srcParams Parameter) Parameter { + newParams := make(Parameter) + for key, value := range srcParams { + newParams[key] = value + } + return newParams +} + +// execute 执行API接口 +func execute(method string, url string, param Parameter) (bytes []byte, err error) { + err = checkConfig() + if err != nil { + return + } + + //Authorization := auth.GetAuthorizationV2(AccessKeyId, SecretAccessKey, "guangdong", "drcdn", method, url, param.getRequestData(), "") + //fmt.Printf("Authorization:%#v\n", Authorization) + + var req *http.Request + req, err = http.NewRequest(method, Router, strings.NewReader(param.getRequestData())) + if err != nil { + return + } + + //req.Header.Add("Authorization", Authorization) + req.Header.Add("x-bce-date", time2.GetCurrentDate()) + req.Header.Add("x-bce-request-id", uuid.GenUUID()) + req.Header.Add("Content-Type", "application/x-www-form-urlencoded;charset=utf-8") + httpClient := &http.Client{} + httpClient.Timeout = Timeout + var response *http.Response + response, err = httpClient.Do(req) + if err != nil { + return + } + + fmt.Printf("Body:%#v\n", response.Body) + if response.StatusCode != 200 { + err = fmt.Errorf("请求错误:%d", response.StatusCode) + return + } + defer response.Body.Close() + bytes, err = ioutil.ReadAll(response.Body) + return +} + +// Execute 执行API接口 +func Execute(method string, url string, param Parameter) (res *simplejson.Json, err error) { + // 更新参数 + param.setRequestData() + + var bodyBytes []byte + bodyBytes, err = execute(method, url, param) + if err != nil { + return + } + + return bytesToResult(bodyBytes) +} + +func bytesToResult(bytes []byte) (res *simplejson.Json, err error) { + res, err = simplejson.NewJson(bytes) + if err != nil { + return + } + + if responseError, ok := res.CheckGet("error_response"); ok { + if subMsg, subOk := responseError.CheckGet("sub_msg"); subOk { + err = errors.New(subMsg.MustString()) + } else { + err = errors.New(responseError.Get("msg").MustString()) + } + res = nil + } + return +} + +// 检查配置 +func checkConfig() error { + if AccessKeyId == "" { + return errors.New("AccessKeyId 不能为空") + } + if SecretAccessKey == "" { + return errors.New("SecretAccessKey 不能为空") + } + if Router == "" { + return errors.New("Router 不能为空") + } + return nil +} + +func (p Parameter) setRequestData() { + hh, _ := time.ParseDuration("8h") + loc := time.Now().UTC().Add(hh) + p["timestamp"] = strconv.FormatInt(loc.Unix(), 10) + p["client_id"] = AccessKeyId + p["data_type"] = "JSON" + p["version"] = "v1" + // 设置签名 + p["sign"] = getSign(p) +} + +// 获取请求数据 +func (p Parameter) getRequestData() string { + // 公共参数 + args := url.Values{} + // 请求参数 + for key, val := range p { + args.Set(key, interfaceToString(val)) + } + return args.Encode() +} + +// 获取签名 +func getSign(params Parameter) string { + // 获取Key + keys := []string{} + for k := range params { + keys = append(keys, k) + } + // 排序asc + sort.Strings(keys) + // 把所有参数名和参数值串在一起 + query := bytes.NewBufferString(SecretAccessKey) + for _, k := range keys { + query.WriteString(k) + query.WriteString(interfaceToString(params[k])) + } + query.WriteString(SecretAccessKey) + // 使用MD5加密 + h := md5.New() + io.Copy(h, query) + // 把二进制转化为大写的十六进制 + return strings.ToUpper(hex.EncodeToString(h.Sum(nil))) +} + +func interfaceToString(src interface{}) string { + if src == nil { + panic(service.ErrTypeIsNil) + } + switch src.(type) { + case string: + return src.(string) + case int, int8, int32, int64: + case uint8, uint16, uint32, uint64: + case float32, float64: + return convert.ToString(src) + } + data, err := json.Marshal(src) + if err != nil { + panic(err) + } + return string(data) +} diff --git a/service/ddk/Ddk.go b/service/ddk/Ddk.go index d98bf877..83ed9a3a 100644 --- a/service/ddk/Ddk.go +++ b/service/ddk/Ddk.go @@ -7,8 +7,8 @@ import ( "encoding/json" "errors" "fmt" + "gitee.com/dtapps/go-library/service" "github.com/bitly/go-simplejson" - "github.com/dtapps/go-library/service" "github.com/nilorg/sdk/convert" "io" "io/ioutil" diff --git a/service/jdk/Jdk.go b/service/jdk/Jdk.go index 925dd022..8869424c 100644 --- a/service/jdk/Jdk.go +++ b/service/jdk/Jdk.go @@ -7,8 +7,8 @@ import ( "encoding/json" "errors" "fmt" + "gitee.com/dtapps/go-library/service" "github.com/bitly/go-simplejson" - "github.com/dtapps/go-library/service" "github.com/nilorg/sdk/convert" "io" "io/ioutil" diff --git a/service/tbk/Tbk.go b/service/tbk/Tbk.go index f7155c80..7a986901 100644 --- a/service/tbk/Tbk.go +++ b/service/tbk/Tbk.go @@ -7,8 +7,8 @@ import ( "encoding/json" "errors" "fmt" + "gitee.com/dtapps/go-library/service" "github.com/bitly/go-simplejson" - "github.com/dtapps/go-library/service" "github.com/nilorg/sdk/convert" "io" "io/ioutil"