This commit is contained in:
huangsimin@fusen.cn 2023-12-06 12:08:20 +08:00
parent 34d9e79764
commit e2eb5ad78c
4 changed files with 53 additions and 103 deletions

View File

@ -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
}

View File

@ -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]()

View File

@ -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
}
var gatewayAddrress = "http://localhost:9900"

View File

@ -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: "*"
};
}
}
// 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;
}