From a10f04a54c77845a16bc9fd906676dac57bf995d Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Thu, 3 Jun 2021 22:41:18 +0800 Subject: [PATCH 01/25] create access token file --- src/work/auth/accessToken.go | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/work/auth/accessToken.go diff --git a/src/work/auth/accessToken.go b/src/work/auth/accessToken.go new file mode 100644 index 00000000..b549ff61 --- /dev/null +++ b/src/work/auth/accessToken.go @@ -0,0 +1,4 @@ +package auth + + +// #reference: https://open.work.weixin.qq.com/api/doc/90000/90135/91039 \ No newline at end of file From 6eac8d7882a973f714392235bf6bc91995d98f70 Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Fri, 4 Jun 2021 11:10:40 +0800 Subject: [PATCH 02/25] add app --- src/foundation/app.go | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 src/foundation/app.go diff --git a/src/foundation/app.go b/src/foundation/app.go new file mode 100644 index 00000000..0faf7826 --- /dev/null +++ b/src/foundation/app.go @@ -0,0 +1,2 @@ +package foundation + From 3a24e5a08f69abf726a5cc6d8d568bad1afa20fa Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Fri, 4 Jun 2021 17:03:14 +0800 Subject: [PATCH 03/25] add work app --- main.go | 17 ++++++++++++++++- src/foundation/app.go | 2 -- src/kernel/accessToken.go | 1 + src/kernel/baseClient.go | 1 + src/miniProgram/application.go | 12 ++++++++++++ src/work/application.go | 27 +++++++++++++++++++++++++++ src/work/auth/accessToken.go | 1 + src/work/base/client.go | 1 + 8 files changed, 59 insertions(+), 3 deletions(-) delete mode 100644 src/foundation/app.go create mode 100644 src/kernel/accessToken.go create mode 100644 src/kernel/baseClient.go create mode 100644 src/miniProgram/application.go create mode 100644 src/work/application.go create mode 100644 src/work/auth/accessToken.go create mode 100644 src/work/base/client.go diff --git a/main.go b/main.go index cd3a06fa..755e5d65 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,24 @@ package main -import "fmt" +import ( + "fmt" + fmt2 "github.com/ArtisanCloud/go-libs/fmt" + "github.com/ArtisanCloud/go-wechat/src/work" +) func main() { fmt.Printf("hello Wechat!") + config := map[string]string{ + "corp_id" : "ww454dfb9d6f6d432a", + //'agent_id' : 100020, + "secret" : "", + } + + app := work.NewWork(config) + fmt2.Dump(app) + + + } \ No newline at end of file diff --git a/src/foundation/app.go b/src/foundation/app.go deleted file mode 100644 index 0faf7826..00000000 --- a/src/foundation/app.go +++ /dev/null @@ -1,2 +0,0 @@ -package foundation - diff --git a/src/kernel/accessToken.go b/src/kernel/accessToken.go new file mode 100644 index 00000000..243879d5 --- /dev/null +++ b/src/kernel/accessToken.go @@ -0,0 +1 @@ +package kernel diff --git a/src/kernel/baseClient.go b/src/kernel/baseClient.go new file mode 100644 index 00000000..243879d5 --- /dev/null +++ b/src/kernel/baseClient.go @@ -0,0 +1 @@ +package kernel diff --git a/src/miniProgram/application.go b/src/miniProgram/application.go new file mode 100644 index 00000000..036510ab --- /dev/null +++ b/src/miniProgram/application.go @@ -0,0 +1,12 @@ +package miniProgram + +type MiniProgram struct { + Name string +} + +func (app *MiniProgram)PrintName() { + println("MiniProgram") +} + + + diff --git a/src/work/application.go b/src/work/application.go new file mode 100644 index 00000000..54bf6374 --- /dev/null +++ b/src/work/application.go @@ -0,0 +1,27 @@ +package work + +import "github.com/ArtisanCloud/go-libs/object" + +type Work struct { + DefaultConfig object.HashMap + UserConfig interface{} + + id int +} + +func NewWork(config interface{}) *Work { + app := &Work{ + UserConfig: config, + } + + app.DefaultConfig = object.HashMap{ + "http": map[string]string{ + "base_uri": "https://qyapi.weixin.qq.com/", + }, + } + + + + + return app +} diff --git a/src/work/auth/accessToken.go b/src/work/auth/accessToken.go new file mode 100644 index 00000000..8832b06d --- /dev/null +++ b/src/work/auth/accessToken.go @@ -0,0 +1 @@ +package auth diff --git a/src/work/base/client.go b/src/work/base/client.go new file mode 100644 index 00000000..22f11114 --- /dev/null +++ b/src/work/base/client.go @@ -0,0 +1 @@ +package base From 4eeffb74e14586f701922821cdf8f0300f42620e Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Fri, 4 Jun 2021 18:03:26 +0800 Subject: [PATCH 04/25] update --- src/kernel/accessToken.go | 10 ++++++++++ src/work/auth/accessToken.go | 16 ++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/kernel/accessToken.go b/src/kernel/accessToken.go index 243879d5..63eb6c8e 100644 --- a/src/kernel/accessToken.go +++ b/src/kernel/accessToken.go @@ -1 +1,11 @@ package kernel + +type AccessTokenInterface interface { + GetToken() string + Refresh() AccessTokenInterface + +} + +type AccessToken struct { + +} \ No newline at end of file diff --git a/src/work/auth/accessToken.go b/src/work/auth/accessToken.go index 8832b06d..2d446557 100644 --- a/src/work/auth/accessToken.go +++ b/src/work/auth/accessToken.go @@ -1 +1,17 @@ package auth + +import ( + "github.com/ArtisanCloud/go-libs/object" + "github.com/ArtisanCloud/go-wechat/src/kernel" +) + +type AccessToken struct { + BaseAccessToken kernel.AccessToken +} + +func (token *AccessToken)GetCredentials() object.StringMap { + return object.StringMap{ + "corpid": , + "corpsecret": , + } +} \ No newline at end of file From dcc6804f8993365429880b5008232637f73eefe3 Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Sat, 5 Jun 2021 00:24:08 +0800 Subject: [PATCH 05/25] inject compoments into app --- main.go | 2 +- src/kernel/baseClient.go | 4 ++++ src/kernel/contract/application.go | 15 +++++++++++++++ src/kernel/serviceContainer.go | 3 --- src/work/application.go | 30 ++++++++++++++++++------------ src/work/auth/accessToken.go | 22 ++++++++++++++++------ src/work/base/client.go | 20 ++++++++++++++++++++ 7 files changed, 74 insertions(+), 22 deletions(-) create mode 100644 src/kernel/contract/application.go diff --git a/main.go b/main.go index 219b04af..2d284964 100644 --- a/main.go +++ b/main.go @@ -9,7 +9,7 @@ import ( func main() { - fmt.Printf("hello Wechat!") + fmt.Printf("hello Wechat! \n") config := object.HashMap{ "corp_id": "ww454dfb9d6f6d432a", diff --git a/src/kernel/baseClient.go b/src/kernel/baseClient.go index 243879d5..a9ce3104 100644 --- a/src/kernel/baseClient.go +++ b/src/kernel/baseClient.go @@ -1 +1,5 @@ package kernel + +type BaseClient struct { + +} \ No newline at end of file diff --git a/src/kernel/contract/application.go b/src/kernel/contract/application.go new file mode 100644 index 00000000..90503755 --- /dev/null +++ b/src/kernel/contract/application.go @@ -0,0 +1,15 @@ +package contract + +import ( + "github.com/ArtisanCloud/go-libs/object" + "github.com/ArtisanCloud/go-wechat/src/kernel" +) + +type ApplicationInterface interface { + + GetConfig() []object.HashMap + + GetComponents() *object.HashMap + + GetContainer() *kernel.ServiceContainer +} diff --git a/src/kernel/serviceContainer.go b/src/kernel/serviceContainer.go index 3bfd99b9..d468f042 100644 --- a/src/kernel/serviceContainer.go +++ b/src/kernel/serviceContainer.go @@ -2,9 +2,6 @@ package kernel import "github.com/ArtisanCloud/go-libs/object" -type ApplicationInterface interface { - GetConfig() object.HashMap -} type ServiceContainer struct { ID int diff --git a/src/work/application.go b/src/work/application.go index 6fa62336..e85580a2 100644 --- a/src/work/application.go +++ b/src/work/application.go @@ -3,11 +3,15 @@ package work import ( "github.com/ArtisanCloud/go-libs/object" "github.com/ArtisanCloud/go-wechat/src/kernel" + "github.com/ArtisanCloud/go-wechat/src/work/auth" + "github.com/ArtisanCloud/go-wechat/src/work/base" ) type Work struct { Container *kernel.ServiceContainer Base interface{} + + Components *object.HashMap } func NewWork(config object.HashMap) *Work { @@ -20,23 +24,25 @@ func NewWork(config object.HashMap) *Work { }, }, }, + Components: &object.HashMap{}, } // inject components - //auth.startInit() - //base.startInit() - //base.startInit() - //auth.startInit() - //base.startInit() - //base.startInit() - //auth3.intall() - //base3.startInit() - //base.startInit() - //auth.startInit() - //base.startInit() - //base.startInit() + auth.Inject(app) + base.Inject(app) + return app } +func (app *Work)GetComponents() *object.HashMap{ + return app.Components +} + +func (app *Work) GetConfig() []object.HashMap { + return app.Container.GetConfig() +} +func (app *Work) GetContainer() *kernel.ServiceContainer{ + return app.Container +} \ No newline at end of file diff --git a/src/work/auth/accessToken.go b/src/work/auth/accessToken.go index cf1e3cc4..50aa0ac1 100644 --- a/src/work/auth/accessToken.go +++ b/src/work/auth/accessToken.go @@ -5,22 +5,32 @@ package auth import ( "github.com/ArtisanCloud/go-libs/object" "github.com/ArtisanCloud/go-wechat/src/kernel" + "github.com/ArtisanCloud/go-wechat/src/kernel/contract" ) -func NewToken(app *kernel.ApplicationInterface) { - -} type AccessToken struct { - App kernel.ApplicationInterface + App *kernel.ServiceContainer BaseAccessToken *kernel.AccessToken } +func Inject(app contract.ApplicationInterface){ + + component := &AccessToken{ + App: app.GetContainer(), + BaseAccessToken: &kernel.AccessToken{}, + } + components := app.GetComponents() + + (*components)["access_token"] = component + +} + func (token *AccessToken) GetCredentials() object.StringMap { config := token.App.GetConfig() return object.StringMap{ - "corpid": config["corp_id"].(string), - "corpsecret": config["secret"].(string), + "corpid": config[2]["corp_id"].(string), + "corpsecret": config[2]["secret"].(string), } } diff --git a/src/work/base/client.go b/src/work/base/client.go index 22f11114..0dc89a2d 100644 --- a/src/work/base/client.go +++ b/src/work/base/client.go @@ -1 +1,21 @@ package base + +import ( + "github.com/ArtisanCloud/go-wechat/src/kernel" + "github.com/ArtisanCloud/go-wechat/src/kernel/contract" +) + +type Client struct { + BaseClient *kernel.BaseClient +} + +func Inject(app contract.ApplicationInterface){ + + component := &Client{ + BaseClient: &kernel.BaseClient{}, + } + components := app.GetComponents() + + (*components)["base"] = component + +} \ No newline at end of file From 7de62d8571d90519dae956b708eee1690bf361f5 Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Sat, 5 Jun 2021 00:24:47 +0800 Subject: [PATCH 06/25] inject compoments into app --- src/kernel/contract/component.go | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 src/kernel/contract/component.go diff --git a/src/kernel/contract/component.go b/src/kernel/contract/component.go new file mode 100644 index 00000000..a26b8a52 --- /dev/null +++ b/src/kernel/contract/component.go @@ -0,0 +1,5 @@ +package contract + +type ComponentInterface interface { + +} \ No newline at end of file From 7e746a61e1412d79608557c6a4df654f55286440 Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Sat, 5 Jun 2021 00:31:29 +0800 Subject: [PATCH 07/25] inject compoments into app --- src/kernel/contract/application.go | 2 -- src/work/application.go | 4 ---- 2 files changed, 6 deletions(-) diff --git a/src/kernel/contract/application.go b/src/kernel/contract/application.go index 90503755..704ee598 100644 --- a/src/kernel/contract/application.go +++ b/src/kernel/contract/application.go @@ -7,8 +7,6 @@ import ( type ApplicationInterface interface { - GetConfig() []object.HashMap - GetComponents() *object.HashMap GetContainer() *kernel.ServiceContainer diff --git a/src/work/application.go b/src/work/application.go index e85580a2..29412130 100644 --- a/src/work/application.go +++ b/src/work/application.go @@ -39,10 +39,6 @@ func (app *Work)GetComponents() *object.HashMap{ return app.Components } -func (app *Work) GetConfig() []object.HashMap { - return app.Container.GetConfig() -} - func (app *Work) GetContainer() *kernel.ServiceContainer{ return app.Container } \ No newline at end of file From 3d5e49a02d4aea3022f5122410819fd8901d05d5 Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Mon, 7 Jun 2021 00:05:19 +0800 Subject: [PATCH 08/25] add request and response interface --- src/kernel/baseClient.go | 5 -- src/kernel/http/Contract/clientInterface.go | 13 ++++ src/kernel/http/Contract/requestInterface.go | 5 ++ src/kernel/http/Contract/responseInterface.go | 13 ++++ src/kernel/http/baseClient.go | 63 +++++++++++++++++++ src/kernel/http/request.go | 27 ++++++++ src/kernel/http/response.go | 8 +++ src/kernel/serviceContainer.go | 12 +++- src/work/application.go | 1 - src/work/auth/accessToken.go | 11 ---- src/work/auth/provider.go | 20 ++++++ src/work/base/client.go | 18 ++---- src/work/base/provider.go | 20 ++++++ 13 files changed, 183 insertions(+), 33 deletions(-) delete mode 100644 src/kernel/baseClient.go create mode 100644 src/kernel/http/Contract/clientInterface.go create mode 100644 src/kernel/http/Contract/requestInterface.go create mode 100644 src/kernel/http/Contract/responseInterface.go create mode 100644 src/kernel/http/baseClient.go create mode 100644 src/kernel/http/request.go create mode 100644 src/kernel/http/response.go create mode 100644 src/work/auth/provider.go create mode 100644 src/work/base/provider.go diff --git a/src/kernel/baseClient.go b/src/kernel/baseClient.go deleted file mode 100644 index a9ce3104..00000000 --- a/src/kernel/baseClient.go +++ /dev/null @@ -1,5 +0,0 @@ -package kernel - -type BaseClient struct { - -} \ No newline at end of file diff --git a/src/kernel/http/Contract/clientInterface.go b/src/kernel/http/Contract/clientInterface.go new file mode 100644 index 00000000..6d9b8a7a --- /dev/null +++ b/src/kernel/http/Contract/clientInterface.go @@ -0,0 +1,13 @@ +package Contract + +import ( + "github.com/ArtisanCloud/go-libs/object" +) + +type ClientInterface interface { + Send(request RequestInterface, options object.HashMap) ResponseContract + SendAsync(request RequestInterface, options object.HashMap) PromiseInterface + + Request(method string, uri string, options object.HashMap) ResponseContract + RequestAsync(method string, uri string, options object.HashMap) PromiseInterface +} diff --git a/src/kernel/http/Contract/requestInterface.go b/src/kernel/http/Contract/requestInterface.go new file mode 100644 index 00000000..cdecdebc --- /dev/null +++ b/src/kernel/http/Contract/requestInterface.go @@ -0,0 +1,5 @@ +package Contract + +type RequestInterface interface { + +} \ No newline at end of file diff --git a/src/kernel/http/Contract/responseInterface.go b/src/kernel/http/Contract/responseInterface.go new file mode 100644 index 00000000..acfdf1a7 --- /dev/null +++ b/src/kernel/http/Contract/responseInterface.go @@ -0,0 +1,13 @@ +package Contract + +import "github.com/ArtisanCloud/go-libs/object" + +type ResponseContract interface { + GetStatusCode() int + WithStatus() object.HashMap + GetReasonPhrase() object.HashMap +} + +type PromiseInterface interface { + +} \ No newline at end of file diff --git a/src/kernel/http/baseClient.go b/src/kernel/http/baseClient.go new file mode 100644 index 00000000..530957ba --- /dev/null +++ b/src/kernel/http/baseClient.go @@ -0,0 +1,63 @@ +package http + +import ( + "github.com/ArtisanCloud/go-libs/object" + "github.com/ArtisanCloud/go-wechat/src/kernel" + "github.com/ArtisanCloud/go-wechat/src/kernel/http/Contract" +) + +type BaseClient struct { + App *kernel.ServiceContainer + + httpClient *Contract.ClientInterface +} + +func (client *BaseClient) HttpGet(url string, query object.StringMap) interface{} { + return client.Request( + url, + "GET", + object.HashMap{ + "query": query, + }, + false, + ) +} + +func (client *BaseClient) HttpPost(url string, data object.HashMap) interface{} { + return client.Request( + url, + "POST", + object.HashMap{ + "form_params": data, + }, + false, + ) +} + +func (client *BaseClient) HttpPostJson(url string, data object.HashMap, query object.StringMap) interface{} { + return client.Request( + url, + "POST", + object.HashMap{ + "query": query, + "form_params": data, + }, + false, + ) +} + +func (client *BaseClient) Request(url string, method string, options object.HashMap, returnRaw bool) interface{} { + + // to be setup middleware here + + // + response := client.performRequest(url, method, options) + + if returnRaw { + return response + } else { + client.castResponseToType(response, client.App.Config[1]["response_type"].(string)) + } + return nil +} + diff --git a/src/kernel/http/request.go b/src/kernel/http/request.go new file mode 100644 index 00000000..87d5dbfe --- /dev/null +++ b/src/kernel/http/request.go @@ -0,0 +1,27 @@ +package http + +import ( + "github.com/ArtisanCloud/go-libs/object" + "github.com/ArtisanCloud/go-libs/str" + "github.com/ArtisanCloud/go-wechat/src/kernel/http/Contract" +) + + +func (client *BaseClient) performRequest(url string, method string, options object.HashMap) Contract.ResponseContract { + // change method string format + method = str.Lower(method) + + // merge options with default options + + response := client.GetHttpClient().Request(method, url, options) + return response +} + +func (client *BaseClient) SetHttpClient(httpClient *Contract.ClientInterface) *BaseClient { + client.httpClient = httpClient + return client +} + +func (client *BaseClient) GetHttpClient() Contract.ClientInterface { + return nil +} diff --git a/src/kernel/http/response.go b/src/kernel/http/response.go new file mode 100644 index 00000000..c835b9a0 --- /dev/null +++ b/src/kernel/http/response.go @@ -0,0 +1,8 @@ +package http + +import "github.com/ArtisanCloud/go-wechat/src/kernel/http/Contract" + +func (client *BaseClient) castResponseToType(response Contract.ResponseContract, responseType string) interface{} { + + return nil +} diff --git a/src/kernel/serviceContainer.go b/src/kernel/serviceContainer.go index d468f042..2bcbb5dd 100644 --- a/src/kernel/serviceContainer.go +++ b/src/kernel/serviceContainer.go @@ -2,12 +2,12 @@ package kernel import "github.com/ArtisanCloud/go-libs/object" - type ServiceContainer struct { ID int DefaultConfig object.HashMap UserConfig object.HashMap + Config []object.HashMap } func (container *ServiceContainer) getBaseConfig() object.HashMap { @@ -21,11 +21,17 @@ func (container *ServiceContainer) getBaseConfig() object.HashMap { } func (container *ServiceContainer) GetConfig() []object.HashMap { - baseConfig := container.getBaseConfig() + // if container config has been setup + if container.Config!=nil{ + return container.Config + } - return []object.HashMap{ + // setup the container config first time + baseConfig := container.getBaseConfig() + container.Config = []object.HashMap{ baseConfig, container.DefaultConfig, container.UserConfig, } + return container.Config } diff --git a/src/work/application.go b/src/work/application.go index 29412130..6f559458 100644 --- a/src/work/application.go +++ b/src/work/application.go @@ -9,7 +9,6 @@ import ( type Work struct { Container *kernel.ServiceContainer - Base interface{} Components *object.HashMap } diff --git a/src/work/auth/accessToken.go b/src/work/auth/accessToken.go index 50aa0ac1..533c0d7a 100644 --- a/src/work/auth/accessToken.go +++ b/src/work/auth/accessToken.go @@ -5,7 +5,6 @@ package auth import ( "github.com/ArtisanCloud/go-libs/object" "github.com/ArtisanCloud/go-wechat/src/kernel" - "github.com/ArtisanCloud/go-wechat/src/kernel/contract" ) @@ -14,17 +13,7 @@ type AccessToken struct { BaseAccessToken *kernel.AccessToken } -func Inject(app contract.ApplicationInterface){ - component := &AccessToken{ - App: app.GetContainer(), - BaseAccessToken: &kernel.AccessToken{}, - } - components := app.GetComponents() - - (*components)["access_token"] = component - -} func (token *AccessToken) GetCredentials() object.StringMap { diff --git a/src/work/auth/provider.go b/src/work/auth/provider.go new file mode 100644 index 00000000..d74b872d --- /dev/null +++ b/src/work/auth/provider.go @@ -0,0 +1,20 @@ +package auth + +import ( + "github.com/ArtisanCloud/go-wechat/src/kernel" + "github.com/ArtisanCloud/go-wechat/src/kernel/contract" +) + +func Inject(app contract.ApplicationInterface) { + container := app.GetContainer() + container.GetConfig() + + component := &AccessToken{ + App: container, + BaseAccessToken: &kernel.AccessToken{}, + } + components := app.GetComponents() + + (*components)["access_token"] = component + +} diff --git a/src/work/base/client.go b/src/work/base/client.go index 0dc89a2d..6545f1b1 100644 --- a/src/work/base/client.go +++ b/src/work/base/client.go @@ -1,21 +1,13 @@ package base import ( - "github.com/ArtisanCloud/go-wechat/src/kernel" - "github.com/ArtisanCloud/go-wechat/src/kernel/contract" + "github.com/ArtisanCloud/go-wechat/src/kernel/http" ) type Client struct { - BaseClient *kernel.BaseClient + BaseClient *http.BaseClient } -func Inject(app contract.ApplicationInterface){ - - component := &Client{ - BaseClient: &kernel.BaseClient{}, - } - components := app.GetComponents() - - (*components)["base"] = component - -} \ No newline at end of file +func (client *Client) GetCallbackIp() interface{} { + return client.BaseClient.HttpGet("cgi-bin/getcallbackip", nil) +} diff --git a/src/work/base/provider.go b/src/work/base/provider.go new file mode 100644 index 00000000..2c018313 --- /dev/null +++ b/src/work/base/provider.go @@ -0,0 +1,20 @@ +package base + +import ( + "github.com/ArtisanCloud/go-wechat/src/kernel/contract" + "github.com/ArtisanCloud/go-wechat/src/kernel/http" +) + +func Inject(app contract.ApplicationInterface) { + container := app.GetContainer() + container.GetConfig() + + component := &Client{ + BaseClient: &http.BaseClient{ + App: container, + }, + } + components := app.GetComponents() + + (*components)["base"] = component +} From 3c532d5b76620d9a64a83f45129b990caf8f12ac Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Mon, 7 Jun 2021 18:00:37 +0800 Subject: [PATCH 09/25] update --- main.go | 4 + src/kernel/accessToken.go | 78 +++++++++++++++++-- src/kernel/contract/AccessTokenInterface.go | 13 ++++ src/kernel/http/Contract/clientInterface.go | 2 +- src/kernel/http/Contract/requestInterface.go | 2 +- src/kernel/http/Contract/responseInterface.go | 2 +- src/kernel/http/baseClient.go | 5 +- src/kernel/http/request.go | 17 +++- src/kernel/http/response.go | 6 +- src/kernel/serviceContainer.go | 6 +- src/work/auth/accessToken.go | 27 +++++-- src/work/auth/provider.go | 6 +- src/work/base/client.go | 4 +- 13 files changed, 139 insertions(+), 33 deletions(-) create mode 100644 src/kernel/contract/AccessTokenInterface.go diff --git a/main.go b/main.go index 2d284964..8ab762e6 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( fmt2 "github.com/ArtisanCloud/go-libs/fmt" "github.com/ArtisanCloud/go-libs/object" "github.com/ArtisanCloud/go-wechat/src/work" + "github.com/ArtisanCloud/go-wechat/src/work/auth" ) func main() { @@ -19,6 +20,9 @@ func main() { app := work.NewWork(config) fmt2.Dump(app) + components := app.GetComponents() + token := (*components)["access_token"].(*auth.AccessToken).BaseAccessToken.GetToken() + fmt2.Dump(token) //fmt2.Dump(app.Container.GetConfig()) } diff --git a/src/kernel/accessToken.go b/src/kernel/accessToken.go index 63eb6c8e..400ef1f0 100644 --- a/src/kernel/accessToken.go +++ b/src/kernel/accessToken.go @@ -1,11 +1,77 @@ package kernel -type AccessTokenInterface interface { - GetToken() string - Refresh() AccessTokenInterface - -} +import ( + "errors" + "github.com/ArtisanCloud/go-libs/object" + "reflect" + "time" +) type AccessToken struct { + App *ServiceContainer + + RequestMethod string + EndpointToGetToken string + QueryName string + Token object.HashMap + TokenKey string + CachePrefix string + + GetCredentials func() *object.StringMap +} + +func NewAccessToken(container *ServiceContainer) *AccessToken { + token := &AccessToken{ + App: container, + RequestMethod: "GET", + EndpointToGetToken: "", + QueryName: "", + Token: nil, + TokenKey: "access_token", + CachePrefix: "ac.go.wechat.kernel.access_token.", + } + + return token +} + +func (component *AccessToken) GetRefreshedToken() *object.HashMap { + return component.GetToken() +} + +func (component *AccessToken) GetToken() (token *object.HashMap) { + //cacheKey := token.GetCacheKey() + //cache:= GetCache() -} \ No newline at end of file + response := component.requestToken(component.GetCredentials(), true) + typeResponse := reflect.TypeOf(response) + //fmt.Printf("response name:%s, type: %s \n",typeResponse.Name(), typeResponse.Kind() ) + if typeResponse.Name() == "ResponseInterface" { + token = response.(*object.HashMap) + } + + return token +} + +func (component *AccessToken) SetToken(token string, lifeTime time.Duration) (token *object.HashMap, err error) { + if lifeTime <= 0 { + lifeTime = 7200 * time.Second + } + + cache := component.GetCache() + cache.Set(component.GetCacheKey(), map[string]interface{}{ + component.TokenKey: token, + "expires_in": lifeTime, + }) + + if !cache.Has(component.GetCacheKey()) { + err = errors.New("Failed to cache access token.") + } + return token, err + +} + +func (component *AccessToken) requestToken(credentials *object.StringMap, toArray bool) interface{} { + //fmt.Printf("credentials:%v \n", credentials) + + return nil +} diff --git a/src/kernel/contract/AccessTokenInterface.go b/src/kernel/contract/AccessTokenInterface.go new file mode 100644 index 00000000..a72bcf22 --- /dev/null +++ b/src/kernel/contract/AccessTokenInterface.go @@ -0,0 +1,13 @@ +package contract + +import ( + "github.com/ArtisanCloud/go-libs/object" + "github.com/ArtisanCloud/go-wechat/src/kernel/http/contract" +) + +type AccessTokenInterface interface { + GetToken() string + Refresh() AccessTokenInterface + ApplyToRequest(request contract.RequestInterface, requestOptions object.HashMap) contract.RequestInterface + +} \ No newline at end of file diff --git a/src/kernel/http/Contract/clientInterface.go b/src/kernel/http/Contract/clientInterface.go index 6d9b8a7a..11df10af 100644 --- a/src/kernel/http/Contract/clientInterface.go +++ b/src/kernel/http/Contract/clientInterface.go @@ -1,4 +1,4 @@ -package Contract +package contract import ( "github.com/ArtisanCloud/go-libs/object" diff --git a/src/kernel/http/Contract/requestInterface.go b/src/kernel/http/Contract/requestInterface.go index cdecdebc..78ebbe14 100644 --- a/src/kernel/http/Contract/requestInterface.go +++ b/src/kernel/http/Contract/requestInterface.go @@ -1,4 +1,4 @@ -package Contract +package contract type RequestInterface interface { diff --git a/src/kernel/http/Contract/responseInterface.go b/src/kernel/http/Contract/responseInterface.go index acfdf1a7..5bab34ff 100644 --- a/src/kernel/http/Contract/responseInterface.go +++ b/src/kernel/http/Contract/responseInterface.go @@ -1,4 +1,4 @@ -package Contract +package contract import "github.com/ArtisanCloud/go-libs/object" diff --git a/src/kernel/http/baseClient.go b/src/kernel/http/baseClient.go index 530957ba..38992259 100644 --- a/src/kernel/http/baseClient.go +++ b/src/kernel/http/baseClient.go @@ -3,13 +3,13 @@ package http import ( "github.com/ArtisanCloud/go-libs/object" "github.com/ArtisanCloud/go-wechat/src/kernel" - "github.com/ArtisanCloud/go-wechat/src/kernel/http/Contract" + "github.com/ArtisanCloud/go-wechat/src/kernel/http/contract" ) type BaseClient struct { App *kernel.ServiceContainer - httpClient *Contract.ClientInterface + httpClient *contract.ClientInterface } func (client *BaseClient) HttpGet(url string, query object.StringMap) interface{} { @@ -60,4 +60,3 @@ func (client *BaseClient) Request(url string, method string, options object.Hash } return nil } - diff --git a/src/kernel/http/request.go b/src/kernel/http/request.go index 87d5dbfe..f25bd67d 100644 --- a/src/kernel/http/request.go +++ b/src/kernel/http/request.go @@ -3,11 +3,20 @@ package http import ( "github.com/ArtisanCloud/go-libs/object" "github.com/ArtisanCloud/go-libs/str" - "github.com/ArtisanCloud/go-wechat/src/kernel/http/Contract" + "github.com/ArtisanCloud/go-wechat/src/kernel/http/contract" ) +var _defaults *object.HashMap -func (client *BaseClient) performRequest(url string, method string, options object.HashMap) Contract.ResponseContract { +func SetDefaultOptions(defaults *object.HashMap) { + _defaults = defaults +} + +func GetDefaultOptions() *object.HashMap { + return _defaults +} + +func (client *BaseClient) performRequest(url string, method string, options object.HashMap) contract.ResponseContract { // change method string format method = str.Lower(method) @@ -17,11 +26,11 @@ func (client *BaseClient) performRequest(url string, method string, options obje return response } -func (client *BaseClient) SetHttpClient(httpClient *Contract.ClientInterface) *BaseClient { +func (client *BaseClient) SetHttpClient(httpClient *contract.ClientInterface) *BaseClient { client.httpClient = httpClient return client } -func (client *BaseClient) GetHttpClient() Contract.ClientInterface { +func (client *BaseClient) GetHttpClient() contract.ClientInterface { return nil } diff --git a/src/kernel/http/response.go b/src/kernel/http/response.go index c835b9a0..a7b7282c 100644 --- a/src/kernel/http/response.go +++ b/src/kernel/http/response.go @@ -1,8 +1,10 @@ package http -import "github.com/ArtisanCloud/go-wechat/src/kernel/http/Contract" +import ( + "github.com/ArtisanCloud/go-wechat/src/kernel/http/contract" +) -func (client *BaseClient) castResponseToType(response Contract.ResponseContract, responseType string) interface{} { +func (client *BaseClient) castResponseToType(response contract.ResponseContract, responseType string) interface{} { return nil } diff --git a/src/kernel/serviceContainer.go b/src/kernel/serviceContainer.go index 2bcbb5dd..c8fefaa2 100644 --- a/src/kernel/serviceContainer.go +++ b/src/kernel/serviceContainer.go @@ -2,6 +2,10 @@ package kernel import "github.com/ArtisanCloud/go-libs/object" +const CONFIG_BASE_INDEX = 0 +const CONFIG_DEFAULT_INDEX = 1 +const CONFIG_USER_INDEX = 2 + type ServiceContainer struct { ID int @@ -26,7 +30,7 @@ func (container *ServiceContainer) GetConfig() []object.HashMap { return container.Config } - // setup the container config first time + // setup the container config firstly baseConfig := container.getBaseConfig() container.Config = []object.HashMap{ baseConfig, diff --git a/src/work/auth/accessToken.go b/src/work/auth/accessToken.go index 533c0d7a..1a43f837 100644 --- a/src/work/auth/accessToken.go +++ b/src/work/auth/accessToken.go @@ -7,19 +7,32 @@ import ( "github.com/ArtisanCloud/go-wechat/src/kernel" ) - type AccessToken struct { - App *kernel.ServiceContainer BaseAccessToken *kernel.AccessToken + +} + +func NewAccessToken(container *kernel.ServiceContainer) *AccessToken { + token := &AccessToken{ + BaseAccessToken: kernel.NewAccessToken(container), + } + + token.BaseAccessToken.EndpointToGetToken = "cgi-bin/gettoken" + token.SetupCredentials() + + return token } -func (token *AccessToken) GetCredentials() object.StringMap { +func (component *AccessToken) SetupCredentials(){ + config := component.BaseAccessToken.App.GetConfig() + component.BaseAccessToken.GetCredentials = func() *object.StringMap { - config := token.App.GetConfig() - return object.StringMap{ - "corpid": config[2]["corp_id"].(string), - "corpsecret": config[2]["secret"].(string), + return &object.StringMap{ + "corpid": config[kernel.CONFIG_USER_INDEX]["corp_id"].(string), + "corpsecret": config[kernel.CONFIG_USER_INDEX]["secret"].(string), + } } + } diff --git a/src/work/auth/provider.go b/src/work/auth/provider.go index d74b872d..a506ed7f 100644 --- a/src/work/auth/provider.go +++ b/src/work/auth/provider.go @@ -1,7 +1,6 @@ package auth import ( - "github.com/ArtisanCloud/go-wechat/src/kernel" "github.com/ArtisanCloud/go-wechat/src/kernel/contract" ) @@ -9,10 +8,7 @@ func Inject(app contract.ApplicationInterface) { container := app.GetContainer() container.GetConfig() - component := &AccessToken{ - App: container, - BaseAccessToken: &kernel.AccessToken{}, - } + component := NewAccessToken(container) components := app.GetComponents() (*components)["access_token"] = component diff --git a/src/work/base/client.go b/src/work/base/client.go index 6545f1b1..c5fbbbac 100644 --- a/src/work/base/client.go +++ b/src/work/base/client.go @@ -8,6 +8,6 @@ type Client struct { BaseClient *http.BaseClient } -func (client *Client) GetCallbackIp() interface{} { - return client.BaseClient.HttpGet("cgi-bin/getcallbackip", nil) +func (comp *Client) GetCallbackIp() interface{} { + return comp.BaseClient.HttpGet("cgi-bin/getcallbackip", nil) } From 102d1870bfb6ee4bb94c46c0c235c679b6b43e5d Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Tue, 8 Jun 2021 00:35:54 +0800 Subject: [PATCH 10/25] update --- go.mod | 4 + src/kernel/accessToken.go | 83 ++++++++++++++++--- src/kernel/contract/AccessTokenInterface.go | 13 --- src/kernel/contract/accessTokenInterface.go | 14 ++++ src/kernel/http/Contract/clientInterface.go | 13 --- src/kernel/http/Contract/requestInterface.go | 5 -- src/kernel/http/Contract/responseInterface.go | 13 --- src/kernel/http/baseClient.go | 62 -------------- src/kernel/http/request.go | 36 -------- src/kernel/http/response.go | 10 --- src/work/auth/accessToken.go | 20 ++--- src/work/base/client.go | 6 +- src/work/base/provider.go | 2 +- 13 files changed, 102 insertions(+), 179 deletions(-) delete mode 100644 src/kernel/contract/AccessTokenInterface.go create mode 100644 src/kernel/contract/accessTokenInterface.go delete mode 100644 src/kernel/http/Contract/clientInterface.go delete mode 100644 src/kernel/http/Contract/requestInterface.go delete mode 100644 src/kernel/http/Contract/responseInterface.go delete mode 100644 src/kernel/http/baseClient.go delete mode 100644 src/kernel/http/request.go delete mode 100644 src/kernel/http/response.go diff --git a/go.mod b/go.mod index f6277e02..42a9e67d 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,10 @@ module github.com/ArtisanCloud/go-wechat go 1.16 +replace ( + github.com/ArtisanCloud/go-libs => ../go-libs +) + require ( github.com/ArtisanCloud/go-libs v1.0.9 // indirect github.com/ArtisanCloud/ubt-go v1.0.4 // indirect diff --git a/src/kernel/accessToken.go b/src/kernel/accessToken.go index 400ef1f0..6986000e 100644 --- a/src/kernel/accessToken.go +++ b/src/kernel/accessToken.go @@ -1,8 +1,13 @@ package kernel import ( - "errors" + "crypto/md5" + "encoding/json" + "fmt" + httpContract "github.com/ArtisanCloud/go-libs/http/contract" + "github.com/ArtisanCloud/go-libs/exception" "github.com/ArtisanCloud/go-libs/object" + "github.com/ArtisanCloud/go-wechat/src/kernel/contract" "reflect" "time" ) @@ -43,6 +48,7 @@ func (component *AccessToken) GetToken() (token *object.HashMap) { //cache:= GetCache() response := component.requestToken(component.GetCredentials(), true) + return nil typeResponse := reflect.TypeOf(response) //fmt.Printf("response name:%s, type: %s \n",typeResponse.Name(), typeResponse.Kind() ) if typeResponse.Name() == "ResponseInterface" { @@ -52,26 +58,79 @@ func (component *AccessToken) GetToken() (token *object.HashMap) { return token } -func (component *AccessToken) SetToken(token string, lifeTime time.Duration) (token *object.HashMap, err error) { +func (component *AccessToken) SetToken(token string, lifeTime time.Duration) (*object.HashMap, error) { if lifeTime <= 0 { lifeTime = 7200 * time.Second } - cache := component.GetCache() - cache.Set(component.GetCacheKey(), map[string]interface{}{ - component.TokenKey: token, - "expires_in": lifeTime, - }) + //cache := component.GetCache() + //cache.Set(component.GetCacheKey(), map[string]interface{}{ + // component.TokenKey: token, + // "expires_in": lifeTime, + //}) + // + //if !cache.Has(component.GetCacheKey()) { + // err = errors.New("Failed to cache access token.") + //} + //return token, err + return nil, nil - if !cache.Has(component.GetCacheKey()) { - err = errors.New("Failed to cache access token.") - } - return token, err +} + +func (component *AccessToken) Refresh() *contract.AccessTokenInterface { + return nil } func (component *AccessToken) requestToken(credentials *object.StringMap, toArray bool) interface{} { - //fmt.Printf("credentials:%v \n", credentials) + fmt.Printf("credentials:%v \n", credentials) + + return nil +} + +func (component *AccessToken) ApplyToRequest(request httpContract.RequestInterface, requestOptions object.HashMap) *httpContract.RequestInterface { + return nil +} +func (component *AccessToken) sendRequest(credential *object.HashMap) *httpContract.ResponseContract { return nil } + +func (component *AccessToken) getCacheKey() string { + data, _ := json.Marshal(component.GetCredentials()) + buffer := md5.Sum(data) + return component.CachePrefix + string(buffer[:]) +} + +func (component *AccessToken) getQuery() interface{} { + if component.QueryName != "" { + return []string{component.QueryName} + } else { + arrayReturn := []object.HashMap{} + token := component.GetToken() + result := object.HashMap{ + component.TokenKey: (*token)[component.TokenKey], + } + arrayReturn = append(arrayReturn, result) + + return arrayReturn + + } +} + +func (component *AccessToken) getEndpoint() string { + defer (&exception.Exception{}).HandleException(nil, "accessToken.get.endpoint", component) + if component.EndpointToGetToken == "" { + panic("No endpoint for access token request.") + return "" + } + + return component.EndpointToGetToken +} + +func (component *AccessToken)getTokenKey() string{ + return component.TokenKey +} + + + diff --git a/src/kernel/contract/AccessTokenInterface.go b/src/kernel/contract/AccessTokenInterface.go deleted file mode 100644 index a72bcf22..00000000 --- a/src/kernel/contract/AccessTokenInterface.go +++ /dev/null @@ -1,13 +0,0 @@ -package contract - -import ( - "github.com/ArtisanCloud/go-libs/object" - "github.com/ArtisanCloud/go-wechat/src/kernel/http/contract" -) - -type AccessTokenInterface interface { - GetToken() string - Refresh() AccessTokenInterface - ApplyToRequest(request contract.RequestInterface, requestOptions object.HashMap) contract.RequestInterface - -} \ No newline at end of file diff --git a/src/kernel/contract/accessTokenInterface.go b/src/kernel/contract/accessTokenInterface.go new file mode 100644 index 00000000..9ba78122 --- /dev/null +++ b/src/kernel/contract/accessTokenInterface.go @@ -0,0 +1,14 @@ +package contract + +import ( + "github.com/ArtisanCloud/go-libs/http/contract" + "github.com/ArtisanCloud/go-libs/object" +) + +type ( + AccessTokenInterface interface { + GetToken() string + Refresh() AccessTokenInterface + ApplyToRequest(request contract.RequestInterface, requestOptions object.HashMap) contract.RequestInterface + } +) \ No newline at end of file diff --git a/src/kernel/http/Contract/clientInterface.go b/src/kernel/http/Contract/clientInterface.go deleted file mode 100644 index 11df10af..00000000 --- a/src/kernel/http/Contract/clientInterface.go +++ /dev/null @@ -1,13 +0,0 @@ -package contract - -import ( - "github.com/ArtisanCloud/go-libs/object" -) - -type ClientInterface interface { - Send(request RequestInterface, options object.HashMap) ResponseContract - SendAsync(request RequestInterface, options object.HashMap) PromiseInterface - - Request(method string, uri string, options object.HashMap) ResponseContract - RequestAsync(method string, uri string, options object.HashMap) PromiseInterface -} diff --git a/src/kernel/http/Contract/requestInterface.go b/src/kernel/http/Contract/requestInterface.go deleted file mode 100644 index 78ebbe14..00000000 --- a/src/kernel/http/Contract/requestInterface.go +++ /dev/null @@ -1,5 +0,0 @@ -package contract - -type RequestInterface interface { - -} \ No newline at end of file diff --git a/src/kernel/http/Contract/responseInterface.go b/src/kernel/http/Contract/responseInterface.go deleted file mode 100644 index 5bab34ff..00000000 --- a/src/kernel/http/Contract/responseInterface.go +++ /dev/null @@ -1,13 +0,0 @@ -package contract - -import "github.com/ArtisanCloud/go-libs/object" - -type ResponseContract interface { - GetStatusCode() int - WithStatus() object.HashMap - GetReasonPhrase() object.HashMap -} - -type PromiseInterface interface { - -} \ No newline at end of file diff --git a/src/kernel/http/baseClient.go b/src/kernel/http/baseClient.go deleted file mode 100644 index 38992259..00000000 --- a/src/kernel/http/baseClient.go +++ /dev/null @@ -1,62 +0,0 @@ -package http - -import ( - "github.com/ArtisanCloud/go-libs/object" - "github.com/ArtisanCloud/go-wechat/src/kernel" - "github.com/ArtisanCloud/go-wechat/src/kernel/http/contract" -) - -type BaseClient struct { - App *kernel.ServiceContainer - - httpClient *contract.ClientInterface -} - -func (client *BaseClient) HttpGet(url string, query object.StringMap) interface{} { - return client.Request( - url, - "GET", - object.HashMap{ - "query": query, - }, - false, - ) -} - -func (client *BaseClient) HttpPost(url string, data object.HashMap) interface{} { - return client.Request( - url, - "POST", - object.HashMap{ - "form_params": data, - }, - false, - ) -} - -func (client *BaseClient) HttpPostJson(url string, data object.HashMap, query object.StringMap) interface{} { - return client.Request( - url, - "POST", - object.HashMap{ - "query": query, - "form_params": data, - }, - false, - ) -} - -func (client *BaseClient) Request(url string, method string, options object.HashMap, returnRaw bool) interface{} { - - // to be setup middleware here - - // - response := client.performRequest(url, method, options) - - if returnRaw { - return response - } else { - client.castResponseToType(response, client.App.Config[1]["response_type"].(string)) - } - return nil -} diff --git a/src/kernel/http/request.go b/src/kernel/http/request.go deleted file mode 100644 index f25bd67d..00000000 --- a/src/kernel/http/request.go +++ /dev/null @@ -1,36 +0,0 @@ -package http - -import ( - "github.com/ArtisanCloud/go-libs/object" - "github.com/ArtisanCloud/go-libs/str" - "github.com/ArtisanCloud/go-wechat/src/kernel/http/contract" -) - -var _defaults *object.HashMap - -func SetDefaultOptions(defaults *object.HashMap) { - _defaults = defaults -} - -func GetDefaultOptions() *object.HashMap { - return _defaults -} - -func (client *BaseClient) performRequest(url string, method string, options object.HashMap) contract.ResponseContract { - // change method string format - method = str.Lower(method) - - // merge options with default options - - response := client.GetHttpClient().Request(method, url, options) - return response -} - -func (client *BaseClient) SetHttpClient(httpClient *contract.ClientInterface) *BaseClient { - client.httpClient = httpClient - return client -} - -func (client *BaseClient) GetHttpClient() contract.ClientInterface { - return nil -} diff --git a/src/kernel/http/response.go b/src/kernel/http/response.go deleted file mode 100644 index a7b7282c..00000000 --- a/src/kernel/http/response.go +++ /dev/null @@ -1,10 +0,0 @@ -package http - -import ( - "github.com/ArtisanCloud/go-wechat/src/kernel/http/contract" -) - -func (client *BaseClient) castResponseToType(response contract.ResponseContract, responseType string) interface{} { - - return nil -} diff --git a/src/work/auth/accessToken.go b/src/work/auth/accessToken.go index 1a43f837..92b6748b 100644 --- a/src/work/auth/accessToken.go +++ b/src/work/auth/accessToken.go @@ -8,31 +8,29 @@ import ( ) type AccessToken struct { - BaseAccessToken *kernel.AccessToken - + *kernel.AccessToken } func NewAccessToken(container *kernel.ServiceContainer) *AccessToken { token := &AccessToken{ - BaseAccessToken: kernel.NewAccessToken(container), + kernel.NewAccessToken(container), } - token.BaseAccessToken.EndpointToGetToken = "cgi-bin/gettoken" - token.SetupCredentials() + // Override fields and functions + token.EndpointToGetToken = "cgi-bin/gettoken" + token.OverrideGetCredentials() return token } - -func (component *AccessToken) SetupCredentials(){ - config := component.BaseAccessToken.App.GetConfig() - component.BaseAccessToken.GetCredentials = func() *object.StringMap { - +// Override GetCredentials +func (component *AccessToken) OverrideGetCredentials() { + config := component.App.GetConfig() + component.GetCredentials = func() *object.StringMap { return &object.StringMap{ "corpid": config[kernel.CONFIG_USER_INDEX]["corp_id"].(string), "corpsecret": config[kernel.CONFIG_USER_INDEX]["secret"].(string), } } - } diff --git a/src/work/base/client.go b/src/work/base/client.go index c5fbbbac..33e12422 100644 --- a/src/work/base/client.go +++ b/src/work/base/client.go @@ -1,13 +1,13 @@ package base import ( - "github.com/ArtisanCloud/go-wechat/src/kernel/http" + "github.com/ArtisanCloud/go-libs/http" ) type Client struct { - BaseClient *http.BaseClient + *http.BaseClient } func (comp *Client) GetCallbackIp() interface{} { - return comp.BaseClient.HttpGet("cgi-bin/getcallbackip", nil) + return comp.HttpGet("cgi-bin/getcallbackip", nil) } diff --git a/src/work/base/provider.go b/src/work/base/provider.go index 2c018313..d5736170 100644 --- a/src/work/base/provider.go +++ b/src/work/base/provider.go @@ -10,7 +10,7 @@ func Inject(app contract.ApplicationInterface) { container.GetConfig() component := &Client{ - BaseClient: &http.BaseClient{ + &http.BaseClient{ App: container, }, } From 563bf1899eb49c800787ebc8473b65ad1921e6ef Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Tue, 8 Jun 2021 17:45:19 +0800 Subject: [PATCH 11/25] update --- go.mod | 10 ++-- go.sum | 16 ++++++ main.go | 22 +++++--- src/kernel/accessToken.go | 18 +++--- src/kernel/baseClient.go | 91 ++++++++++++++++++++++++++++++ src/kernel/contract/application.go | 13 ----- src/kernel/serviceContainer.go | 34 ++++++----- src/work/application.go | 21 ++++--- src/work/auth/accessToken.go | 4 +- src/work/auth/provider.go | 4 +- src/work/base/client.go | 4 +- src/work/base/provider.go | 9 +-- 12 files changed, 179 insertions(+), 67 deletions(-) create mode 100644 src/kernel/baseClient.go delete mode 100644 src/kernel/contract/application.go diff --git a/go.mod b/go.mod index 42a9e67d..a50c65fe 100644 --- a/go.mod +++ b/go.mod @@ -2,11 +2,13 @@ module github.com/ArtisanCloud/go-wechat go 1.16 -replace ( - github.com/ArtisanCloud/go-libs => ../go-libs -) +replace github.com/ArtisanCloud/go-libs => ../go-libs require ( - github.com/ArtisanCloud/go-libs v1.0.9 // indirect + github.com/ArtisanCloud/go-libs v1.0.9 github.com/ArtisanCloud/ubt-go v1.0.4 // indirect + github.com/mattn/go-isatty v0.0.13 // indirect + golang.org/x/net v0.0.0-20210525063256-abc453219eb5 // indirect + golang.org/x/sys v0.0.0-20210608053332-aa57babbf139 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index 4e4af732..07c4ee31 100644 --- a/go.sum +++ b/go.sum @@ -33,6 +33,7 @@ github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrU github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= @@ -42,6 +43,8 @@ github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/guonaihong/gout v0.1.8 h1:upJrG4yB4HxKt2XIeIZ/JYK7WtGcwPXl+2xiFkP7VnU= github.com/guonaihong/gout v0.1.8/go.mod h1:P6P8+0+toYgmhFqzLxVde+9vQbCDHrxn56V9TglC5io= +github.com/guonaihong/gout v0.1.11 h1:Sl4OwyNtp1BoMZXankNC0DimAPuh9kgBsZIGynD9kiw= +github.com/guonaihong/gout v0.1.11/go.mod h1:JkjNv1G2oRWvFgP/r4DUbYhoeIBB0zMP2j1ID+5CYpU= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/now v1.1.2/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= @@ -51,6 +54,8 @@ github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-isatty v0.0.13 h1:qdl+GuBjcsKKDco5BsxPJlId98mSWNKqYA+Co0SC1yA= +github.com/mattn/go-isatty v0.0.13/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= @@ -93,6 +98,8 @@ golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/ golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210525063256-abc453219eb5 h1:wjuX4b5yYQnEQHzd+CBcrcC6OVR2J1CN6mUy0oSxIPo= +golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -108,10 +115,14 @@ golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210608053332-aa57babbf139 h1:C+AwYEtBp/VQwoLntUmQ/yx3MS9vmZaKNdw5eOpoQe8= +golang.org/x/sys v0.0.0-20210608053332-aa57babbf139/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= @@ -125,6 +136,9 @@ google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQ google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= @@ -133,5 +147,7 @@ gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gorm.io/gorm v1.21.6/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0= diff --git a/main.go b/main.go index 8ab762e6..5b7a6451 100644 --- a/main.go +++ b/main.go @@ -5,24 +5,30 @@ import ( fmt2 "github.com/ArtisanCloud/go-libs/fmt" "github.com/ArtisanCloud/go-libs/object" "github.com/ArtisanCloud/go-wechat/src/work" - "github.com/ArtisanCloud/go-wechat/src/work/auth" ) func main() { fmt.Printf("hello Wechat! \n") - config := object.HashMap{ + config := &object.HashMap{ "corp_id": "ww454dfb9d6f6d432a", //'agent_id' : 100020, - "secret": "", + "secret": "9iZxAhtyKC9OB0ofN8Qrvjy4Kk80nuoJ7nIbocE5j8M", } app := work.NewWork(config) - fmt2.Dump(app) - components := app.GetComponents() - token := (*components)["access_token"].(*auth.AccessToken).BaseAccessToken.GetToken() - fmt2.Dump(token) - //fmt2.Dump(app.Container.GetConfig()) + //fmt2.Dump(app) + //components := app.GetComponents() + //token := (*components)["access_token"].(*auth.AccessToken).GetToken() + //fmt2.Dump(token) + fmt2.Dump(app.GetConfig()) + + //cType := reflect.TypeOf((*app.Components)["base"].(*base.Client)) + //fmt.Printf("kind %s \n", cType.Kind()) + //fmt.Printf("type %v \n", cType) + + //ips :=(*app.Components)["base"].(*base.Client).GetCallbackIp() + //fmt2.Dump(ips) } diff --git a/src/kernel/accessToken.go b/src/kernel/accessToken.go index 6986000e..9fd34c22 100644 --- a/src/kernel/accessToken.go +++ b/src/kernel/accessToken.go @@ -4,8 +4,8 @@ import ( "crypto/md5" "encoding/json" "fmt" - httpContract "github.com/ArtisanCloud/go-libs/http/contract" "github.com/ArtisanCloud/go-libs/exception" + httpContract "github.com/ArtisanCloud/go-libs/http/contract" "github.com/ArtisanCloud/go-libs/object" "github.com/ArtisanCloud/go-wechat/src/kernel/contract" "reflect" @@ -88,8 +88,15 @@ func (component *AccessToken) requestToken(credentials *object.StringMap, toArra return nil } -func (component *AccessToken) ApplyToRequest(request httpContract.RequestInterface, requestOptions object.HashMap) *httpContract.RequestInterface { - return nil +func (component *AccessToken) ApplyToRequest(request *httpContract.RequestInterface, requestOptions object.HashMap) *httpContract.RequestInterface { + // parse query + //strURL := request.GetUri().GetQuery() + //strURL = parsestring(strURL) + + // merge query with query again + //query = http + + return request } func (component *AccessToken) sendRequest(credential *object.HashMap) *httpContract.ResponseContract { @@ -128,9 +135,6 @@ func (component *AccessToken) getEndpoint() string { return component.EndpointToGetToken } -func (component *AccessToken)getTokenKey() string{ +func (component *AccessToken) getTokenKey() string { return component.TokenKey } - - - diff --git a/src/kernel/baseClient.go b/src/kernel/baseClient.go new file mode 100644 index 00000000..a039f5f0 --- /dev/null +++ b/src/kernel/baseClient.go @@ -0,0 +1,91 @@ +package kernel + +import ( + "github.com/ArtisanCloud/go-libs/http" + "github.com/ArtisanCloud/go-libs/object" +) + +type BaseClient struct { + *http.HttpRequest + *http.HttpResponse + + App *ServiceContainer + + AccessToken *AccessToken +} + +func NewBaseClient(app *ServiceContainer, token *AccessToken) *BaseClient { + client := &BaseClient{ + HttpRequest: http.NewHttpRequest(app.GetConfig()), + App: app, + AccessToken: token, + } + return client + +} + +func (client *BaseClient) HttpGet(url string, query object.StringMap) interface{} { + return client.Request( + url, + "GET", + object.HashMap{ + "query": query, + }, + false, + ) +} + +func (client *BaseClient) HttpPost(url string, data object.HashMap) interface{} { + return client.Request( + url, + "POST", + object.HashMap{ + "form_params": data, + }, + false, + ) +} + +func (client *BaseClient) HttpPostJson(url string, data object.HashMap, query object.StringMap) interface{} { + return client.Request( + url, + "POST", + object.HashMap{ + "query": query, + "form_params": data, + }, + false, + ) +} + +func (client *BaseClient) Request(url string, method string, options object.HashMap, returnRaw bool) interface{} { + + // to be setup middleware here + if client.Middlewares != nil { + client.registerHttpMiddlewares() + } + // + response := client.PerformRequest(url, method, options) + + if returnRaw { + return response + } else { + client.CastResponseToType(response, (*client.App.Config)["response_type"].(string)) + } + return response +} + +func (client *BaseClient) registerHttpMiddlewares() { + // retry + //client.pushMiddleware(client.retryMiddleware(), "retry") + //// access token + //client.pushMiddleware(client.accessTokenMiddleware(), "access_token") + //// log + //client.pushMiddleware(client.logMiddleware(), "log") +} + +func (client *BaseClient) accessTokenMiddleware() func() { + return func() { + + } +} diff --git a/src/kernel/contract/application.go b/src/kernel/contract/application.go deleted file mode 100644 index 704ee598..00000000 --- a/src/kernel/contract/application.go +++ /dev/null @@ -1,13 +0,0 @@ -package contract - -import ( - "github.com/ArtisanCloud/go-libs/object" - "github.com/ArtisanCloud/go-wechat/src/kernel" -) - -type ApplicationInterface interface { - - GetComponents() *object.HashMap - - GetContainer() *kernel.ServiceContainer -} diff --git a/src/kernel/serviceContainer.go b/src/kernel/serviceContainer.go index c8fefaa2..554dff4b 100644 --- a/src/kernel/serviceContainer.go +++ b/src/kernel/serviceContainer.go @@ -2,16 +2,23 @@ package kernel import "github.com/ArtisanCloud/go-libs/object" -const CONFIG_BASE_INDEX = 0 -const CONFIG_DEFAULT_INDEX = 1 -const CONFIG_USER_INDEX = 2 + + +type ApplicationInterface interface { + + GetComponents() *object.HashMap + GetContainer() *ServiceContainer + GetApp() ApplicationInterface +} + + type ServiceContainer struct { ID int - DefaultConfig object.HashMap - UserConfig object.HashMap - Config []object.HashMap + DefaultConfig *object.HashMap + UserConfig *object.HashMap + Config *object.HashMap } func (container *ServiceContainer) getBaseConfig() object.HashMap { @@ -24,18 +31,17 @@ func (container *ServiceContainer) getBaseConfig() object.HashMap { } } -func (container *ServiceContainer) GetConfig() []object.HashMap { +func (container *ServiceContainer) GetConfig() *object.HashMap { // if container config has been setup if container.Config!=nil{ return container.Config } - // setup the container config firstly - baseConfig := container.getBaseConfig() - container.Config = []object.HashMap{ - baseConfig, - container.DefaultConfig, - container.UserConfig, - } + container.Config = &object.HashMap{} + + // merge config + container.Config = object.MergeHashMap(container.DefaultConfig,container.Config) + container.Config = object.MergeHashMap(container.UserConfig,container.Config) + return container.Config } diff --git a/src/work/application.go b/src/work/application.go index 6f559458..588b733c 100644 --- a/src/work/application.go +++ b/src/work/application.go @@ -8,16 +8,16 @@ import ( ) type Work struct { - Container *kernel.ServiceContainer + *kernel.ServiceContainer Components *object.HashMap } -func NewWork(config object.HashMap) *Work { +func NewWork(config *object.HashMap) *Work { app := &Work{ - Container: &kernel.ServiceContainer{ + ServiceContainer:&kernel.ServiceContainer{ UserConfig: config, - DefaultConfig: object.HashMap{ + DefaultConfig: &object.HashMap{ "http": map[string]string{ "base_uri": "https://qyapi.weixin.qq.com/", }, @@ -30,14 +30,17 @@ func NewWork(config object.HashMap) *Work { auth.Inject(app) base.Inject(app) - return app } -func (app *Work)GetComponents() *object.HashMap{ +func (app *Work) GetComponents() *object.HashMap { return app.Components } -func (app *Work) GetContainer() *kernel.ServiceContainer{ - return app.Container -} \ No newline at end of file +func (app *Work) GetContainer() *kernel.ServiceContainer { + return app.ServiceContainer +} + +func (app *Work) GetApp() kernel.ApplicationInterface { + return app +} diff --git a/src/work/auth/accessToken.go b/src/work/auth/accessToken.go index 92b6748b..f0c23cfa 100644 --- a/src/work/auth/accessToken.go +++ b/src/work/auth/accessToken.go @@ -29,8 +29,8 @@ func (component *AccessToken) OverrideGetCredentials() { config := component.App.GetConfig() component.GetCredentials = func() *object.StringMap { return &object.StringMap{ - "corpid": config[kernel.CONFIG_USER_INDEX]["corp_id"].(string), - "corpsecret": config[kernel.CONFIG_USER_INDEX]["secret"].(string), + "corpid": (*config)["corp_id"].(string), + "corpsecret": (*config)["secret"].(string), } } } diff --git a/src/work/auth/provider.go b/src/work/auth/provider.go index a506ed7f..6c48c372 100644 --- a/src/work/auth/provider.go +++ b/src/work/auth/provider.go @@ -1,10 +1,10 @@ package auth import ( - "github.com/ArtisanCloud/go-wechat/src/kernel/contract" + "github.com/ArtisanCloud/go-wechat/src/kernel" ) -func Inject(app contract.ApplicationInterface) { +func Inject(app kernel.ApplicationInterface) { container := app.GetContainer() container.GetConfig() diff --git a/src/work/base/client.go b/src/work/base/client.go index 33e12422..43578692 100644 --- a/src/work/base/client.go +++ b/src/work/base/client.go @@ -1,11 +1,11 @@ package base import ( - "github.com/ArtisanCloud/go-libs/http" + "github.com/ArtisanCloud/go-wechat/src/kernel" ) type Client struct { - *http.BaseClient + *kernel.BaseClient } func (comp *Client) GetCallbackIp() interface{} { diff --git a/src/work/base/provider.go b/src/work/base/provider.go index d5736170..3858160d 100644 --- a/src/work/base/provider.go +++ b/src/work/base/provider.go @@ -1,18 +1,15 @@ package base import ( - "github.com/ArtisanCloud/go-wechat/src/kernel/contract" - "github.com/ArtisanCloud/go-wechat/src/kernel/http" + "github.com/ArtisanCloud/go-wechat/src/kernel" ) -func Inject(app contract.ApplicationInterface) { +func Inject(app kernel.ApplicationInterface) { container := app.GetContainer() container.GetConfig() component := &Client{ - &http.BaseClient{ - App: container, - }, + kernel.NewBaseClient(container, nil), } components := app.GetComponents() From 1c4e4489f1cca89c627d19fac3f0a1bf9b022e70 Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Tue, 8 Jun 2021 17:59:31 +0800 Subject: [PATCH 12/25] inject app into compoments --- main.go | 9 +++++---- src/kernel/accessToken.go | 6 +++--- src/kernel/baseClient.go | 10 ++++++---- src/kernel/serviceContainer.go | 1 - src/work/auth/accessToken.go | 6 +++--- src/work/auth/provider.go | 2 +- src/work/base/provider.go | 2 +- 7 files changed, 19 insertions(+), 17 deletions(-) diff --git a/main.go b/main.go index 5b7a6451..55d5b4f8 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( fmt2 "github.com/ArtisanCloud/go-libs/fmt" "github.com/ArtisanCloud/go-libs/object" "github.com/ArtisanCloud/go-wechat/src/work" + "github.com/ArtisanCloud/go-wechat/src/work/base" ) func main() { @@ -20,15 +21,15 @@ func main() { app := work.NewWork(config) //fmt2.Dump(app) //components := app.GetComponents() - //token := (*components)["access_token"].(*auth.AccessToken).GetToken() + //token := (*app.Components)["access_token"].(*auth.AccessToken).GetToken() //fmt2.Dump(token) - fmt2.Dump(app.GetConfig()) + //fmt2.Dump(app.GetConfig()) //cType := reflect.TypeOf((*app.Components)["base"].(*base.Client)) //fmt.Printf("kind %s \n", cType.Kind()) //fmt.Printf("type %v \n", cType) - //ips :=(*app.Components)["base"].(*base.Client).GetCallbackIp() - //fmt2.Dump(ips) + ips :=(*app.Components)["base"].(*base.Client).GetCallbackIp() + fmt2.Dump(ips) } diff --git a/src/kernel/accessToken.go b/src/kernel/accessToken.go index 9fd34c22..8b446b67 100644 --- a/src/kernel/accessToken.go +++ b/src/kernel/accessToken.go @@ -13,7 +13,7 @@ import ( ) type AccessToken struct { - App *ServiceContainer + App *ApplicationInterface RequestMethod string EndpointToGetToken string @@ -25,9 +25,9 @@ type AccessToken struct { GetCredentials func() *object.StringMap } -func NewAccessToken(container *ServiceContainer) *AccessToken { +func NewAccessToken(app *ApplicationInterface) *AccessToken { token := &AccessToken{ - App: container, + App: app, RequestMethod: "GET", EndpointToGetToken: "", QueryName: "", diff --git a/src/kernel/baseClient.go b/src/kernel/baseClient.go index a039f5f0..e37a19f8 100644 --- a/src/kernel/baseClient.go +++ b/src/kernel/baseClient.go @@ -9,14 +9,15 @@ type BaseClient struct { *http.HttpRequest *http.HttpResponse - App *ServiceContainer + App *ApplicationInterface AccessToken *AccessToken } -func NewBaseClient(app *ServiceContainer, token *AccessToken) *BaseClient { +func NewBaseClient(app *ApplicationInterface, token *AccessToken) *BaseClient { + config := (*app).GetContainer().GetConfig() client := &BaseClient{ - HttpRequest: http.NewHttpRequest(app.GetConfig()), + HttpRequest: http.NewHttpRequest(config), App: app, AccessToken: token, } @@ -70,7 +71,8 @@ func (client *BaseClient) Request(url string, method string, options object.Hash if returnRaw { return response } else { - client.CastResponseToType(response, (*client.App.Config)["response_type"].(string)) + config := *(*client.App).GetContainer().GetConfig() + client.CastResponseToType(response, config["response_type"].(string)) } return response } diff --git a/src/kernel/serviceContainer.go b/src/kernel/serviceContainer.go index 554dff4b..345c71d3 100644 --- a/src/kernel/serviceContainer.go +++ b/src/kernel/serviceContainer.go @@ -8,7 +8,6 @@ type ApplicationInterface interface { GetComponents() *object.HashMap GetContainer() *ServiceContainer - GetApp() ApplicationInterface } diff --git a/src/work/auth/accessToken.go b/src/work/auth/accessToken.go index f0c23cfa..9f4ee434 100644 --- a/src/work/auth/accessToken.go +++ b/src/work/auth/accessToken.go @@ -11,9 +11,9 @@ type AccessToken struct { *kernel.AccessToken } -func NewAccessToken(container *kernel.ServiceContainer) *AccessToken { +func NewAccessToken(app *kernel.ApplicationInterface) *AccessToken { token := &AccessToken{ - kernel.NewAccessToken(container), + kernel.NewAccessToken(app), } // Override fields and functions @@ -26,7 +26,7 @@ func NewAccessToken(container *kernel.ServiceContainer) *AccessToken { // Override GetCredentials func (component *AccessToken) OverrideGetCredentials() { - config := component.App.GetConfig() + config := (*component.App).GetContainer().GetConfig() component.GetCredentials = func() *object.StringMap { return &object.StringMap{ "corpid": (*config)["corp_id"].(string), diff --git a/src/work/auth/provider.go b/src/work/auth/provider.go index 6c48c372..ae8303df 100644 --- a/src/work/auth/provider.go +++ b/src/work/auth/provider.go @@ -8,7 +8,7 @@ func Inject(app kernel.ApplicationInterface) { container := app.GetContainer() container.GetConfig() - component := NewAccessToken(container) + component := NewAccessToken(&app) components := app.GetComponents() (*components)["access_token"] = component diff --git a/src/work/base/provider.go b/src/work/base/provider.go index 3858160d..d49a373c 100644 --- a/src/work/base/provider.go +++ b/src/work/base/provider.go @@ -9,7 +9,7 @@ func Inject(app kernel.ApplicationInterface) { container.GetConfig() component := &Client{ - kernel.NewBaseClient(container, nil), + kernel.NewBaseClient(&app, nil), } components := app.GetComponents() From ef0c694ef364ad5ce275d7d6bcc7308885f8a2ce Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Tue, 8 Jun 2021 18:04:27 +0800 Subject: [PATCH 13/25] update --- .gitignore | 4 +++- main.go | 7 +------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index c47138d9..9f78aa5c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ .DS_Store environment.yml -environment.yaml \ No newline at end of file +environment.yaml + +temp.go \ No newline at end of file diff --git a/main.go b/main.go index 55d5b4f8..981e4c7c 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main import ( "fmt" fmt2 "github.com/ArtisanCloud/go-libs/fmt" - "github.com/ArtisanCloud/go-libs/object" "github.com/ArtisanCloud/go-wechat/src/work" "github.com/ArtisanCloud/go-wechat/src/work/base" ) @@ -12,11 +11,7 @@ func main() { fmt.Printf("hello Wechat! \n") - config := &object.HashMap{ - "corp_id": "ww454dfb9d6f6d432a", - //'agent_id' : 100020, - "secret": "9iZxAhtyKC9OB0ofN8Qrvjy4Kk80nuoJ7nIbocE5j8M", - } + config := GetConfig() app := work.NewWork(config) //fmt2.Dump(app) From da066a8851562f46810962cf725b2489b9f29f58 Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Tue, 8 Jun 2021 18:06:27 +0800 Subject: [PATCH 14/25] update --- main.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 981e4c7c..a8f1db7f 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,6 @@ import ( "fmt" fmt2 "github.com/ArtisanCloud/go-libs/fmt" "github.com/ArtisanCloud/go-wechat/src/work" - "github.com/ArtisanCloud/go-wechat/src/work/base" ) func main() { @@ -18,13 +17,13 @@ func main() { //components := app.GetComponents() //token := (*app.Components)["access_token"].(*auth.AccessToken).GetToken() //fmt2.Dump(token) - //fmt2.Dump(app.GetConfig()) + fmt2.Dump(app.GetConfig()) //cType := reflect.TypeOf((*app.Components)["base"].(*base.Client)) //fmt.Printf("kind %s \n", cType.Kind()) //fmt.Printf("type %v \n", cType) - ips :=(*app.Components)["base"].(*base.Client).GetCallbackIp() - fmt2.Dump(ips) + //ips :=(*app.Components)["base"].(*base.Client).GetCallbackIp() + //fmt2.Dump(ips) } From ec7a473e420303588eb2ae8287135b2b5c7629f6 Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Tue, 8 Jun 2021 23:48:56 +0800 Subject: [PATCH 15/25] update --- go.mod | 2 +- go.sum | 27 +++++++++------------------ main.go | 2 +- src/kernel/baseClient.go | 2 +- src/work/application.go | 2 ++ 5 files changed, 14 insertions(+), 21 deletions(-) diff --git a/go.mod b/go.mod index a50c65fe..4bd5ccb0 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ replace github.com/ArtisanCloud/go-libs => ../go-libs require ( github.com/ArtisanCloud/go-libs v1.0.9 - github.com/ArtisanCloud/ubt-go v1.0.4 // indirect + github.com/gin-gonic/gin v1.7.1 // indirect github.com/mattn/go-isatty v0.0.13 // indirect golang.org/x/net v0.0.0-20210525063256-abc453219eb5 // indirect golang.org/x/sys v0.0.0-20210608053332-aa57babbf139 // indirect diff --git a/go.sum b/go.sum index 07c4ee31..eb276564 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,8 @@ -github.com/ArtisanCloud/go-libs v1.0.9 h1:lF6lx8qZ7OIhvbVHt9SEJMBtn+jBfa7ZthMFL5Xwozc= -github.com/ArtisanCloud/go-libs v1.0.9/go.mod h1:NMCmLHB/v5cWOr62XMqIn0Q+N6m7UfGdx/bP++Y2kKM= -github.com/ArtisanCloud/ubt-go v1.0.4 h1:pEdYcP1+6SYzFkSyciRse8ORgWJGFg39thJjbvYLHEc= -github.com/ArtisanCloud/ubt-go v1.0.4/go.mod h1:eQJ8xHNbsYhMc1N353bDSCw2hHKPGfWV3UIwCMsg4uQ= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= -github.com/elliotchance/phpserialize v1.3.0/go.mod h1:gt7XX9+ETUcLXbtTKEuyrqW3lcLUAeS/AnGZ2e49TZs= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= @@ -25,7 +21,6 @@ github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn github.com/go-redis/redis/v8 v8.8.0/go.mod h1:F7resOH5Kdug49Otu24RjHWwgK7u9AmtqWMnCV1iP5Y= github.com/golang-module/carbon v1.3.5/go.mod h1:wIIzB2BwewCNV4KyXfAASbAIsCJpwceR9JwVrgqdLIw= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I= github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= @@ -33,26 +28,22 @@ github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrU github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/guonaihong/gout v0.1.8 h1:upJrG4yB4HxKt2XIeIZ/JYK7WtGcwPXl+2xiFkP7VnU= -github.com/guonaihong/gout v0.1.8/go.mod h1:P6P8+0+toYgmhFqzLxVde+9vQbCDHrxn56V9TglC5io= github.com/guonaihong/gout v0.1.11 h1:Sl4OwyNtp1BoMZXankNC0DimAPuh9kgBsZIGynD9kiw= github.com/guonaihong/gout v0.1.11/go.mod h1:JkjNv1G2oRWvFgP/r4DUbYhoeIBB0zMP2j1ID+5CYpU= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= -github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= -github.com/jinzhu/now v1.1.2/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= -github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.13 h1:qdl+GuBjcsKKDco5BsxPJlId98mSWNKqYA+Co0SC1yA= github.com/mattn/go-isatty v0.0.13/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= @@ -67,12 +58,13 @@ github.com/onsi/ginkgo v1.15.0/go.mod h1:hF8qUzuuC8DJGygJH3726JnCZX4MYbRB8yFfISq github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7mt48= -github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= @@ -85,14 +77,13 @@ go.opentelemetry.io/otel/oteltest v0.19.0/go.mod h1:tI4yxwh8U21v7JD6R3BcA/2+RBoT go.opentelemetry.io/otel/trace v0.19.0/go.mod h1:4IXiNextNOpPnRlI4ryK69mn5iC84bjBWZQA5DXz/qg= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20210314154223-e6e6c4f2bb5b h1:wSOdpTq0/eI46Ez/LkDwIsAKA71YP2SRKBODiRWM0as= golang.org/x/crypto v0.0.0-20210314154223-e6e6c4f2bb5b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa h1:F+8P+gmewFQYRk6JoLQLwjBCTu3mcIURZfNkVweuRKA= golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= @@ -109,7 +100,6 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -129,6 +119,7 @@ golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4f golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= @@ -139,15 +130,15 @@ google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2 google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gorm.io/gorm v1.21.6/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0= diff --git a/main.go b/main.go index a8f1db7f..2cd60317 100644 --- a/main.go +++ b/main.go @@ -23,7 +23,7 @@ func main() { //fmt.Printf("kind %s \n", cType.Kind()) //fmt.Printf("type %v \n", cType) - //ips :=(*app.Components)["base"].(*base.Client).GetCallbackIp() + //ips :=(*app.Base).GetCallbackIp() //fmt2.Dump(ips) } diff --git a/src/kernel/baseClient.go b/src/kernel/baseClient.go index e37a19f8..8b0a403c 100644 --- a/src/kernel/baseClient.go +++ b/src/kernel/baseClient.go @@ -72,7 +72,7 @@ func (client *BaseClient) Request(url string, method string, options object.Hash return response } else { config := *(*client.App).GetContainer().GetConfig() - client.CastResponseToType(response, config["response_type"].(string)) + client.CastResponseToType(response, config["response_type"]) } return response } diff --git a/src/work/application.go b/src/work/application.go index 588b733c..f2f14f78 100644 --- a/src/work/application.go +++ b/src/work/application.go @@ -11,6 +11,8 @@ type Work struct { *kernel.ServiceContainer Components *object.HashMap + Base *base.Client + AccessToken *auth.AccessToken } func NewWork(config *object.HashMap) *Work { From e5a546b8d9928ed18b561956bd2a2903b8c73d73 Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Wed, 9 Jun 2021 18:01:35 +0800 Subject: [PATCH 16/25] add middlewares for request --- go.sum | 1 + main.go | 6 +-- src/kernel/ResponseWX.go | 6 +++ src/kernel/accessToken.go | 2 +- src/kernel/baseClient.go | 71 ++++++++++++++++++++++++-------- src/kernel/contract/component.go | 5 --- src/kernel/serviceContainer.go | 11 +---- src/work/application.go | 18 ++------ src/work/auth/accessToken.go | 6 +-- src/work/auth/provider.go | 9 +--- src/work/base/client.go | 14 ++++++- src/work/base/provider.go | 8 +--- 12 files changed, 90 insertions(+), 67 deletions(-) create mode 100644 src/kernel/ResponseWX.go delete mode 100644 src/kernel/contract/component.go diff --git a/go.sum b/go.sum index eb276564..4ccb4f55 100644 --- a/go.sum +++ b/go.sum @@ -58,6 +58,7 @@ github.com/onsi/ginkgo v1.15.0/go.mod h1:hF8qUzuuC8DJGygJH3726JnCZX4MYbRB8yFfISq github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7mt48= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= diff --git a/main.go b/main.go index 2cd60317..eeaa6b6c 100644 --- a/main.go +++ b/main.go @@ -17,13 +17,13 @@ func main() { //components := app.GetComponents() //token := (*app.Components)["access_token"].(*auth.AccessToken).GetToken() //fmt2.Dump(token) - fmt2.Dump(app.GetConfig()) + //fmt2.Dump(app.GetConfig()) //cType := reflect.TypeOf((*app.Components)["base"].(*base.Client)) //fmt.Printf("kind %s \n", cType.Kind()) //fmt.Printf("type %v \n", cType) - //ips :=(*app.Base).GetCallbackIp() - //fmt2.Dump(ips) + ips := app.Base.GetCallbackIp() + fmt2.Dump(ips) } diff --git a/src/kernel/ResponseWX.go b/src/kernel/ResponseWX.go new file mode 100644 index 00000000..36fc0916 --- /dev/null +++ b/src/kernel/ResponseWX.go @@ -0,0 +1,6 @@ +package kernel + +type ResponseWX struct { + ErrCode int `json:"errcode"` + ErrMSG string `json:"errmsg"` +} diff --git a/src/kernel/accessToken.go b/src/kernel/accessToken.go index 8b446b67..0cc274dc 100644 --- a/src/kernel/accessToken.go +++ b/src/kernel/accessToken.go @@ -88,7 +88,7 @@ func (component *AccessToken) requestToken(credentials *object.StringMap, toArra return nil } -func (component *AccessToken) ApplyToRequest(request *httpContract.RequestInterface, requestOptions object.HashMap) *httpContract.RequestInterface { +func (component *AccessToken) ApplyToRequest(request *httpContract.RequestInterface, requestOptions object.HashMap) httpContract.RequestInterface { // parse query //strURL := request.GetUri().GetQuery() //strURL = parsestring(strURL) diff --git a/src/kernel/baseClient.go b/src/kernel/baseClient.go index 8b0a403c..679a2cb4 100644 --- a/src/kernel/baseClient.go +++ b/src/kernel/baseClient.go @@ -1,8 +1,10 @@ package kernel import ( + "fmt" "github.com/ArtisanCloud/go-libs/http" "github.com/ArtisanCloud/go-libs/object" + http2 "net/http" ) type BaseClient struct { @@ -25,48 +27,51 @@ func NewBaseClient(app *ApplicationInterface, token *AccessToken) *BaseClient { } -func (client *BaseClient) HttpGet(url string, query object.StringMap) interface{} { +func (client *BaseClient) HttpGet(url string, query object.StringMap, outResponse interface{}) interface{} { return client.Request( url, "GET", - object.HashMap{ + &object.HashMap{ "query": query, }, false, + outResponse, ) } -func (client *BaseClient) HttpPost(url string, data object.HashMap) interface{} { +func (client *BaseClient) HttpPost(url string, data object.HashMap, outResponse interface{}) interface{} { return client.Request( url, "POST", - object.HashMap{ + &object.HashMap{ "form_params": data, }, false, + outResponse, ) } -func (client *BaseClient) HttpPostJson(url string, data object.HashMap, query object.StringMap) interface{} { +func (client *BaseClient) HttpPostJson(url string, data object.HashMap, query object.StringMap, outResponse interface{}) interface{} { return client.Request( url, "POST", - object.HashMap{ + &object.HashMap{ "query": query, "form_params": data, }, false, + outResponse, ) } -func (client *BaseClient) Request(url string, method string, options object.HashMap, returnRaw bool) interface{} { +func (client *BaseClient) Request(url string, method string, options *object.HashMap, returnRaw bool, outResponse interface{}) interface{} { // to be setup middleware here - if client.Middlewares != nil { + if client.Middlewares == nil { client.registerHttpMiddlewares() } // - response := client.PerformRequest(url, method, options) + response := client.PerformRequest(url, method, options, outResponse) if returnRaw { return response @@ -78,16 +83,48 @@ func (client *BaseClient) Request(url string, method string, options object.Hash } func (client *BaseClient) registerHttpMiddlewares() { + + client.Middlewares = []interface{}{} + // retry - //client.pushMiddleware(client.retryMiddleware(), "retry") - //// access token - //client.pushMiddleware(client.accessTokenMiddleware(), "access_token") - //// log - //client.pushMiddleware(client.logMiddleware(), "log") + client.PushMiddleware(client.retryMiddleware(), "retry") + // access token + client.PushMiddleware(client.accessTokenMiddleware(), "access_token") + // log + client.PushMiddleware(client.logMiddleware(), "log") + } -func (client *BaseClient) accessTokenMiddleware() func() { - return func() { +// ---------------------------------------------------------------------- +type MiddlewareAccessToken struct{} +type MiddlewareLogMiddleware struct{} +type MiddlewareRetry struct{} - } +func (d *MiddlewareAccessToken) ModifyRequest(req *http2.Request) error { + fmt.Println("accessTokenMiddleware") + return nil +} + +func (d *MiddlewareLogMiddleware) ModifyRequest(req *http2.Request) error { + fmt.Println("logMiddleware") + return nil +} + + +func (d *MiddlewareRetry) ModifyRequest(req *http2.Request) error { + fmt.Println("retryMiddleware") + return nil +} + +func (client *BaseClient) accessTokenMiddleware() interface{} { + + return &MiddlewareAccessToken{} +} + +func (client *BaseClient) logMiddleware() interface{} { + return &MiddlewareLogMiddleware{} +} + +func (client *BaseClient) retryMiddleware() interface{} { + return &MiddlewareRetry{} } diff --git a/src/kernel/contract/component.go b/src/kernel/contract/component.go deleted file mode 100644 index a26b8a52..00000000 --- a/src/kernel/contract/component.go +++ /dev/null @@ -1,5 +0,0 @@ -package contract - -type ComponentInterface interface { - -} \ No newline at end of file diff --git a/src/kernel/serviceContainer.go b/src/kernel/serviceContainer.go index 345c71d3..860e6e05 100644 --- a/src/kernel/serviceContainer.go +++ b/src/kernel/serviceContainer.go @@ -2,16 +2,10 @@ package kernel import "github.com/ArtisanCloud/go-libs/object" - - type ApplicationInterface interface { - - GetComponents() *object.HashMap GetContainer() *ServiceContainer } - - type ServiceContainer struct { ID int @@ -32,15 +26,14 @@ func (container *ServiceContainer) getBaseConfig() object.HashMap { func (container *ServiceContainer) GetConfig() *object.HashMap { // if container config has been setup - if container.Config!=nil{ + if container.Config != nil { return container.Config } container.Config = &object.HashMap{} // merge config - container.Config = object.MergeHashMap(container.DefaultConfig,container.Config) - container.Config = object.MergeHashMap(container.UserConfig,container.Config) + container.Config = object.MergeHashMap(container.Config, container.DefaultConfig, container.UserConfig) return container.Config } diff --git a/src/work/application.go b/src/work/application.go index f2f14f78..27d2f5ae 100644 --- a/src/work/application.go +++ b/src/work/application.go @@ -10,14 +10,13 @@ import ( type Work struct { *kernel.ServiceContainer - Components *object.HashMap - Base *base.Client + Base *base.Client AccessToken *auth.AccessToken } func NewWork(config *object.HashMap) *Work { app := &Work{ - ServiceContainer:&kernel.ServiceContainer{ + ServiceContainer: &kernel.ServiceContainer{ UserConfig: config, DefaultConfig: &object.HashMap{ "http": map[string]string{ @@ -25,24 +24,15 @@ func NewWork(config *object.HashMap) *Work { }, }, }, - Components: &object.HashMap{}, } // inject components - auth.Inject(app) - base.Inject(app) + app.AccessToken = auth.RegisterProvider(app) + app.Base = base.RegisterProvider(app) return app } -func (app *Work) GetComponents() *object.HashMap { - return app.Components -} - func (app *Work) GetContainer() *kernel.ServiceContainer { return app.ServiceContainer } - -func (app *Work) GetApp() kernel.ApplicationInterface { - return app -} diff --git a/src/work/auth/accessToken.go b/src/work/auth/accessToken.go index 9f4ee434..347b55ad 100644 --- a/src/work/auth/accessToken.go +++ b/src/work/auth/accessToken.go @@ -25,9 +25,9 @@ func NewAccessToken(app *kernel.ApplicationInterface) *AccessToken { // Override GetCredentials -func (component *AccessToken) OverrideGetCredentials() { - config := (*component.App).GetContainer().GetConfig() - component.GetCredentials = func() *object.StringMap { +func (accessToken *AccessToken) OverrideGetCredentials() { + config := (*accessToken.App).GetContainer().GetConfig() + accessToken.GetCredentials = func() *object.StringMap { return &object.StringMap{ "corpid": (*config)["corp_id"].(string), "corpsecret": (*config)["secret"].(string), diff --git a/src/work/auth/provider.go b/src/work/auth/provider.go index ae8303df..0bc8a68e 100644 --- a/src/work/auth/provider.go +++ b/src/work/auth/provider.go @@ -4,13 +4,8 @@ import ( "github.com/ArtisanCloud/go-wechat/src/kernel" ) -func Inject(app kernel.ApplicationInterface) { - container := app.GetContainer() - container.GetConfig() +func RegisterProvider(app kernel.ApplicationInterface) *AccessToken { - component := NewAccessToken(&app) - components := app.GetComponents() - - (*components)["access_token"] = component + return NewAccessToken(&app) } diff --git a/src/work/base/client.go b/src/work/base/client.go index 43578692..47ef20f0 100644 --- a/src/work/base/client.go +++ b/src/work/base/client.go @@ -8,6 +8,16 @@ type Client struct { *kernel.BaseClient } -func (comp *Client) GetCallbackIp() interface{} { - return comp.HttpGet("cgi-bin/getcallbackip", nil) +type ResponseGetCallBackIp struct { + IPList []string `json:"ip_list"` + *kernel.ResponseWX +} + +func (comp *Client) GetCallbackIp() *ResponseGetCallBackIp { + + result := &ResponseGetCallBackIp{} + + comp.HttpGet("cgi-bin/getcallbackip", nil, result) + + return result } diff --git a/src/work/base/provider.go b/src/work/base/provider.go index d49a373c..8bd5a714 100644 --- a/src/work/base/provider.go +++ b/src/work/base/provider.go @@ -4,14 +4,10 @@ import ( "github.com/ArtisanCloud/go-wechat/src/kernel" ) -func Inject(app kernel.ApplicationInterface) { - container := app.GetContainer() - container.GetConfig() +func RegisterProvider(app kernel.ApplicationInterface) *Client { - component := &Client{ + return &Client{ kernel.NewBaseClient(&app, nil), } - components := app.GetComponents() - (*components)["base"] = component } From af17d0d4be6f0fd7e98dea12a8d5984c8b142cbe Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Thu, 10 Jun 2021 01:09:28 +0800 Subject: [PATCH 17/25] get token access --- go.mod | 1 + go.sum | 11 ++ main.go | 4 +- src/kernel/accessToken.go | 150 +++++++++++++------- src/kernel/baseClient.go | 54 ++++--- src/kernel/cacheToken.go | 26 ++++ src/kernel/contract/accessTokenInterface.go | 4 +- src/kernel/serviceContainer.go | 6 +- src/work/application.go | 27 ++-- 9 files changed, 203 insertions(+), 80 deletions(-) create mode 100644 src/kernel/cacheToken.go diff --git a/go.mod b/go.mod index 4bd5ccb0..25056312 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ replace github.com/ArtisanCloud/go-libs => ../go-libs require ( github.com/ArtisanCloud/go-libs v1.0.9 github.com/gin-gonic/gin v1.7.1 // indirect + github.com/go-redis/redis/v8 v8.10.0 // indirect github.com/mattn/go-isatty v0.0.13 // indirect golang.org/x/net v0.0.0-20210525063256-abc453219eb5 // indirect golang.org/x/sys v0.0.0-20210608053332-aa57babbf139 // indirect diff --git a/go.sum b/go.sum index 4ccb4f55..1e31e757 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,9 @@ +github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= @@ -19,6 +21,8 @@ github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GO github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE= github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= github.com/go-redis/redis/v8 v8.8.0/go.mod h1:F7resOH5Kdug49Otu24RjHWwgK7u9AmtqWMnCV1iP5Y= +github.com/go-redis/redis/v8 v8.10.0 h1:OZwrQKuZqdJ4QIM8wn8rnuz868Li91xA3J2DEq+TPGA= +github.com/go-redis/redis/v8 v8.10.0/go.mod h1:vXLTvigok0VtUX0znvbcEW1SOt4OA9CU1ZfnOtKOaiM= github.com/golang-module/carbon v1.3.5/go.mod h1:wIIzB2BwewCNV4KyXfAASbAIsCJpwceR9JwVrgqdLIw= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= @@ -73,9 +77,16 @@ 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/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= go.opentelemetry.io/otel v0.19.0/go.mod h1:j9bF567N9EfomkSidSfmMwIwIBuP37AMAIzVW85OxSg= +go.opentelemetry.io/otel v0.20.0 h1:eaP0Fqu7SXHwvjiqDq83zImeehOHX8doTvU9AwXON8g= +go.opentelemetry.io/otel v0.20.0/go.mod h1:Y3ugLH2oa81t5QO+Lty+zXf8zC9L26ax4Nzoxm/dooo= go.opentelemetry.io/otel/metric v0.19.0/go.mod h1:8f9fglJPRnXuskQmKpnad31lcLJ2VmNNqIsx/uIwBSc= +go.opentelemetry.io/otel/metric v0.20.0 h1:4kzhXFP+btKm4jwxpjIqjs41A7MakRFUS86bqLHTIw8= +go.opentelemetry.io/otel/metric v0.20.0/go.mod h1:598I5tYlH1vzBjn+BTuhzTCSb/9debfNp6R3s7Pr1eU= go.opentelemetry.io/otel/oteltest v0.19.0/go.mod h1:tI4yxwh8U21v7JD6R3BcA/2+RBoTKFexE/PJ/nSO7IA= +go.opentelemetry.io/otel/oteltest v0.20.0/go.mod h1:L7bgKf9ZB7qCwT9Up7i9/pn0PWIa9FqQ2IQ8LoxiGnw= go.opentelemetry.io/otel/trace v0.19.0/go.mod h1:4IXiNextNOpPnRlI4ryK69mn5iC84bjBWZQA5DXz/qg= +go.opentelemetry.io/otel/trace v0.20.0 h1:1DL6EXUdcg95gukhuRRvLDO/4X5THh/5dIV52lqtnbw= +go.opentelemetry.io/otel/trace v0.20.0/go.mod h1:6GjCW8zgDjwGHGa6GkyeB8+/5vjT16gUEi0Nf1iBdgw= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= diff --git a/main.go b/main.go index eeaa6b6c..23a59774 100644 --- a/main.go +++ b/main.go @@ -14,8 +14,8 @@ func main() { app := work.NewWork(config) //fmt2.Dump(app) - //components := app.GetComponents() - //token := (*app.Components)["access_token"].(*auth.AccessToken).GetToken() + + //token := app.AccessToken.GetToken() //fmt2.Dump(token) //fmt2.Dump(app.GetConfig()) diff --git a/src/kernel/accessToken.go b/src/kernel/accessToken.go index 0cc274dc..70e27e58 100644 --- a/src/kernel/accessToken.go +++ b/src/kernel/accessToken.go @@ -3,18 +3,29 @@ package kernel import ( "crypto/md5" "encoding/json" - "fmt" "github.com/ArtisanCloud/go-libs/exception" + fmt2 "github.com/ArtisanCloud/go-libs/fmt" + http2 "github.com/ArtisanCloud/go-libs/http" httpContract "github.com/ArtisanCloud/go-libs/http/contract" "github.com/ArtisanCloud/go-libs/object" "github.com/ArtisanCloud/go-wechat/src/kernel/contract" - "reflect" + "net/http" + "net/url" "time" ) +type ResponseGetToken struct { + AccessToken string `json:"access_token"` + ExpiresIn int `json:"expires_in"` + *ResponseWX +} + type AccessToken struct { App *ApplicationInterface + *http2.HttpRequest + *http2.HttpResponse + RequestMethod string EndpointToGetToken string QueryName string @@ -22,12 +33,18 @@ type AccessToken struct { TokenKey string CachePrefix string + *CacheToken + GetCredentials func() *object.StringMap } func NewAccessToken(app *ApplicationInterface) *AccessToken { + config := (*app).GetContainer().GetConfig() + token := &AccessToken{ - App: app, + App: app, + HttpRequest: http2.NewHttpRequest(config), + RequestMethod: "GET", EndpointToGetToken: "", QueryName: "", @@ -39,37 +56,57 @@ func NewAccessToken(app *ApplicationInterface) *AccessToken { return token } -func (component *AccessToken) GetRefreshedToken() *object.HashMap { - return component.GetToken() +func (accessToken *AccessToken) GetRefreshedToken() *object.HashMap { + return accessToken.GetToken(true) } -func (component *AccessToken) GetToken() (token *object.HashMap) { - //cacheKey := token.GetCacheKey() - //cache:= GetCache() +func (accessToken *AccessToken) GetToken(refresh bool) (token *object.HashMap) { + //cacheKey := accessToken.getCacheKey() + //cache := *accessToken.getCache() + // + //// get token from cache + //if !refresh && cache.Has(cacheKey) { + // token = &object.HashMap{} + // err := cache.Get(cacheKey, token) + // if err == nil { + // return token + // } + //} - response := component.requestToken(component.GetCredentials(), true) - return nil - typeResponse := reflect.TypeOf(response) - //fmt.Printf("response name:%s, type: %s \n",typeResponse.Name(), typeResponse.Kind() ) - if typeResponse.Name() == "ResponseInterface" { - token = response.(*object.HashMap) + // request token from wx + response := accessToken.requestToken(accessToken.GetCredentials(), true) + + //// save token into cache + token = response.(*object.HashMap) + strToken := (*token)[accessToken.TokenKey].(string) + var expireIn time.Duration = 7200 * time.Second + if (*token)[accessToken.TokenKey] == nil { + expireIn = (*token)[accessToken.TokenKey].(time.Duration) * time.Second } + fmt2.Dump(token, strToken, expireIn) + //token, _ = accessToken.SetToken(strToken, expireIn) + + //typeResponse := reflect.TypeOf(response) + ////fmt.Printf("response name:%s, type: %s \n",typeResponse.Name(), typeResponse.Kind() ) + //if typeResponse.Name() == "ResponseInterface" { + // token = response.(*object.HashMap) + //} return token } -func (component *AccessToken) SetToken(token string, lifeTime time.Duration) (*object.HashMap, error) { +func (accessToken *AccessToken) SetToken(token string, lifeTime time.Duration) (*object.HashMap, error) { if lifeTime <= 0 { lifeTime = 7200 * time.Second } - //cache := component.GetCache() - //cache.Set(component.GetCacheKey(), map[string]interface{}{ - // component.TokenKey: token, + //cache := accessToken.GetCache() + //cache.Set(accessToken.GetCacheKey(), map[string]interface{}{ + // accessToken.TokenKey: token, // "expires_in": lifeTime, //}) // - //if !cache.Has(component.GetCacheKey()) { + //if !cache.Has(accessToken.GetCacheKey()) { // err = errors.New("Failed to cache access token.") //} //return token, err @@ -77,64 +114,81 @@ func (component *AccessToken) SetToken(token string, lifeTime time.Duration) (*o } -func (component *AccessToken) Refresh() *contract.AccessTokenInterface { +func (accessToken *AccessToken) Refresh() *contract.AccessTokenInterface { return nil } -func (component *AccessToken) requestToken(credentials *object.StringMap, toArray bool) interface{} { - fmt.Printf("credentials:%v \n", credentials) +func (accessToken *AccessToken) requestToken(credentials *object.StringMap, toArray bool) interface{} { + + accessToken.sendRequest(credentials) return nil } -func (component *AccessToken) ApplyToRequest(request *httpContract.RequestInterface, requestOptions object.HashMap) httpContract.RequestInterface { +func (accessToken *AccessToken) ApplyToRequest(request *http.Request, requestOptions *object.HashMap) *http.Request { // parse query - //strURL := request.GetUri().GetQuery() - //strURL = parsestring(strURL) - // merge query with query again - //query = http + hashToken := accessToken.getQuery() + strMapToString := object.ConvertStringMapToString(hashToken) + uriToken, _ := url.Parse(strMapToString) + request.URL.ResolveReference(uriToken) return request } -func (component *AccessToken) sendRequest(credential *object.HashMap) *httpContract.ResponseContract { +func (accessToken *AccessToken) sendRequest(credential *object.StringMap) *httpContract.ResponseContract { + + key := "json" + if accessToken.RequestMethod == "GET" { + key = "query" + } + options := &object.HashMap{ + key: credential, + } + + accessToken.SetHttpClient(accessToken.GetHttpClient()).PerformRequest( + accessToken.getEndpoint(), + accessToken.RequestMethod, + options, + &ResponseGetToken{}, + ) return nil } -func (component *AccessToken) getCacheKey() string { - data, _ := json.Marshal(component.GetCredentials()) +func (accessToken *AccessToken) getCacheKey() string { + data, _ := json.Marshal(accessToken.GetCredentials()) buffer := md5.Sum(data) - return component.CachePrefix + string(buffer[:]) + return accessToken.CachePrefix + string(buffer[:]) } -func (component *AccessToken) getQuery() interface{} { - if component.QueryName != "" { - return []string{component.QueryName} +func (accessToken *AccessToken) getQuery() *object.StringMap { + var key string + if accessToken.QueryName != "" { + key = accessToken.QueryName } else { - arrayReturn := []object.HashMap{} - token := component.GetToken() - result := object.HashMap{ - component.TokenKey: (*token)[component.TokenKey], - } - arrayReturn = append(arrayReturn, result) - - return arrayReturn + key = accessToken.TokenKey + } + token := accessToken.GetToken(false) + arrayReturn := &object.StringMap{ + key: (*token)[accessToken.TokenKey].(string), } + + return arrayReturn + } -func (component *AccessToken) getEndpoint() string { - defer (&exception.Exception{}).HandleException(nil, "accessToken.get.endpoint", component) - if component.EndpointToGetToken == "" { +func (accessToken *AccessToken) getEndpoint() string { + defer (&exception.Exception{}).HandleException(nil, "accessToken.get.endpoint", accessToken) + if accessToken.EndpointToGetToken == "" { panic("No endpoint for access token request.") return "" } - return component.EndpointToGetToken + return accessToken.EndpointToGetToken } -func (component *AccessToken) getTokenKey() string { - return component.TokenKey +func (accessToken *AccessToken) getTokenKey() string { + return accessToken.TokenKey } diff --git a/src/kernel/baseClient.go b/src/kernel/baseClient.go index 679a2cb4..b2efc485 100644 --- a/src/kernel/baseClient.go +++ b/src/kernel/baseClient.go @@ -11,17 +11,21 @@ type BaseClient struct { *http.HttpRequest *http.HttpResponse - App *ApplicationInterface - - AccessToken *AccessToken + App *ApplicationInterface + Token *AccessToken } func NewBaseClient(app *ApplicationInterface, token *AccessToken) *BaseClient { config := (*app).GetContainer().GetConfig() + + if token == nil { + token = (*app).GetAccessToken() + } + client := &BaseClient{ HttpRequest: http.NewHttpRequest(config), App: app, - AccessToken: token, + Token: token, } return client @@ -87,44 +91,56 @@ func (client *BaseClient) registerHttpMiddlewares() { client.Middlewares = []interface{}{} // retry - client.PushMiddleware(client.retryMiddleware(), "retry") + //client.PushMiddleware(client.retryMiddleware(), "retry") // access token client.PushMiddleware(client.accessTokenMiddleware(), "access_token") // log - client.PushMiddleware(client.logMiddleware(), "log") + //client.PushMiddleware(client.logMiddleware(), "log") } // ---------------------------------------------------------------------- -type MiddlewareAccessToken struct{} -type MiddlewareLogMiddleware struct{} -type MiddlewareRetry struct{} +type MiddlewareAccessToken struct { + *BaseClient +} +type MiddlewareLogMiddleware struct { + *BaseClient +} +type MiddlewareRetry struct { + *BaseClient +} func (d *MiddlewareAccessToken) ModifyRequest(req *http2.Request) error { - fmt.Println("accessTokenMiddleware") + accessToken := (*d.App).GetAccessToken() + + if accessToken != nil { + config := (*d.App).GetContainer().GetConfig() + accessToken.ApplyToRequest(req, config) + } + return nil } - func (d *MiddlewareLogMiddleware) ModifyRequest(req *http2.Request) error { fmt.Println("logMiddleware") return nil } - - func (d *MiddlewareRetry) ModifyRequest(req *http2.Request) error { fmt.Println("retryMiddleware") return nil } func (client *BaseClient) accessTokenMiddleware() interface{} { - - return &MiddlewareAccessToken{} + return &MiddlewareAccessToken{ + client, + } } - func (client *BaseClient) logMiddleware() interface{} { - return &MiddlewareLogMiddleware{} + return &MiddlewareLogMiddleware{ + client, + } } - func (client *BaseClient) retryMiddleware() interface{} { - return &MiddlewareRetry{} + return &MiddlewareRetry{ + client, + } } diff --git a/src/kernel/cacheToken.go b/src/kernel/cacheToken.go new file mode 100644 index 00000000..b559d343 --- /dev/null +++ b/src/kernel/cacheToken.go @@ -0,0 +1,26 @@ +package kernel + +import "github.com/ArtisanCloud/go-libs/cache" + +type CacheToken struct { + Cache *cache.CacheInterface +} + +func (cacheToken *CacheToken) getCache() *cache.CacheInterface { + if cacheToken.Cache !=nil{ + return cacheToken.Cache + } + + cacheToken.Cache = cacheToken.createDefaultCache() + + return cacheToken.Cache +} + +func (cacheToken *CacheToken) setCache(cache *cache.CacheInterface){ + cacheToken.Cache = cache +} + +func (cacheToken *CacheToken) createDefaultCache() *cache.CacheInterface { + + return cacheToken.Cache +} diff --git a/src/kernel/contract/accessTokenInterface.go b/src/kernel/contract/accessTokenInterface.go index 9ba78122..91da748b 100644 --- a/src/kernel/contract/accessTokenInterface.go +++ b/src/kernel/contract/accessTokenInterface.go @@ -1,14 +1,14 @@ package contract import ( - "github.com/ArtisanCloud/go-libs/http/contract" "github.com/ArtisanCloud/go-libs/object" + "net/http" ) type ( AccessTokenInterface interface { GetToken() string Refresh() AccessTokenInterface - ApplyToRequest(request contract.RequestInterface, requestOptions object.HashMap) contract.RequestInterface + ApplyToRequest(request *http.Request, requestOptions *object.HashMap) *http.Request } ) \ No newline at end of file diff --git a/src/kernel/serviceContainer.go b/src/kernel/serviceContainer.go index 860e6e05..2f5b0227 100644 --- a/src/kernel/serviceContainer.go +++ b/src/kernel/serviceContainer.go @@ -1,9 +1,13 @@ package kernel -import "github.com/ArtisanCloud/go-libs/object" +import ( + "github.com/ArtisanCloud/go-libs/object" +) + type ApplicationInterface interface { GetContainer() *ServiceContainer + GetAccessToken() *AccessToken } type ServiceContainer struct { diff --git a/src/work/application.go b/src/work/application.go index 27d2f5ae..7368012c 100644 --- a/src/work/application.go +++ b/src/work/application.go @@ -15,24 +15,35 @@ type Work struct { } func NewWork(config *object.HashMap) *Work { - app := &Work{ - ServiceContainer: &kernel.ServiceContainer{ - UserConfig: config, - DefaultConfig: &object.HashMap{ - "http": map[string]string{ - "base_uri": "https://qyapi.weixin.qq.com/", - }, + // init an app container + container:=&kernel.ServiceContainer{ + UserConfig: config, + DefaultConfig: &object.HashMap{ + "http": map[string]string{ + "base_uri": "https://qyapi.weixin.qq.com/", }, }, } - // inject components + // init app + app := &Work{ + ServiceContainer: container, + } + + // register Auth app.AccessToken = auth.RegisterProvider(app) + // register Base app.Base = base.RegisterProvider(app) + + return app } func (app *Work) GetContainer() *kernel.ServiceContainer { return app.ServiceContainer } + +func (app *Work) GetAccessToken() *kernel.AccessToken{ + return app.AccessToken.AccessToken +} \ No newline at end of file From 98d88451147649793de085a47aedab99865900d8 Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Thu, 10 Jun 2021 17:33:14 +0800 Subject: [PATCH 18/25] add cache driver in memory --- go.sum | 2 + src/kernel/ResponseWX.go | 6 -- src/kernel/accessToken.go | 120 ++++++++++++++----------- src/kernel/baseClient.go | 2 +- src/kernel/cacheToken.go | 22 +++-- src/kernel/response/getToken.go | 7 ++ src/kernel/response/wx.go | 19 ++++ src/work/base/client.go | 5 -- src/work/base/responseGetCallbackIp.go | 11 +++ 9 files changed, 122 insertions(+), 72 deletions(-) delete mode 100644 src/kernel/ResponseWX.go create mode 100644 src/kernel/response/getToken.go create mode 100644 src/kernel/response/wx.go create mode 100644 src/work/base/responseGetCallbackIp.go diff --git a/go.sum b/go.sum index 1e31e757..26276164 100644 --- a/go.sum +++ b/go.sum @@ -62,6 +62,8 @@ github.com/onsi/ginkgo v1.15.0/go.mod h1:hF8qUzuuC8DJGygJH3726JnCZX4MYbRB8yFfISq github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7mt48= +github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= +github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/src/kernel/ResponseWX.go b/src/kernel/ResponseWX.go deleted file mode 100644 index 36fc0916..00000000 --- a/src/kernel/ResponseWX.go +++ /dev/null @@ -1,6 +0,0 @@ -package kernel - -type ResponseWX struct { - ErrCode int `json:"errcode"` - ErrMSG string `json:"errmsg"` -} diff --git a/src/kernel/accessToken.go b/src/kernel/accessToken.go index 70e27e58..93b309fe 100644 --- a/src/kernel/accessToken.go +++ b/src/kernel/accessToken.go @@ -3,23 +3,17 @@ package kernel import ( "crypto/md5" "encoding/json" + "fmt" "github.com/ArtisanCloud/go-libs/exception" - fmt2 "github.com/ArtisanCloud/go-libs/fmt" http2 "github.com/ArtisanCloud/go-libs/http" httpContract "github.com/ArtisanCloud/go-libs/http/contract" "github.com/ArtisanCloud/go-libs/object" "github.com/ArtisanCloud/go-wechat/src/kernel/contract" + response2 "github.com/ArtisanCloud/go-wechat/src/kernel/response" "net/http" - "net/url" "time" ) -type ResponseGetToken struct { - AccessToken string `json:"access_token"` - ExpiresIn int `json:"expires_in"` - *ResponseWX -} - type AccessToken struct { App *ApplicationInterface @@ -56,11 +50,11 @@ func NewAccessToken(app *ApplicationInterface) *AccessToken { return token } -func (accessToken *AccessToken) GetRefreshedToken() *object.HashMap { +func (accessToken *AccessToken) GetRefreshedToken() *response2.ResponseGetToken { return accessToken.GetToken(true) } -func (accessToken *AccessToken) GetToken(refresh bool) (token *object.HashMap) { +func (accessToken *AccessToken) GetToken(refresh bool) (resToken *response2.ResponseGetToken) { //cacheKey := accessToken.getCacheKey() //cache := *accessToken.getCache() // @@ -74,70 +68,84 @@ func (accessToken *AccessToken) GetToken(refresh bool) (token *object.HashMap) { //} // request token from wx - response := accessToken.requestToken(accessToken.GetCredentials(), true) + response := accessToken.requestToken(accessToken.GetCredentials()) //// save token into cache - token = response.(*object.HashMap) - strToken := (*token)[accessToken.TokenKey].(string) + resToken = response.(*response2.ResponseGetToken) var expireIn time.Duration = 7200 * time.Second - if (*token)[accessToken.TokenKey] == nil { - expireIn = (*token)[accessToken.TokenKey].(time.Duration) * time.Second + if resToken.ExpiresIn > 0 { + expireIn = time.Duration(resToken.ExpiresIn) * time.Second } - fmt2.Dump(token, strToken, expireIn) - //token, _ = accessToken.SetToken(strToken, expireIn) + accessToken.SetToken(resToken.AccessToken, expireIn) - //typeResponse := reflect.TypeOf(response) - ////fmt.Printf("response name:%s, type: %s \n",typeResponse.Name(), typeResponse.Kind() ) - //if typeResponse.Name() == "ResponseInterface" { - // token = response.(*object.HashMap) - //} + // tbd dispatch a event for AccessTokenRefresh - return token + return resToken } -func (accessToken *AccessToken) SetToken(token string, lifeTime time.Duration) (*object.HashMap, error) { +func (accessToken *AccessToken) SetToken(token string, lifeTime time.Duration) (tokenInterface contract.AccessTokenInterface, err error) { if lifeTime <= 0 { lifeTime = 7200 * time.Second } - //cache := accessToken.GetCache() - //cache.Set(accessToken.GetCacheKey(), map[string]interface{}{ - // accessToken.TokenKey: token, - // "expires_in": lifeTime, - //}) - // - //if !cache.Has(accessToken.GetCacheKey()) { - // err = errors.New("Failed to cache access token.") - //} - //return token, err - return nil, nil + // tbd + cache := accessToken.getCache() + err = cache.Set(accessToken.getCacheKey(), map[string]interface{}{ + accessToken.TokenKey: token, + "expires_in": lifeTime, + }, lifeTime) + + defer (&exception.Exception{}).HandleException(nil, "accessToken.set.token", nil) + if !cache.Has(accessToken.getCacheKey()) { + panic("Failed to cache access token.") + return nil, err + } + return accessToken, err } -func (accessToken *AccessToken) Refresh() *contract.AccessTokenInterface { +func (accessToken *AccessToken) Refresh() contract.AccessTokenInterface { return nil } -func (accessToken *AccessToken) requestToken(credentials *object.StringMap, toArray bool) interface{} { +func (accessToken *AccessToken) requestToken(credentials *object.StringMap) httpContract.ResponseContract { + + // tbf + return &response2.ResponseGetToken{ + AccessToken: "Np9hU7IcFX8fajwPvhI4-_ZIJh0kDIBambDyI8nYuNMNsO74lUAgkA75iETLu2mAv05EkAwZf9caFupjQuVATBo17lk7xqNnqw_e5NpfdjEK9F5moG755THiHOd_XSGFb6a9QTggn_t6ejA7CcxlKetr6ntcxdRomQC9rnJ-razLKrWrJilWERDHVtwxg4TGh86qFvOjRZ7OMBznnmrbbQ", + ExpiresIn: 7200, + ResponseWX: &response2.ResponseWX{ + ErrCode: 0, + ErrMSG: "ok", + }, + } - accessToken.sendRequest(credentials) + res := accessToken.sendRequest(credentials) + token := res.(*response2.ResponseGetToken) + defer (&exception.Exception{}).HandleException(nil, "accessToken.request.token", nil) + if token == nil || token.AccessToken == "" { + panic(fmt.Sprintf("Request access_token fail: %v", res)) + return nil + } - return nil + return token } func (accessToken *AccessToken) ApplyToRequest(request *http.Request, requestOptions *object.HashMap) *http.Request { - // parse query - // merge query with query again - hashToken := accessToken.getQuery() - strMapToString := object.ConvertStringMapToString(hashToken) - uriToken, _ := url.Parse(strMapToString) - request.URL.ResolveReference(uriToken) + // query Access Token map + mapToken := accessToken.getQuery() + q := request.URL.Query() + for key, value := range *mapToken { + q.Set(key, value) + } + request.URL.RawQuery = q.Encode() + return request } -func (accessToken *AccessToken) sendRequest(credential *object.StringMap) *httpContract.ResponseContract { +func (accessToken *AccessToken) sendRequest(credential *object.StringMap) httpContract.ResponseContract { key := "json" if accessToken.RequestMethod == "GET" { @@ -147,13 +155,15 @@ func (accessToken *AccessToken) sendRequest(credential *object.StringMap) *httpC key: credential, } + res := &response2.ResponseGetToken{} + accessToken.SetHttpClient(accessToken.GetHttpClient()).PerformRequest( - accessToken.getEndpoint(), - accessToken.RequestMethod, - options, - &ResponseGetToken{}, - ) - return nil + accessToken.getEndpoint(), + accessToken.RequestMethod, + options, + res, + ) + return res } func (accessToken *AccessToken) getCacheKey() string { @@ -163,6 +173,7 @@ func (accessToken *AccessToken) getCacheKey() string { } func (accessToken *AccessToken) getQuery() *object.StringMap { + // set the current token key var key string if accessToken.QueryName != "" { key = accessToken.QueryName @@ -170,9 +181,10 @@ func (accessToken *AccessToken) getQuery() *object.StringMap { key = accessToken.TokenKey } - token := accessToken.GetToken(false) + // get token string map + resToken := accessToken.GetToken(false) arrayReturn := &object.StringMap{ - key: (*token)[accessToken.TokenKey].(string), + key: resToken.AccessToken, } return arrayReturn diff --git a/src/kernel/baseClient.go b/src/kernel/baseClient.go index b2efc485..73e4bfc3 100644 --- a/src/kernel/baseClient.go +++ b/src/kernel/baseClient.go @@ -74,7 +74,7 @@ func (client *BaseClient) Request(url string, method string, options *object.Has if client.Middlewares == nil { client.registerHttpMiddlewares() } - // + // http client request response := client.PerformRequest(url, method, options, outResponse) if returnRaw { diff --git a/src/kernel/cacheToken.go b/src/kernel/cacheToken.go index b559d343..0d926c40 100644 --- a/src/kernel/cacheToken.go +++ b/src/kernel/cacheToken.go @@ -1,26 +1,36 @@ package kernel -import "github.com/ArtisanCloud/go-libs/cache" +import ( + "github.com/ArtisanCloud/go-libs/cache" + "time" +) type CacheToken struct { - Cache *cache.CacheInterface + Cache cache.CacheInterface } -func (cacheToken *CacheToken) getCache() *cache.CacheInterface { - if cacheToken.Cache !=nil{ +func (cacheToken *CacheToken) getCache() cache.CacheInterface { + if cacheToken.Cache != nil { return cacheToken.Cache } + // tbd global redis exist + + // create default cache cacheToken.Cache = cacheToken.createDefaultCache() return cacheToken.Cache } -func (cacheToken *CacheToken) setCache(cache *cache.CacheInterface){ +func (cacheToken *CacheToken) setCache(cache cache.CacheInterface) { + // tbd + cacheToken.Cache = cache } -func (cacheToken *CacheToken) createDefaultCache() *cache.CacheInterface { +func (cacheToken *CacheToken) createDefaultCache() cache.CacheInterface { + // tbd + cacheToken.Cache = cache.NewFileCache("ac.go.wx", time.Duration(1500), "") return cacheToken.Cache } diff --git a/src/kernel/response/getToken.go b/src/kernel/response/getToken.go new file mode 100644 index 00000000..ddd555ed --- /dev/null +++ b/src/kernel/response/getToken.go @@ -0,0 +1,7 @@ +package response + +type ResponseGetToken struct { + AccessToken string `json:"access_token"` + ExpiresIn int `json:"expires_in"` + *ResponseWX +} \ No newline at end of file diff --git a/src/kernel/response/wx.go b/src/kernel/response/wx.go new file mode 100644 index 00000000..3b98ab09 --- /dev/null +++ b/src/kernel/response/wx.go @@ -0,0 +1,19 @@ +package response + +import "github.com/ArtisanCloud/go-libs/object" + +type ResponseWX struct { + ErrCode int `json:"errcode"` + ErrMSG string `json:"errmsg"` +} + +func (res *ResponseWX)GetBody() *object.HashMap{ + return nil +} +func (res *ResponseWX)GetHeaders() *object.HashMap{ + return nil +} + +func (res *ResponseWX)GetStatusCode() int{ + return 200 +} \ No newline at end of file diff --git a/src/work/base/client.go b/src/work/base/client.go index 47ef20f0..e4fd4c7b 100644 --- a/src/work/base/client.go +++ b/src/work/base/client.go @@ -8,11 +8,6 @@ type Client struct { *kernel.BaseClient } -type ResponseGetCallBackIp struct { - IPList []string `json:"ip_list"` - *kernel.ResponseWX -} - func (comp *Client) GetCallbackIp() *ResponseGetCallBackIp { result := &ResponseGetCallBackIp{} diff --git a/src/work/base/responseGetCallbackIp.go b/src/work/base/responseGetCallbackIp.go new file mode 100644 index 00000000..e99682db --- /dev/null +++ b/src/work/base/responseGetCallbackIp.go @@ -0,0 +1,11 @@ +package base + +import ( + "github.com/ArtisanCloud/go-wechat/src/kernel/response" +) + +type ResponseGetCallBackIp struct { + IPList []string `json:"ip_list"` + *response.ResponseWX +} + From 6840319edba74d4ce4869cebbe42151f7144161c Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Thu, 10 Jun 2021 23:00:02 +0800 Subject: [PATCH 19/25] finish mem cache --- main.go | 1 + src/kernel/accessToken.go | 58 ++++++++++++--------- src/kernel/cacheToken.go | 9 ++-- src/kernel/contract/accessTokenInterface.go | 5 +- src/kernel/response/getToken.go | 2 +- 5 files changed, 42 insertions(+), 33 deletions(-) diff --git a/main.go b/main.go index 23a59774..a712e990 100644 --- a/main.go +++ b/main.go @@ -24,6 +24,7 @@ func main() { //fmt.Printf("type %v \n", cType) ips := app.Base.GetCallbackIp() + ips = app.Base.GetCallbackIp() fmt2.Dump(ips) } diff --git a/src/kernel/accessToken.go b/src/kernel/accessToken.go index 93b309fe..29722b53 100644 --- a/src/kernel/accessToken.go +++ b/src/kernel/accessToken.go @@ -45,6 +45,8 @@ func NewAccessToken(app *ApplicationInterface) *AccessToken { Token: nil, TokenKey: "access_token", CachePrefix: "ac.go.wechat.kernel.access_token.", + + CacheToken: &CacheToken{}, } return token @@ -55,26 +57,29 @@ func (accessToken *AccessToken) GetRefreshedToken() *response2.ResponseGetToken } func (accessToken *AccessToken) GetToken(refresh bool) (resToken *response2.ResponseGetToken) { - //cacheKey := accessToken.getCacheKey() - //cache := *accessToken.getCache() - // - //// get token from cache - //if !refresh && cache.Has(cacheKey) { - // token = &object.HashMap{} - // err := cache.Get(cacheKey, token) - // if err == nil { - // return token - // } - //} + cacheKey := accessToken.getCacheKey() + cache := accessToken.getCache() + + // get token from cache + if !refresh && cache.Has(cacheKey) { + value, err := cache.Get(cacheKey, nil) + if err == nil { + token := value.(*object.HashMap) + return &response2.ResponseGetToken{ + AccessToken: (*token)[accessToken.TokenKey].(string), + ExpiresIn: (*token)["expires_in"].(int), + } + } + } // request token from wx response := accessToken.requestToken(accessToken.GetCredentials()) //// save token into cache resToken = response.(*response2.ResponseGetToken) - var expireIn time.Duration = 7200 * time.Second + var expireIn int = 7200 if resToken.ExpiresIn > 0 { - expireIn = time.Duration(resToken.ExpiresIn) * time.Second + expireIn = resToken.ExpiresIn } accessToken.SetToken(resToken.AccessToken, expireIn) @@ -83,17 +88,17 @@ func (accessToken *AccessToken) GetToken(refresh bool) (resToken *response2.Resp return resToken } -func (accessToken *AccessToken) SetToken(token string, lifeTime time.Duration) (tokenInterface contract.AccessTokenInterface, err error) { +func (accessToken *AccessToken) SetToken(token string, lifeTime int) (tokenInterface contract.AccessTokenInterface, err error) { if lifeTime <= 0 { - lifeTime = 7200 * time.Second + lifeTime = 7200 } - // tbd + // set token into cache cache := accessToken.getCache() - err = cache.Set(accessToken.getCacheKey(), map[string]interface{}{ + err = cache.Set(accessToken.getCacheKey(), &object.HashMap{ accessToken.TokenKey: token, "expires_in": lifeTime, - }, lifeTime) + }, time.Duration(lifeTime)*time.Second) defer (&exception.Exception{}).HandleException(nil, "accessToken.set.token", nil) if !cache.Has(accessToken.getCacheKey()) { @@ -112,14 +117,14 @@ func (accessToken *AccessToken) Refresh() contract.AccessTokenInterface { func (accessToken *AccessToken) requestToken(credentials *object.StringMap) httpContract.ResponseContract { // tbf - return &response2.ResponseGetToken{ - AccessToken: "Np9hU7IcFX8fajwPvhI4-_ZIJh0kDIBambDyI8nYuNMNsO74lUAgkA75iETLu2mAv05EkAwZf9caFupjQuVATBo17lk7xqNnqw_e5NpfdjEK9F5moG755THiHOd_XSGFb6a9QTggn_t6ejA7CcxlKetr6ntcxdRomQC9rnJ-razLKrWrJilWERDHVtwxg4TGh86qFvOjRZ7OMBznnmrbbQ", - ExpiresIn: 7200, - ResponseWX: &response2.ResponseWX{ - ErrCode: 0, - ErrMSG: "ok", - }, - } + //return &response2.ResponseGetToken{ + // AccessToken: "NTZc03xEsnxqpavAgJlaVqjQmq9UG5_shDGmaEkoB0AcqncvkD7Y_fprh_-WRtp206tSqGRa3Rj7hZ7wRNwk5Y-AUqJ1Y0yAOTqilAhDWFm3EBbM5_Cr9cFueq1Y7O-TG0kgaYor1_h3Ap7OleAYG2Rqray3P2XWUHrU2DprwG4t7BK10UB1y4j1dJ6Z-BKFZCWhvFaZMCLbjjAmO0UUPw", + // ExpiresIn: 7200, + // ResponseWX: &response2.ResponseWX{ + // ErrCode: 0, + // ErrMSG: "ok", + // }, + //} res := accessToken.sendRequest(credentials) token := res.(*response2.ResponseGetToken) @@ -169,6 +174,7 @@ func (accessToken *AccessToken) sendRequest(credential *object.StringMap) httpCo func (accessToken *AccessToken) getCacheKey() string { data, _ := json.Marshal(accessToken.GetCredentials()) buffer := md5.Sum(data) + return accessToken.CachePrefix + string(buffer[:]) } diff --git a/src/kernel/cacheToken.go b/src/kernel/cacheToken.go index 0d926c40..7953a8ca 100644 --- a/src/kernel/cacheToken.go +++ b/src/kernel/cacheToken.go @@ -22,15 +22,16 @@ func (cacheToken *CacheToken) getCache() cache.CacheInterface { return cacheToken.Cache } -func (cacheToken *CacheToken) setCache(cache cache.CacheInterface) { - // tbd +func (cacheToken *CacheToken) setCache(cache cache.CacheInterface) *CacheToken { cacheToken.Cache = cache + + return cacheToken } func (cacheToken *CacheToken) createDefaultCache() cache.CacheInterface { - // tbd - cacheToken.Cache = cache.NewFileCache("ac.go.wx", time.Duration(1500), "") + + cacheToken.Cache = cache.NewMemCache("ac.go.wx", time.Duration(1500), "") return cacheToken.Cache } diff --git a/src/kernel/contract/accessTokenInterface.go b/src/kernel/contract/accessTokenInterface.go index 91da748b..e294702b 100644 --- a/src/kernel/contract/accessTokenInterface.go +++ b/src/kernel/contract/accessTokenInterface.go @@ -2,13 +2,14 @@ package contract import ( "github.com/ArtisanCloud/go-libs/object" + response2 "github.com/ArtisanCloud/go-wechat/src/kernel/response" "net/http" ) type ( AccessTokenInterface interface { - GetToken() string + GetToken(refresh bool) (resToken *response2.ResponseGetToken) Refresh() AccessTokenInterface ApplyToRequest(request *http.Request, requestOptions *object.HashMap) *http.Request } -) \ No newline at end of file +) diff --git a/src/kernel/response/getToken.go b/src/kernel/response/getToken.go index ddd555ed..f331b1a2 100644 --- a/src/kernel/response/getToken.go +++ b/src/kernel/response/getToken.go @@ -4,4 +4,4 @@ type ResponseGetToken struct { AccessToken string `json:"access_token"` ExpiresIn int `json:"expires_in"` *ResponseWX -} \ No newline at end of file +} From 9f0fbeb055dbaee46de0ed86a59e6beeda0be697 Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Fri, 11 Jun 2021 13:45:29 +0800 Subject: [PATCH 20/25] add GetAPIDomainIP --- src/kernel/accessToken.go | 16 ++++++++-------- src/work/base/client.go | 11 +++++++++++ src/work/base/responseGetAPIDomainIP.go | 11 +++++++++++ 3 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 src/work/base/responseGetAPIDomainIP.go diff --git a/src/kernel/accessToken.go b/src/kernel/accessToken.go index 29722b53..b6cc811d 100644 --- a/src/kernel/accessToken.go +++ b/src/kernel/accessToken.go @@ -117,14 +117,14 @@ func (accessToken *AccessToken) Refresh() contract.AccessTokenInterface { func (accessToken *AccessToken) requestToken(credentials *object.StringMap) httpContract.ResponseContract { // tbf - //return &response2.ResponseGetToken{ - // AccessToken: "NTZc03xEsnxqpavAgJlaVqjQmq9UG5_shDGmaEkoB0AcqncvkD7Y_fprh_-WRtp206tSqGRa3Rj7hZ7wRNwk5Y-AUqJ1Y0yAOTqilAhDWFm3EBbM5_Cr9cFueq1Y7O-TG0kgaYor1_h3Ap7OleAYG2Rqray3P2XWUHrU2DprwG4t7BK10UB1y4j1dJ6Z-BKFZCWhvFaZMCLbjjAmO0UUPw", - // ExpiresIn: 7200, - // ResponseWX: &response2.ResponseWX{ - // ErrCode: 0, - // ErrMSG: "ok", - // }, - //} + return &response2.ResponseGetToken{ + AccessToken: "NTZc03xEsnxqpavAgJlaVqjQmq9UG5_shDGmaEkoB0AcqncvkD7Y_fprh_-WRtp206tSqGRa3Rj7hZ7wRNwk5Y-AUqJ1Y0yAOTqilAhDWFm3EBbM5_Cr9cFueq1Y7O-TG0kgaYor1_h3Ap7OleAYG2Rqray3P2XWUHrU2DprwG4t7BK10UB1y4j1dJ6Z-BKFZCWhvFaZMCLbjjAmO0UUPw", + ExpiresIn: 7200, + ResponseWX: &response2.ResponseWX{ + ErrCode: 0, + ErrMSG: "ok", + }, + } res := accessToken.sendRequest(credentials) token := res.(*response2.ResponseGetToken) diff --git a/src/work/base/client.go b/src/work/base/client.go index e4fd4c7b..0a7dcabd 100644 --- a/src/work/base/client.go +++ b/src/work/base/client.go @@ -16,3 +16,14 @@ func (comp *Client) GetCallbackIp() *ResponseGetCallBackIp { return result } + + +func (comp *Client) GetAPIDomainIP() *ResponseGetAPIDomainIP { + + result := &ResponseGetAPIDomainIP{} + + comp.HttpGet("cgi-bin/get_api_domain_ip", nil, result) + + return result +} + diff --git a/src/work/base/responseGetAPIDomainIP.go b/src/work/base/responseGetAPIDomainIP.go new file mode 100644 index 00000000..93c83901 --- /dev/null +++ b/src/work/base/responseGetAPIDomainIP.go @@ -0,0 +1,11 @@ +package base + +import ( + "github.com/ArtisanCloud/go-wechat/src/kernel/response" +) + +type ResponseGetAPIDomainIP struct { + IPList []string `json:"ip_list"` + *response.ResponseWX +} + From 7b01dc48725104a5cb2924cd618ad449b2157900 Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Fri, 11 Jun 2021 15:18:58 +0800 Subject: [PATCH 21/25] add oauth --- main.go | 11 +++++++---- src/kernel/accessToken.go | 2 +- src/kernel/provider.go | 13 +++++++++++++ src/work/application.go | 2 ++ src/work/oauth/manager.go | 3 +++ src/work/oauth/provider.go | 3 +++ 6 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 src/kernel/provider.go create mode 100644 src/work/oauth/manager.go create mode 100644 src/work/oauth/provider.go diff --git a/main.go b/main.go index a712e990..523c4475 100644 --- a/main.go +++ b/main.go @@ -14,17 +14,20 @@ func main() { app := work.NewWork(config) //fmt2.Dump(app) + fmt2.Dump(app.GetConfig()) //token := app.AccessToken.GetToken() //fmt2.Dump(token) - //fmt2.Dump(app.GetConfig()) //cType := reflect.TypeOf((*app.Components)["base"].(*base.Client)) //fmt.Printf("kind %s \n", cType.Kind()) //fmt.Printf("type %v \n", cType) - ips := app.Base.GetCallbackIp() - ips = app.Base.GetCallbackIp() - fmt2.Dump(ips) + //ips := app.Base.GetCallbackIp() + //fmt2.Dump(ips) + //domainIps := app.Base.GetAPIDomainIP() + //fmt2.Dump(domainIps) + + } diff --git a/src/kernel/accessToken.go b/src/kernel/accessToken.go index b6cc811d..aa4ae8e8 100644 --- a/src/kernel/accessToken.go +++ b/src/kernel/accessToken.go @@ -118,7 +118,7 @@ func (accessToken *AccessToken) requestToken(credentials *object.StringMap) http // tbf return &response2.ResponseGetToken{ - AccessToken: "NTZc03xEsnxqpavAgJlaVqjQmq9UG5_shDGmaEkoB0AcqncvkD7Y_fprh_-WRtp206tSqGRa3Rj7hZ7wRNwk5Y-AUqJ1Y0yAOTqilAhDWFm3EBbM5_Cr9cFueq1Y7O-TG0kgaYor1_h3Ap7OleAYG2Rqray3P2XWUHrU2DprwG4t7BK10UB1y4j1dJ6Z-BKFZCWhvFaZMCLbjjAmO0UUPw", + AccessToken: "FwhFYGuWs47wxxUQHf5gbOIwMBe8ohu3EUnhF-x6uw4VX55-m46bnQUHLJ1nM5R0WAVfrLHvdn6XJtkMkzRkN_E75edrNzWK6IZxx3RwUJNowZEg3dcU3NEygFGfzUEGLS5fiB4GtNPOi-HKUhQumAMcP0lncEij2Hjg95FTqS4YBRNVG4OL92h8RAiP-yllfDvt2URk6tntGe4HaxAGdA", ExpiresIn: 7200, ResponseWX: &response2.ResponseWX{ ErrCode: 0, diff --git a/src/kernel/provider.go b/src/kernel/provider.go new file mode 100644 index 00000000..0cb5b7de --- /dev/null +++ b/src/kernel/provider.go @@ -0,0 +1,13 @@ +package kernel + +func RegisterProvider(app ApplicationInterface) { + container := app.GetContainer() + + // first merge configs + container.GetConfig() + + // register global http client + + // register log service + +} diff --git a/src/work/application.go b/src/work/application.go index 7368012c..8cccce31 100644 --- a/src/work/application.go +++ b/src/work/application.go @@ -30,6 +30,8 @@ func NewWork(config *object.HashMap) *Work { ServiceContainer: container, } + + // register Auth app.AccessToken = auth.RegisterProvider(app) // register Base diff --git a/src/work/oauth/manager.go b/src/work/oauth/manager.go new file mode 100644 index 00000000..d9b0ca5b --- /dev/null +++ b/src/work/oauth/manager.go @@ -0,0 +1,3 @@ +package oauth + + diff --git a/src/work/oauth/provider.go b/src/work/oauth/provider.go new file mode 100644 index 00000000..d9b0ca5b --- /dev/null +++ b/src/work/oauth/provider.go @@ -0,0 +1,3 @@ +package oauth + + From a47b2407506e3c71a222aff77de2107b47b55e69 Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Fri, 11 Jun 2021 18:10:07 +0800 Subject: [PATCH 22/25] init oauth manager --- go.mod | 5 +- src/kernel/baseClient.go | 4 +- src/kernel/exception/exception.go | 8 +++ src/kernel/provider.go | 18 ++++--- src/kernel/serviceContainer.go | 17 ++++--- src/work/application.go | 13 ++--- src/work/base/client.go | 9 ++-- .../{ => response}/responseGetAPIDomainIP.go | 2 +- .../{ => response}/responseGetCallbackIp.go | 2 +- src/work/oauth/manager.go | 21 ++++++++ src/work/oauth/provider.go | 51 +++++++++++++++++++ 11 files changed, 119 insertions(+), 31 deletions(-) create mode 100644 src/kernel/exception/exception.go rename src/work/base/{ => response}/responseGetAPIDomainIP.go (90%) rename src/work/base/{ => response}/responseGetCallbackIp.go (90%) diff --git a/go.mod b/go.mod index 25056312..6498d0ec 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,11 @@ go 1.16 replace github.com/ArtisanCloud/go-libs => ../go-libs +replace github.com/ArtisanCloud/go-socialite => ../go-socialite + require ( - github.com/ArtisanCloud/go-libs v1.0.9 + github.com/ArtisanCloud/go-libs v1.0.10 + github.com/ArtisanCloud/go-socialite v0.0.0-00010101000000-000000000000 // indirect github.com/gin-gonic/gin v1.7.1 // indirect github.com/go-redis/redis/v8 v8.10.0 // indirect github.com/mattn/go-isatty v0.0.13 // indirect diff --git a/src/kernel/baseClient.go b/src/kernel/baseClient.go index 73e4bfc3..87f8f411 100644 --- a/src/kernel/baseClient.go +++ b/src/kernel/baseClient.go @@ -80,7 +80,7 @@ func (client *BaseClient) Request(url string, method string, options *object.Has if returnRaw { return response } else { - config := *(*client.App).GetContainer().GetConfig() + config := *(*client.App).GetContainer().Config client.CastResponseToType(response, config["response_type"]) } return response @@ -114,7 +114,7 @@ func (d *MiddlewareAccessToken) ModifyRequest(req *http2.Request) error { accessToken := (*d.App).GetAccessToken() if accessToken != nil { - config := (*d.App).GetContainer().GetConfig() + config := (*d.App).GetContainer().Config accessToken.ApplyToRequest(req, config) } diff --git a/src/kernel/exception/exception.go b/src/kernel/exception/exception.go new file mode 100644 index 00000000..c5710647 --- /dev/null +++ b/src/kernel/exception/exception.go @@ -0,0 +1,8 @@ +package exception + +import "github.com/ArtisanCloud/go-libs/exception" + +type Exception struct { + exception.Exception +} + diff --git a/src/kernel/provider.go b/src/kernel/provider.go index 0cb5b7de..59bb9d9c 100644 --- a/src/kernel/provider.go +++ b/src/kernel/provider.go @@ -1,13 +1,15 @@ package kernel func RegisterProvider(app ApplicationInterface) { - container := app.GetContainer() - - // first merge configs - container.GetConfig() - - // register global http client - - // register log service + // tbd + + //container := app.GetContainer() + // + //// first merge configs + //container.GetConfig() + // + //// register global http client + // + //// register log service } diff --git a/src/kernel/serviceContainer.go b/src/kernel/serviceContainer.go index 2f5b0227..d9b1c78c 100644 --- a/src/kernel/serviceContainer.go +++ b/src/kernel/serviceContainer.go @@ -4,7 +4,6 @@ import ( "github.com/ArtisanCloud/go-libs/object" ) - type ApplicationInterface interface { GetContainer() *ServiceContainer GetAccessToken() *AccessToken @@ -18,8 +17,8 @@ type ServiceContainer struct { Config *object.HashMap } -func (container *ServiceContainer) getBaseConfig() object.HashMap { - return object.HashMap{ +func (container *ServiceContainer) getBaseConfig() *object.HashMap { + return &object.HashMap{ // http://docs.guzzlephp.org/en/stable/request-options.html "http": object.HashMap{ "timeout": 30.0, @@ -29,15 +28,17 @@ func (container *ServiceContainer) getBaseConfig() object.HashMap { } func (container *ServiceContainer) GetConfig() *object.HashMap { - // if container config has been setup + + // init container config + var config *object.HashMap if container.Config != nil { - return container.Config + config = container.Config + } else { + config = container.getBaseConfig() } - container.Config = &object.HashMap{} - // merge config - container.Config = object.MergeHashMap(container.Config, container.DefaultConfig, container.UserConfig) + container.Config = object.MergeHashMap(config, container.DefaultConfig, container.UserConfig) return container.Config } diff --git a/src/work/application.go b/src/work/application.go index 8cccce31..105ea680 100644 --- a/src/work/application.go +++ b/src/work/application.go @@ -5,6 +5,7 @@ import ( "github.com/ArtisanCloud/go-wechat/src/kernel" "github.com/ArtisanCloud/go-wechat/src/work/auth" "github.com/ArtisanCloud/go-wechat/src/work/base" + "github.com/ArtisanCloud/go-wechat/src/work/oauth" ) type Work struct { @@ -12,11 +13,12 @@ type Work struct { Base *base.Client AccessToken *auth.AccessToken + OAuth *oauth.Manager } func NewWork(config *object.HashMap) *Work { // init an app container - container:=&kernel.ServiceContainer{ + container := &kernel.ServiceContainer{ UserConfig: config, DefaultConfig: &object.HashMap{ "http": map[string]string{ @@ -30,14 +32,13 @@ func NewWork(config *object.HashMap) *Work { ServiceContainer: container, } - - // register Auth app.AccessToken = auth.RegisterProvider(app) // register Base app.Base = base.RegisterProvider(app) - + // register oauth + app.OAuth = oauth.RegisterProvider(app) return app } @@ -46,6 +47,6 @@ func (app *Work) GetContainer() *kernel.ServiceContainer { return app.ServiceContainer } -func (app *Work) GetAccessToken() *kernel.AccessToken{ +func (app *Work) GetAccessToken() *kernel.AccessToken { return app.AccessToken.AccessToken -} \ No newline at end of file +} diff --git a/src/work/base/client.go b/src/work/base/client.go index 0a7dcabd..4799ceca 100644 --- a/src/work/base/client.go +++ b/src/work/base/client.go @@ -2,15 +2,16 @@ package base import ( "github.com/ArtisanCloud/go-wechat/src/kernel" + "github.com/ArtisanCloud/go-wechat/src/work/base/response" ) type Client struct { *kernel.BaseClient } -func (comp *Client) GetCallbackIp() *ResponseGetCallBackIp { +func (comp *Client) GetCallbackIp() *response.ResponseGetCallBackIp { - result := &ResponseGetCallBackIp{} + result := &response.ResponseGetCallBackIp{} comp.HttpGet("cgi-bin/getcallbackip", nil, result) @@ -18,9 +19,9 @@ func (comp *Client) GetCallbackIp() *ResponseGetCallBackIp { } -func (comp *Client) GetAPIDomainIP() *ResponseGetAPIDomainIP { +func (comp *Client) GetAPIDomainIP() *response.ResponseGetAPIDomainIP { - result := &ResponseGetAPIDomainIP{} + result := &response.ResponseGetAPIDomainIP{} comp.HttpGet("cgi-bin/get_api_domain_ip", nil, result) diff --git a/src/work/base/responseGetAPIDomainIP.go b/src/work/base/response/responseGetAPIDomainIP.go similarity index 90% rename from src/work/base/responseGetAPIDomainIP.go rename to src/work/base/response/responseGetAPIDomainIP.go index 93c83901..50d987ee 100644 --- a/src/work/base/responseGetAPIDomainIP.go +++ b/src/work/base/response/responseGetAPIDomainIP.go @@ -1,4 +1,4 @@ -package base +package response import ( "github.com/ArtisanCloud/go-wechat/src/kernel/response" diff --git a/src/work/base/responseGetCallbackIp.go b/src/work/base/response/responseGetCallbackIp.go similarity index 90% rename from src/work/base/responseGetCallbackIp.go rename to src/work/base/response/responseGetCallbackIp.go index e99682db..63ac44a5 100644 --- a/src/work/base/responseGetCallbackIp.go +++ b/src/work/base/response/responseGetCallbackIp.go @@ -1,4 +1,4 @@ -package base +package response import ( "github.com/ArtisanCloud/go-wechat/src/kernel/response" diff --git a/src/work/oauth/manager.go b/src/work/oauth/manager.go index d9b0ca5b..5894af55 100644 --- a/src/work/oauth/manager.go +++ b/src/work/oauth/manager.go @@ -1,3 +1,24 @@ package oauth +import ( + "github.com/ArtisanCloud/go-libs/object" + "github.com/ArtisanCloud/go-socialite/src/providers" + "github.com/ArtisanCloud/go-wechat/src/kernel" +) +type Manager struct { + Config *object.HashMap + App *kernel.ApplicationInterface + Provider *providers.WeCom +} + +func NewManager(config *object.HashMap,app *kernel.ApplicationInterface) *Manager { + + manager:= &Manager{ + config, + app, + nil, + } + return manager + +} \ No newline at end of file diff --git a/src/work/oauth/provider.go b/src/work/oauth/provider.go index d9b0ca5b..25698bb8 100644 --- a/src/work/oauth/provider.go +++ b/src/work/oauth/provider.go @@ -1,3 +1,54 @@ package oauth +import ( + "fmt" + "github.com/ArtisanCloud/go-libs/object" + "github.com/ArtisanCloud/go-wechat/src/kernel" + "github.com/ArtisanCloud/go-wechat/src/kernel/exception" + "strings" +) +func RegisterProvider(app kernel.ApplicationInterface) *Manager { + + config := *app.GetContainer().Config + + manager := NewManager(&object.HashMap{ + "wecom": &object.HashMap{ + "client_id": config["corp_id"].(string), + "client_secret": "", + "corp_id": config["corp_id"].(string), + "corp_secret": config["secret"].(string), + "redirect": prepareCallbackUrl(app), + }, + }, &app) + + scopes:= []string{ + "snsapi_base", + } + + if config["oauth.scopes"] !=nil{ + scopes = config["oauth.scopes"].([]string) + } + + if len(scopes)>0{ + + } + + return manager + +} + +func prepareCallbackUrl(app kernel.ApplicationInterface) string { + config := *app.GetContainer().Config + + callback := config["oauth.callback"].(string) + if strings.Index(callback, "http") == 0 { + return callback + } else { + // have to setup a complete url with host + defer (&exception.Exception{}).HandleException(nil, "oauth.prepare.callback.url", nil) + panic(fmt.Sprintf("OAuth callback format invalid, please make sure that schema 'http' added: %v", callback)) + + } + +} From fa776a9f5a92fb667c6000ca64b75f00c7d59935 Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Fri, 11 Jun 2021 22:05:59 +0800 Subject: [PATCH 23/25] add app config --- src/kernel/config.go | 13 +++++++++++++ src/kernel/provider.go | 15 --------------- src/kernel/providers/configProvider.go | 8 ++++++++ src/work/application.go | 4 ++++ src/work/oauth/provider.go | 15 ++++++--------- 5 files changed, 31 insertions(+), 24 deletions(-) create mode 100644 src/kernel/config.go delete mode 100644 src/kernel/provider.go create mode 100644 src/kernel/providers/configProvider.go diff --git a/src/kernel/config.go b/src/kernel/config.go new file mode 100644 index 00000000..9cc7a562 --- /dev/null +++ b/src/kernel/config.go @@ -0,0 +1,13 @@ +package kernel + +import "github.com/ArtisanCloud/go-libs/object" + +type Config struct { + *object.Collection +} + +func NewConfig(items *object.HashMap) *Config{ + return &Config{ + object.NewCollection(items), + } +} \ No newline at end of file diff --git a/src/kernel/provider.go b/src/kernel/provider.go deleted file mode 100644 index 59bb9d9c..00000000 --- a/src/kernel/provider.go +++ /dev/null @@ -1,15 +0,0 @@ -package kernel - -func RegisterProvider(app ApplicationInterface) { - // tbd - - //container := app.GetContainer() - // - //// first merge configs - //container.GetConfig() - // - //// register global http client - // - //// register log service - -} diff --git a/src/kernel/providers/configProvider.go b/src/kernel/providers/configProvider.go new file mode 100644 index 00000000..0797d5e9 --- /dev/null +++ b/src/kernel/providers/configProvider.go @@ -0,0 +1,8 @@ +package providers + +import "github.com/ArtisanCloud/go-wechat/src/kernel" + +func RegisterProvider(app kernel.ApplicationInterface) *kernel.Config { + + return kernel.NewConfig(app.GetContainer().GetConfig()) +} diff --git a/src/work/application.go b/src/work/application.go index 105ea680..30321590 100644 --- a/src/work/application.go +++ b/src/work/application.go @@ -3,6 +3,7 @@ package work import ( "github.com/ArtisanCloud/go-libs/object" "github.com/ArtisanCloud/go-wechat/src/kernel" + "github.com/ArtisanCloud/go-wechat/src/kernel/providers" "github.com/ArtisanCloud/go-wechat/src/work/auth" "github.com/ArtisanCloud/go-wechat/src/work/base" "github.com/ArtisanCloud/go-wechat/src/work/oauth" @@ -14,6 +15,7 @@ type Work struct { Base *base.Client AccessToken *auth.AccessToken OAuth *oauth.Manager + Config *kernel.Config } func NewWork(config *object.HashMap) *Work { @@ -40,6 +42,8 @@ func NewWork(config *object.HashMap) *Work { // register oauth app.OAuth = oauth.RegisterProvider(app) + app.Config = providers.RegisterProvider(app) + return app } diff --git a/src/work/oauth/provider.go b/src/work/oauth/provider.go index 25698bb8..c89094a4 100644 --- a/src/work/oauth/provider.go +++ b/src/work/oauth/provider.go @@ -18,21 +18,18 @@ func RegisterProvider(app kernel.ApplicationInterface) *Manager { "client_secret": "", "corp_id": config["corp_id"].(string), "corp_secret": config["secret"].(string), - "redirect": prepareCallbackUrl(app), + //"redirect": prepareCallbackUrl(app), }, }, &app) - scopes:= []string{ - "snsapi_base", - } - if config["oauth.scopes"] !=nil{ - scopes = config["oauth.scopes"].([]string) - } - if len(scopes)>0{ + //scopes := manager.Config - } + + //if len(scopes)>0{ + // + //} return manager From e9910f224347315cbe52d917a400359cd4902e0b Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Fri, 11 Jun 2021 22:06:54 +0800 Subject: [PATCH 24/25] ready to merge --- go.mod | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 6498d0ec..fb1f98dc 100644 --- a/go.mod +++ b/go.mod @@ -2,9 +2,9 @@ module github.com/ArtisanCloud/go-wechat go 1.16 -replace github.com/ArtisanCloud/go-libs => ../go-libs - -replace github.com/ArtisanCloud/go-socialite => ../go-socialite +//replace github.com/ArtisanCloud/go-libs => ../go-libs +// +//replace github.com/ArtisanCloud/go-socialite => ../go-socialite require ( github.com/ArtisanCloud/go-libs v1.0.10 From 7c519f869b84983bb74a6e99129f597630f010df Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Fri, 11 Jun 2021 22:16:38 +0800 Subject: [PATCH 25/25] update --- go.mod | 4 ++-- go.sum | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index fb1f98dc..749d1423 100644 --- a/go.mod +++ b/go.mod @@ -7,8 +7,8 @@ go 1.16 //replace github.com/ArtisanCloud/go-socialite => ../go-socialite require ( - github.com/ArtisanCloud/go-libs v1.0.10 - github.com/ArtisanCloud/go-socialite v0.0.0-00010101000000-000000000000 // indirect + github.com/ArtisanCloud/go-libs v1.0.11 + github.com/ArtisanCloud/go-socialite v1.0.0 github.com/gin-gonic/gin v1.7.1 // indirect github.com/go-redis/redis/v8 v8.10.0 // indirect github.com/mattn/go-isatty v0.0.13 // indirect diff --git a/go.sum b/go.sum index 26276164..6a83891d 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,7 @@ +github.com/ArtisanCloud/go-libs v1.0.11 h1:CF39XsRaOKyphG7eoUVDkdqj2L2DUKfSrDr+tZyYbMI= +github.com/ArtisanCloud/go-libs v1.0.11/go.mod h1:QdDzmJskQvtLTwVpF6KZqnFEwBfTdIpF0FLy6WHvKmA= +github.com/ArtisanCloud/go-socialite v1.0.0 h1:eQnhkQTP8a7qr3QXFU1YOaf1jHJL6UnrmLNPowruUp8= +github.com/ArtisanCloud/go-socialite v1.0.0/go.mod h1:/VYnYt7mGKZxPyWr+qPlLv1/+H6xcZP+B7XdsbNhgt0= github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -6,6 +10,7 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= 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= @@ -40,6 +45,7 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/guonaihong/gout v0.1.11 h1:Sl4OwyNtp1BoMZXankNC0DimAPuh9kgBsZIGynD9kiw= github.com/guonaihong/gout v0.1.11/go.mod h1:JkjNv1G2oRWvFgP/r4DUbYhoeIBB0zMP2j1ID+5CYpU= @@ -55,12 +61,15 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OH github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= +github.com/onsi/ginkgo v1.15.0 h1:1V1NfVQR87RtWAgp1lv9JZJ5Jap+XFGKPi00andXGi4= github.com/onsi/ginkgo v1.15.0/go.mod h1:hF8qUzuuC8DJGygJH3726JnCZX4MYbRB8yFfISqnKUg= github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= +github.com/onsi/gomega v1.10.5 h1:7n6FEkpFmfCoo2t+YYqXH0evK+a9ICQz0xcAy9dYcaQ= github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7mt48= github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= @@ -85,6 +94,7 @@ go.opentelemetry.io/otel/metric v0.19.0/go.mod h1:8f9fglJPRnXuskQmKpnad31lcLJ2Vm go.opentelemetry.io/otel/metric v0.20.0 h1:4kzhXFP+btKm4jwxpjIqjs41A7MakRFUS86bqLHTIw8= go.opentelemetry.io/otel/metric v0.20.0/go.mod h1:598I5tYlH1vzBjn+BTuhzTCSb/9debfNp6R3s7Pr1eU= go.opentelemetry.io/otel/oteltest v0.19.0/go.mod h1:tI4yxwh8U21v7JD6R3BcA/2+RBoTKFexE/PJ/nSO7IA= +go.opentelemetry.io/otel/oteltest v0.20.0 h1:HiITxCawalo5vQzdHfKeZurV8x7ljcqAgiWzF6Vaeaw= go.opentelemetry.io/otel/oteltest v0.20.0/go.mod h1:L7bgKf9ZB7qCwT9Up7i9/pn0PWIa9FqQ2IQ8LoxiGnw= go.opentelemetry.io/otel/trace v0.19.0/go.mod h1:4IXiNextNOpPnRlI4ryK69mn5iC84bjBWZQA5DXz/qg= go.opentelemetry.io/otel/trace v0.20.0 h1:1DL6EXUdcg95gukhuRRvLDO/4X5THh/5dIV52lqtnbw= @@ -126,6 +136,7 @@ golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9sn golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= @@ -147,6 +158,7 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=