From 92772e68a41ee9a083774c5691a40fb2dab31186 Mon Sep 17 00:00:00 2001 From: yoyofx Date: Wed, 10 Nov 2021 20:50:20 +0800 Subject: [PATCH 01/10] action parameters mapping functions --- .../simpleweb/contollers/usercontroller.go | 2 +- web/mvc/action_method_executor.go | 19 ++++----- web/mvc/action_parameters_mapping_func.go | 39 +++++++++++++++++++ 3 files changed, 50 insertions(+), 10 deletions(-) create mode 100644 web/mvc/action_parameters_mapping_func.go diff --git a/examples/simpleweb/contollers/usercontroller.go b/examples/simpleweb/contollers/usercontroller.go index 4be9bb6..efe54cd 100644 --- a/examples/simpleweb/contollers/usercontroller.go +++ b/examples/simpleweb/contollers/usercontroller.go @@ -23,7 +23,7 @@ func NewUserController(userAction models.IUserAction, sd servicediscovery.IServi } type RegisterRequest struct { - mvc.RequestBody + //mvc.RequestBody UserName string `param:"UserName"` Password string `param:"Password"` } diff --git a/web/mvc/action_method_executor.go b/web/mvc/action_method_executor.go index 35885ca..cfcc916 100644 --- a/web/mvc/action_method_executor.go +++ b/web/mvc/action_method_executor.go @@ -2,7 +2,6 @@ package mvc import ( "errors" - "fmt" "github.com/yoyofx/yoyogo/abstractions/xlog" "github.com/yoyofx/yoyogo/web/context" "github.com/yoyofxteam/reflectx" @@ -20,7 +19,7 @@ func NewActionMethodExecutor() ActionMethodExecutor { func (actionExecutor ActionMethodExecutor) Execute(ctx *ActionExecutorContext) interface{} { if ctx.Controller != nil { methodInfo := ctx.ActionDescriptor.MethodInfo - values := getParamValues(methodInfo.Parameters, ctx.Context) + values := actionExecutor.getParamValues(methodInfo.Parameters, ctx.Context) returns := methodInfo.InvokeWithValue(reflect.ValueOf(ctx.Controller), values...) if len(returns) > 0 { responseData := returns[0] @@ -31,7 +30,7 @@ func (actionExecutor ActionMethodExecutor) Execute(ctx *ActionExecutorContext) i return nil } -func getParamValues(paramList []reflectx.MethodParameterInfo, ctx *context.HttpContext) []reflect.Value { +func (actionExecutor ActionMethodExecutor) getParamValues(paramList []reflectx.MethodParameterInfo, ctx *context.HttpContext) []reflect.Value { if len(paramList) == 0 { return nil } @@ -41,11 +40,11 @@ func getParamValues(paramList []reflectx.MethodParameterInfo, ctx *context.HttpC continue } val, err := requestParamTypeConvertFunc(index, param, ctx) - if err == nil { - values[index-1] = val + if err != nil { + actionExecutor.logger.Error(err.Error()) // throw binding error } + values[index-1] = val } - return values } @@ -61,14 +60,16 @@ func requestParamTypeConvertFunc(index int, parameter reflectx.MethodParameterIn case "HttpContext": value = reflect.ValueOf(ctx) default: + reqBindingData := reflect.New(paramType).Interface() if paramType.NumField() > 0 && paramType.Field(0).Name == "RequestBody" { - reqBindingData := reflect.New(paramType).Interface() bindErr := ctx.Bind(reqBindingData) if bindErr != nil { - fmt.Println(bindErr) + panic(bindErr) } - value = reflect.ValueOf(reqBindingData) + } else { + err = errors.New("Can't bind non mvc.RequestBody!") } + value = reflect.ValueOf(reqBindingData) } return value, err } diff --git a/web/mvc/action_parameters_mapping_func.go b/web/mvc/action_parameters_mapping_func.go new file mode 100644 index 0000000..34adf70 --- /dev/null +++ b/web/mvc/action_parameters_mapping_func.go @@ -0,0 +1,39 @@ +package mvc + +import ( + "errors" + "github.com/yoyofx/yoyogo/web/context" + "reflect" +) + +type ActionParametersMappingFunc interface { + Mapping(paramName string, paramTypeName string, paramType reflect.Type, sourceContext *context.HttpContext) (reflect.Value, error) +} + +type httpContextMapping struct{} + +func (mapping httpContextMapping) Mapping(paramName string, paramTypeName string, paramType reflect.Type, sourceContext *context.HttpContext) (reflect.Value, error) { + var value reflect.Value + var err error + if paramTypeName == "HttpContext" { + value = reflect.ValueOf(sourceContext) + } else { + err = errors.New("not HttpContext ") + } + return value, err +} + +type requestBodyMapping struct{} + +func (mapping requestBodyMapping) Mapping(paramName string, paramTypeName string, paramType reflect.Type, sourceContext *context.HttpContext) (reflect.Value, error) { + var value reflect.Value + var err error + reqBindingData := reflect.New(paramType).Interface() + if paramType.NumField() > 0 && paramType.Field(0).Name == "RequestBody" { + err = sourceContext.Bind(reqBindingData) + } else { + err = errors.New("Can't bind non mvc.RequestBody!") + } + value = reflect.ValueOf(reqBindingData) + return value, err +} -- Gitee From 2661c29d58f8dd5f05ae13895c3c9a6498a5e8e7 Mon Sep 17 00:00:00 2001 From: yoyofx Date: Wed, 10 Nov 2021 20:56:02 +0800 Subject: [PATCH 02/10] action parameters mapping functions v2 --- web/mvc/action_method_executor.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/web/mvc/action_method_executor.go b/web/mvc/action_method_executor.go index cfcc916..2860090 100644 --- a/web/mvc/action_method_executor.go +++ b/web/mvc/action_method_executor.go @@ -51,11 +51,14 @@ func (actionExecutor ActionMethodExecutor) getParamValues(paramList []reflectx.M func requestParamTypeConvertFunc(index int, parameter reflectx.MethodParameterInfo, ctx *context.HttpContext) (reflect.Value, error) { var value reflect.Value var err error = nil + paramType := parameter.ParameterType if paramType.Kind() == reflect.Ptr { paramType = paramType.Elem() } if paramType.Kind() == reflect.Struct { + // Mapping -> parameter.Name , paramType.Name() ,paramType, ctx + switch paramType.Name() { case "HttpContext": value = reflect.ValueOf(ctx) -- Gitee From d6ed0ce77d176c6ad7e14e3657b9c266f4d52b51 Mon Sep 17 00:00:00 2001 From: yoyofx Date: Fri, 12 Nov 2021 15:35:51 +0800 Subject: [PATCH 03/10] mixed bindings. supported tags: json , form , uri ,header. --- .../simpleweb/contollers/usercontroller.go | 20 +++++---- web/binding/query_binding.go | 2 +- web/mvc/action_method_executor.go | 42 ++++++++++++------- web/mvc/action_parameters_mapping_func.go | 37 ++++++++++++---- 4 files changed, 68 insertions(+), 33 deletions(-) diff --git a/examples/simpleweb/contollers/usercontroller.go b/examples/simpleweb/contollers/usercontroller.go index efe54cd..191aaa6 100644 --- a/examples/simpleweb/contollers/usercontroller.go +++ b/examples/simpleweb/contollers/usercontroller.go @@ -1,7 +1,6 @@ package contollers import ( - "fmt" "github.com/yoyofx/yoyogo/abstractions/servicediscovery" "github.com/yoyofx/yoyogo/web/actionresult" "github.com/yoyofx/yoyogo/web/binding" @@ -23,22 +22,25 @@ func NewUserController(userAction models.IUserAction, sd servicediscovery.IServi } type RegisterRequest struct { - //mvc.RequestBody - UserName string `param:"UserName"` - Password string `param:"Password"` + mvc.RequestBody `route:"/{id}/{tenant}"` + + UserName string `uri:"userName"` + Password string `uri:"password"` } func (controller UserController) Register(ctx *context.HttpContext, request *RegisterRequest) mvc.ApiResult { return mvc.ApiResult{Success: true, Message: "ok", Data: request} } -func (controller UserController) GetUserName(ctx *context.HttpContext, request *RegisterRequest) actionresult.IActionResult { - result := mvc.ApiResult{Success: true, Message: "ok", Data: request} - fmt.Println("hello world") - return actionresult.Json{Data: result} +type PostUserInfoRequest struct { + mvc.RequestBody //`route:"/{id}"` + + UserName string `form:"userName" json:"userName"` + Password string `form:"password" json:"password"` + Token string `header:"Authorization" json:"token"` } -func (controller UserController) PostUserInfo(ctx *context.HttpContext, request *RegisterRequest) actionresult.IActionResult { +func (controller UserController) PostUserInfo(ctx *context.HttpContext, request *PostUserInfoRequest) actionresult.IActionResult { return actionresult.Json{Data: mvc.ApiResult{Success: true, Message: "ok", Data: context.H{ "user": ctx.GetUser(), "request": request, diff --git a/web/binding/query_binding.go b/web/binding/query_binding.go index be23e80..949308c 100644 --- a/web/binding/query_binding.go +++ b/web/binding/query_binding.go @@ -14,7 +14,7 @@ func (queryBinding) Name() string { func (queryBinding) Bind(req *http.Request, obj interface{}) error { values := req.URL.Query() fmt.Println(values) - if err := mapForm(obj, values); err != nil { + if err := mapUri(obj, values); err != nil { return err } return validate(obj) diff --git a/web/mvc/action_method_executor.go b/web/mvc/action_method_executor.go index 2860090..d167fac 100644 --- a/web/mvc/action_method_executor.go +++ b/web/mvc/action_method_executor.go @@ -57,24 +57,34 @@ func requestParamTypeConvertFunc(index int, parameter reflectx.MethodParameterIn paramType = paramType.Elem() } if paramType.Kind() == reflect.Struct { - // Mapping -> parameter.Name , paramType.Name() ,paramType, ctx - - switch paramType.Name() { - case "HttpContext": - value = reflect.ValueOf(ctx) - default: - reqBindingData := reflect.New(paramType).Interface() - if paramType.NumField() > 0 && paramType.Field(0).Name == "RequestBody" { - bindErr := ctx.Bind(reqBindingData) - if bindErr != nil { - panic(bindErr) - } - } else { - err = errors.New("Can't bind non mvc.RequestBody!") - } - value = reflect.ValueOf(reqBindingData) + // Mapping * struct type -> parameter.Name , paramType.Name() ,paramType, ctx + mappingFunc, hasMapping := RequestMppingFuncs[paramType.Name()] + if hasMapping { + value, err = mappingFunc(parameter.Name, paramType.Name(), paramType, ctx) + } else { + value, err = RequestMppingFuncs["Default"](parameter.Name, paramType.Name(), paramType, ctx) } + + //switch paramType.Name() { + //case "HttpContext": + // value = reflect.ValueOf(ctx) + //default: + // reqBindingData := reflect.New(paramType).Interface() + // if paramType.NumField() > 0 && paramType.Field(0).Name == "RequestBody" { + // bindErr := ctx.Bind(reqBindingData) + // bindErr2:= ctx.BindWith(reqBindingData, binding.Query) + // if bindErr != nil || bindErr2!=nil { + // panic(bindErr.Error() + bindErr2.Error()) + // } + // } else { + // err = errors.New("Can't bind non mvc.RequestBody!") + // } + // value = reflect.ValueOf(reqBindingData) + //} return value, err + } else { + // normal type , such as int ,string, float } + return value, errors.New("the type not support") } diff --git a/web/mvc/action_parameters_mapping_func.go b/web/mvc/action_parameters_mapping_func.go index 34adf70..74c60f3 100644 --- a/web/mvc/action_parameters_mapping_func.go +++ b/web/mvc/action_parameters_mapping_func.go @@ -2,17 +2,19 @@ package mvc import ( "errors" + "github.com/yoyofx/yoyogo/web/binding" "github.com/yoyofx/yoyogo/web/context" "reflect" ) -type ActionParametersMappingFunc interface { - Mapping(paramName string, paramTypeName string, paramType reflect.Type, sourceContext *context.HttpContext) (reflect.Value, error) +var RequestMppingFuncs map[string]ActionParametersMappingFunc = map[string]ActionParametersMappingFunc{ + "HttpContext": httpContextMappingMapping, + "Default": requestBodyMappingMapping, } -type httpContextMapping struct{} +type ActionParametersMappingFunc func(paramName string, paramTypeName string, paramType reflect.Type, sourceContext *context.HttpContext) (reflect.Value, error) -func (mapping httpContextMapping) Mapping(paramName string, paramTypeName string, paramType reflect.Type, sourceContext *context.HttpContext) (reflect.Value, error) { +func httpContextMappingMapping(paramName string, paramTypeName string, paramType reflect.Type, sourceContext *context.HttpContext) (reflect.Value, error) { var value reflect.Value var err error if paramTypeName == "HttpContext" { @@ -23,14 +25,35 @@ func (mapping httpContextMapping) Mapping(paramName string, paramTypeName string return value, err } -type requestBodyMapping struct{} - -func (mapping requestBodyMapping) Mapping(paramName string, paramTypeName string, paramType reflect.Type, sourceContext *context.HttpContext) (reflect.Value, error) { +/** + 绑定 +form-data/multipart-form , json , uri , header +tags: json , form , uri ,header +*/ +func requestBodyMappingMapping(paramName string, paramTypeName string, paramType reflect.Type, sourceContext *context.HttpContext) (reflect.Value, error) { var value reflect.Value var err error reqBindingData := reflect.New(paramType).Interface() + + fmTags := map[string]bool{"header": false, "uri": false} + for fi := 0; fi < paramType.NumField(); fi++ { + for key, _ := range fmTags { + _, inTag := paramType.Field(fi).Tag.Lookup(key) + if inTag { + fmTags[key] = inTag + continue + } + } + } + if paramType.NumField() > 0 && paramType.Field(0).Name == "RequestBody" { err = sourceContext.Bind(reqBindingData) + if fmTags["uri"] { + _ = sourceContext.BindWith(reqBindingData, binding.Query) + } else if fmTags["header"] { + _ = sourceContext.BindWith(reqBindingData, binding.Header) + } + } else { err = errors.New("Can't bind non mvc.RequestBody!") } -- Gitee From 0843458cbce5ec14853f136495a38451f17169e0 Mon Sep 17 00:00:00 2001 From: yoyofx Date: Thu, 30 Dec 2021 22:32:30 +0800 Subject: [PATCH 04/10] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9C=AC=E5=9C=B0?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=AF=B9=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/simpleweb/config_dev.yml | 8 ++++---- examples/simpleweb/main.go | 15 ++++++++++++--- pkg/configuration/localconfig.go | 7 +++++++ 3 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 pkg/configuration/localconfig.go diff --git a/examples/simpleweb/config_dev.yml b/examples/simpleweb/config_dev.yml index 1c5f009..8d19946 100644 --- a/examples/simpleweb/config_dev.yml +++ b/examples/simpleweb/config_dev.yml @@ -46,17 +46,17 @@ yoyogo: strategy: "round-robin" # round-robin , weight-time , random type: "nacos" metadata: - url: "nacos.yoyogo.run" - port: 80 + url: "47.100.213.49" + port: 8848 namespace: "public" group: "DEFAULT_GROUP" cluster: "" configserver: dataId: "simple_demo" auth: - enable: true + enable: false username: "nacos" - password: "nacos" + password: "P@ssW0rd" endpoint: "" regionId: "" accessKey: "" diff --git a/examples/simpleweb/main.go b/examples/simpleweb/main.go index 28df430..4cddb49 100644 --- a/examples/simpleweb/main.go +++ b/examples/simpleweb/main.go @@ -6,7 +6,6 @@ import ( "github.com/yoyofx/yoyogo/abstractions" "github.com/yoyofx/yoyogo/abstractions/xlog" "github.com/yoyofx/yoyogo/pkg/configuration" - nacosconfig "github.com/yoyofx/yoyogo/pkg/configuration/nacos" _ "github.com/yoyofx/yoyogo/pkg/datasources/mysql" _ "github.com/yoyofx/yoyogo/pkg/datasources/redis" "github.com/yoyofx/yoyogo/pkg/servicediscovery/nacos" @@ -45,8 +44,18 @@ func main() { //* Create the builder of Web host func CreateCustomBuilder() *abstractions.HostBuilder { - config := nacosconfig.RemoteConfig("config") - //config := apollo.RemoteConfig("config") + // local away: + // config := abstractions.NewConfigurationBuilder(). + // AddEnvironment(). + // AddYamlFile("config").Build() + // remote away: + // -- github.com/yoyofx/yoyogo/pkg/configuration/nacos + // -- github.com/yoyofx/yoyogo/pkg/configuration/apollo + // config := nacosconfig.RemoteConfig("config") + // config := apollo.RemoteConfig("config") + + config := configuration.LocalConfig("config") + return web.NewWebHostBuilder(). UseConfiguration(config). Configure(func(app *web.ApplicationBuilder) { diff --git a/pkg/configuration/localconfig.go b/pkg/configuration/localconfig.go new file mode 100644 index 0000000..cc03f06 --- /dev/null +++ b/pkg/configuration/localconfig.go @@ -0,0 +1,7 @@ +package configuration + +import "github.com/yoyofx/yoyogo/abstractions" + +func LocalConfig(configPath string) *abstractions.Configuration { + return abstractions.NewConfigurationBuilder().AddEnvironment().AddYamlFile(configPath).Build() +} -- Gitee From 460035fdb905ffe74595fb8c5fa1084f54a32784 Mon Sep 17 00:00:00 2001 From: yoyofx Date: Thu, 30 Dec 2021 23:27:43 +0800 Subject: [PATCH 05/10] =?UTF-8?q?=E4=BC=98=E5=8C=96dockerfile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/simpleweb/Dockerfile | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/simpleweb/Dockerfile b/examples/simpleweb/Dockerfile index 772633c..f83491d 100644 --- a/examples/simpleweb/Dockerfile +++ b/examples/simpleweb/Dockerfile @@ -16,8 +16,10 @@ COPY . . WORKDIR /build/examples/simpleweb RUN ls -a # 将我们的代码编译成二进制可执行文件 app -RUN go mod download -RUN go build -o app . + +RUN go mod download \ + && go mod tidy \ + && go build -o app . ################### # 接下来创建一个小镜像 -- Gitee From fe62813e2ca9868d694ef9e395e83daa24f1e2a9 Mon Sep 17 00:00:00 2001 From: anjoy8 <3143422472@qq.com> Date: Tue, 8 Feb 2022 19:06:11 +0800 Subject: [PATCH 06/10] fix: add vendor to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 7e3dc02..6bc2200 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ go.sum __debug_bin +**/vendor/** -- Gitee From e2e7716a302bbe7b07a966769c86c6f0e56ab2ca Mon Sep 17 00:00:00 2001 From: yoyofx Date: Thu, 3 Mar 2022 16:12:14 +0800 Subject: [PATCH 07/10] remove nacos config for simpleweb examples --- examples/simpleweb/main.go | 4 ++-- pkg/configuration/configobjectadder.go | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/examples/simpleweb/main.go b/examples/simpleweb/main.go index 28df430..f3250b4 100644 --- a/examples/simpleweb/main.go +++ b/examples/simpleweb/main.go @@ -6,7 +6,6 @@ import ( "github.com/yoyofx/yoyogo/abstractions" "github.com/yoyofx/yoyogo/abstractions/xlog" "github.com/yoyofx/yoyogo/pkg/configuration" - nacosconfig "github.com/yoyofx/yoyogo/pkg/configuration/nacos" _ "github.com/yoyofx/yoyogo/pkg/datasources/mysql" _ "github.com/yoyofx/yoyogo/pkg/datasources/redis" "github.com/yoyofx/yoyogo/pkg/servicediscovery/nacos" @@ -45,8 +44,9 @@ func main() { //* Create the builder of Web host func CreateCustomBuilder() *abstractions.HostBuilder { - config := nacosconfig.RemoteConfig("config") + //config := nacosconfig.RemoteConfig("config") //config := apollo.RemoteConfig("config") + config := configuration.Local("config") return web.NewWebHostBuilder(). UseConfiguration(config). Configure(func(app *web.ApplicationBuilder) { diff --git a/pkg/configuration/configobjectadder.go b/pkg/configuration/configobjectadder.go index f9e843d..a8e159e 100644 --- a/pkg/configuration/configobjectadder.go +++ b/pkg/configuration/configobjectadder.go @@ -1,6 +1,7 @@ package configuration import ( + "github.com/yoyofx/yoyogo/abstractions" "github.com/yoyofxteam/dependencyinjection" ) @@ -12,3 +13,10 @@ func AddConfiguration(sc *dependencyinjection.ServiceCollection, objType interfa //fmt.Println(sectionName) sc.AddTransient(objType) } + +func Local(configName string) *abstractions.Configuration { + config := abstractions.NewConfigurationBuilder(). + AddEnvironment(). + AddYamlFile(configName).Build() + return config +} -- Gitee From 705b1e95fc37ae838fd19a71c5926a9349a4291c Mon Sep 17 00:00:00 2001 From: yoyofx Date: Thu, 3 Mar 2022 16:15:14 +0800 Subject: [PATCH 08/10] remove nacos config for simpleweb examples --- examples/simpleweb/main.go | 4 ++-- pkg/configuration/configobjectadder.go | 8 -------- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/examples/simpleweb/main.go b/examples/simpleweb/main.go index 28df430..d3925b0 100644 --- a/examples/simpleweb/main.go +++ b/examples/simpleweb/main.go @@ -6,7 +6,6 @@ import ( "github.com/yoyofx/yoyogo/abstractions" "github.com/yoyofx/yoyogo/abstractions/xlog" "github.com/yoyofx/yoyogo/pkg/configuration" - nacosconfig "github.com/yoyofx/yoyogo/pkg/configuration/nacos" _ "github.com/yoyofx/yoyogo/pkg/datasources/mysql" _ "github.com/yoyofx/yoyogo/pkg/datasources/redis" "github.com/yoyofx/yoyogo/pkg/servicediscovery/nacos" @@ -45,8 +44,9 @@ func main() { //* Create the builder of Web host func CreateCustomBuilder() *abstractions.HostBuilder { - config := nacosconfig.RemoteConfig("config") + //config := nacosconfig.RemoteConfig("config") //config := apollo.RemoteConfig("config") + config := configuration.LocalConfig("config") return web.NewWebHostBuilder(). UseConfiguration(config). Configure(func(app *web.ApplicationBuilder) { diff --git a/pkg/configuration/configobjectadder.go b/pkg/configuration/configobjectadder.go index a8e159e..f9e843d 100644 --- a/pkg/configuration/configobjectadder.go +++ b/pkg/configuration/configobjectadder.go @@ -1,7 +1,6 @@ package configuration import ( - "github.com/yoyofx/yoyogo/abstractions" "github.com/yoyofxteam/dependencyinjection" ) @@ -13,10 +12,3 @@ func AddConfiguration(sc *dependencyinjection.ServiceCollection, objType interfa //fmt.Println(sectionName) sc.AddTransient(objType) } - -func Local(configName string) *abstractions.Configuration { - config := abstractions.NewConfigurationBuilder(). - AddEnvironment(). - AddYamlFile(configName).Build() - return config -} -- Gitee From 5fd9112b8f42215cbdf6f87b33d6b598b9196b31 Mon Sep 17 00:00:00 2001 From: yoyofx Date: Thu, 3 Mar 2022 16:37:46 +0800 Subject: [PATCH 09/10] alert --- examples/grafanaalertwebhook/go.mod | 2 +- examples/grafanaalertwebhook/wechatrequests/qiyewechat.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/grafanaalertwebhook/go.mod b/examples/grafanaalertwebhook/go.mod index 641f4cb..ab074bb 100644 --- a/examples/grafanaalertwebhook/go.mod +++ b/examples/grafanaalertwebhook/go.mod @@ -3,8 +3,8 @@ module grafanaalertwebhook go 1.16 require ( - github.com/yoyofxteam/dependencyinjection v1.0.0 github.com/yoyofx/yoyogo v0.0.0 + gopkg.in/go-playground/assert.v1 v1.2.1 ) replace github.com/yoyofx/yoyogo => ../../ diff --git a/examples/grafanaalertwebhook/wechatrequests/qiyewechat.go b/examples/grafanaalertwebhook/wechatrequests/qiyewechat.go index 34e366b..c3ac640 100644 --- a/examples/grafanaalertwebhook/wechatrequests/qiyewechat.go +++ b/examples/grafanaalertwebhook/wechatrequests/qiyewechat.go @@ -10,7 +10,7 @@ import ( "net/http" ) -func postWechatMessage(sendUrl, msg string) string { +func PostWechatMessage(sendUrl, msg string) string { client := &http.Client{} req, _ := http.NewRequest("POST", sendUrl, bytes.NewBuffer([]byte(msg))) req.Header.Set("Content-Type", "application/json") @@ -61,5 +61,5 @@ func SendTxtMessage(request GrafanaAlertRequest, config abstractions.IConfigurat logger.Info("send message:%s", msgStr) //return sendUrl + msgStr - return postWechatMessage(sendUrl, msgStr) + return PostWechatMessage(sendUrl, msgStr) } -- Gitee From 6acc4b3ef9185d5b3c66235937682cf44d220d78 Mon Sep 17 00:00:00 2001 From: yoyofx Date: Thu, 3 Mar 2022 16:42:06 +0800 Subject: [PATCH 10/10] alert --- .../grafanaalertwebhook/tests/unit_test.go | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 examples/grafanaalertwebhook/tests/unit_test.go diff --git a/examples/grafanaalertwebhook/tests/unit_test.go b/examples/grafanaalertwebhook/tests/unit_test.go new file mode 100644 index 0000000..bd8457a --- /dev/null +++ b/examples/grafanaalertwebhook/tests/unit_test.go @@ -0,0 +1,32 @@ +package tests + +import ( + "encoding/json" + "gopkg.in/go-playground/assert.v1" + "grafanaalertwebhook/wechatrequests" + "testing" +) + +func TestMessage(t *testing.T) { + + var message *wechatrequests.MarkdownMessage + message = &wechatrequests.MarkdownMessage{ + Markdown: struct { + Content string `json:"content" gorm:"column:content"` + }{ + Content: "## " + "test" + ",请相关同事注意。\n" + + " > [报警信息] : " + "test" + "\n" + + " > [报警次数] : " + "0" + "次" + "\n" + + " > [报警明细] : (" + "app" + ")\n", + }, + Msgtype: "markdown", + } + + msg, _ := json.Marshal(message) + msgStr := string(msg) + + //return sendUrl + msgStr + dd := wechatrequests.PostWechatMessage("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=93fb0726-9794-49f5-b10f-dac3c85396da", msgStr) + + assert.Equal(t, dd != "", true) +} -- Gitee