From e2eb5ad78c87f1d4a3dad037430fe783e70f176c Mon Sep 17 00:00:00 2001 From: "huangsimin@fusen.cn" Date: Wed, 6 Dec 2023 12:08:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- goutils/proto_build/main.go | 27 +++++- .../tpls/http_grpc_method_test.tpl | 10 --- .../proto_build/tpls/http_grpc_method_var.tpl | 88 +------------------ service/notify.proto | 31 ++++++- 4 files changed, 53 insertions(+), 103 deletions(-) diff --git a/goutils/proto_build/main.go b/goutils/proto_build/main.go index a5d52fd..e0364bc 100644 --- a/goutils/proto_build/main.go +++ b/goutils/proto_build/main.go @@ -212,7 +212,7 @@ func ExecProtoc(workerSpaceDir, serviceProtoDir, genDir, packageName string, pro allServiceNames := getAllServiceName() - protoCmdStr := fmt.Sprintf(`protoc -I %s --go_out %s --go_opt paths=source_relative --go-grpc_out %s --go-grpc_opt paths=source_relative --grpc-gateway_out %s --grpc-gateway_opt paths=source_relative`, "proto", genDir, genDir, genDir) + protoCmdStr := fmt.Sprintf(`protoc -I %s --go_out %s --go_opt paths=source_relative --go-grpc_out %s --go-grpc_opt paths=source_relative `, "proto", genDir, genDir) for _, sname := range allServiceNames { protoCmdStr += importFileCmdStr(serviceNameEncode(packageName, sname), projectName) } @@ -292,13 +292,28 @@ func ExecCreateAutoLogic(workerSpaceDir string, ServiceName string, genDir, pack for _, info := range infos { // logicStructNames = append(logicStructNames, info.StructName) - var methods []map[string]string + var methods []map[string]interface{} for _, met := range info.Method { - methodMap := map[string]string{ + var params []string + for _, p := range met.Params { + if p == "context.Context" { + p = "ctx " + p + } else if strings.HasPrefix(p, "*service.") { + p = "req " + p + } else { + + } + + params = append(params, p) + + } + + methodMap := map[string]interface{}{ "StructName": info.StructName, "MethodName": met.MethodName, + "Params": met.Params, "ParamCtx": met.Params[0], "ParamReq": met.Params[1], "MethodReturn": met.Returns[0], @@ -363,7 +378,7 @@ func ExecCreateAutoLogic(workerSpaceDir string, ServiceName string, genDir, pack ss.StructServiceName = info.ServiceName for _, method := range methods { - fileName := convertToSnakeCase(method["MethodName"]) + fileName := convertToSnakeCase(method["MethodName"].(string)) method["ProjectName"] = projectName method["PackageName"] = logicPackageName createFileWithPermNotExists(fmt.Sprintf("%s/%s_logic.go", logicPath, fileName), func(f io.Writer) error { @@ -1133,6 +1148,10 @@ func ParseGrpcServerInfo(grpcPath string) (infos []*GrpcServerInfo) { // 打印接口方法 for _, method := range ifaceType.Methods.List { + if len(method.Names) == 0 { + continue + } + if !isUpper(method.Names[0].Name) { continue } diff --git a/goutils/proto_build/tpls/http_grpc_method_test.tpl b/goutils/proto_build/tpls/http_grpc_method_test.tpl index 105722d..c1775d1 100644 --- a/goutils/proto_build/tpls/http_grpc_method_test.tpl +++ b/goutils/proto_build/tpls/http_grpc_method_test.tpl @@ -12,16 +12,6 @@ import ( var {{.RequestVar}} *service.{{.RequestStruct}} - -func {{.MethodName}}Http(reqHandler func(*http.Request) error) (any, error) { - resp, err := HttpRequest({{.HttpMethod}}, gatewayAddrress+{{.UrlPath}}, {{.RequestVar}}, reqHandler) - if err != nil { - // t.Error(err) - return nil, err - } - return resp, nil -} - func {{.MethodName}}RPC() (any, error) { if fusen == nil { fusen := env.NewFusenTest[config.Config]() diff --git a/goutils/proto_build/tpls/http_grpc_method_var.tpl b/goutils/proto_build/tpls/http_grpc_method_var.tpl index 99e850a..ea98445 100644 --- a/goutils/proto_build/tpls/http_grpc_method_var.tpl +++ b/goutils/proto_build/tpls/http_grpc_method_var.tpl @@ -15,90 +15,4 @@ import ( ) var fusen *env.Fusen[config.Config] -var gatewayAddrress = "http://localhost:9900" - - -func HttpRequest(method, urlStr string, body interface{}, reqHandler func(*http.Request) error) (map[string]interface{}, error) { - var jsonBody *bytes.Buffer - var queryParams url.Values - if method == "GET" { - queryParams = JsonTagToURLValues(body) - jsonBody = bytes.NewBuffer(nil) - } else { - // 将请求体转换为JSON格式 - jsonData, err := json.Marshal(body) - if err != nil { - log.Printf("Failed to marshal request body: %s\n", err.Error()) - return nil, err - } - jsonBody = bytes.NewBuffer(jsonData) - log.Println(string(jsonData)) - } - - // 创建URL对象并添加查询参数 - u, err := url.Parse(urlStr) - if err != nil { - fmt.Printf("Failed to parse URL: %s\n", err.Error()) - return nil, err - } - u.RawQuery = queryParams.Encode() - // 创建HTTP请求 - req, err := http.NewRequest(method, u.String(), jsonBody) - if err != nil { - fmt.Printf("Failed to create HTTP request: %s\n", err.Error()) - return nil, err - } - - // 设置请求头 - req.Header.Set("Content-Type", "application/json") - - if reqHandler != nil { - reqHandler(req) - } - - // 发送HTTP请求 - client := http.DefaultClient - client.Timeout = time.Second * 60 - resp, err := client.Do(req) - if err != nil { - fmt.Printf("HTTP request failed: %s\n", err.Error()) - return nil, err - } - - if resp.StatusCode != 200 { - return nil, fmt.Errorf("%s", resp.Status) - } - - defer resp.Body.Close() - - // 解析响应体 - var response map[string]interface{} - err = json.NewDecoder(resp.Body).Decode(&response) - if err != nil { - fmt.Printf("Failed to decode response body: %s\n", err.Error()) - return nil, err - } - return response, nil -} - -func JsonTagToURLValues(body interface{}) url.Values { - values := url.Values{} - - v := reflect.ValueOf(body) - if v.Kind() == reflect.Ptr { - v = v.Elem() - } - t := v.Type() - - for i := 0; i < t.NumField(); i++ { - field := t.Field(i) - if field.IsExported() { - if jsonTag, ok := field.Tag.Lookup("json"); ok { - jtag := strings.Split(jsonTag, ",")[0] - value := v.Field(i).String() - values.Set(jtag, value) - } - } - } - return values -} \ No newline at end of file +var gatewayAddrress = "http://localhost:9900" \ No newline at end of file diff --git a/service/notify.proto b/service/notify.proto index 2d5ed02..bda3a0b 100644 --- a/service/notify.proto +++ b/service/notify.proto @@ -8,15 +8,42 @@ import "google/api/annotations.proto"; import "service/basic.proto"; import "google/protobuf/struct.proto"; import "google/protobuf/any.proto"; +import "google/api/httpbody.proto"; //定义服务 service notify { + // 邮件注册确认 + rpc EmailSend(basic.Request) returns (basic.Response) { + option (google.api.http) = { + post: "/api/notify/email/send" + body: "*" + }; + } // 邮件注册确认 rpc EmailRegisterConfirm(basic.Request) returns (basic.Response) { option (google.api.http) = { - post: "/api/notify/email" + post: "/api/notify/email/register/confirm" body: "*" }; } -} \ No newline at end of file + + + + // rpc StreamTest(stream EmailStreamReq) returns (basic.Response) { + // option (google.api.http) = { + // post: "/api/notify/email/stream" + // body: "file_content" + // }; + // } +} + +message EmailSendReq { + +} + +message EmailStreamReq { + string file_name = 1; + google.api.HttpBody file_content = 2; +} +