cloudwego / cwgo

An all-in-one code generation tool for CloudWeGo
Apache License 2.0
107 stars 54 forks source link

在使用hex生成同时启用 HTTP 和 RPC 服务的代码时,如何配置自定义模板? #207

Open AdrianWangs opened 3 months ago

AdrianWangs commented 3 months ago

我的项目的每个微服务都是使用 hex 生成代码的,但是最近发现每次生成代码都有一些重复的自定义配置需要编写,所以想要写一个自己的代码生成模板,但是我发现下载官方的 kitex/server的模板,并结合--hex生成命令时会报错缺少 package.yaml:

[ERROR]/Users/user/go/pkg/mod/github.com/cloudwego/hertz/cmd/hz@v0.8.1/thrift/plugin.go:162: generate package failed: init layout config failed, err: read layout config from  ../../idl/tpl/kitex/server/standard/package.yaml failed, err: open ../../idl/tpl/kitex/server/standard/package.yaml: no such file or directory

execute plugin 'hertz' failed: exit status 4

但是我把 hertz 的 package.yaml直接复制进来使用--hex 生成代码又会报错:

cwgo server --type RPC --module github.com/AdrianWangs/ai-nexus/go-service/test-user --server_name test-user-service  --idl ../../idl/user-service.thrift --template ../../idl/tpl/kitex/server/standard --hex
[WARN] stdout:

[WARN] stderr:
panic: runtime error: index out of range [-1]

goroutine 1 [running]:
github.com/cloudwego/kitex/tool/internal_pkg/generator.(*customGenerator).commonGenerate(0xc000057a60, 0xc0007fcbd0)
        /Users/user/go/pkg/mod/github.com/cloudwego/kitex@v0.9.1/tool/internal_pkg/generator/custom_template.go:149 +0x730
github.com/cloudwego/kitex/tool/internal_pkg/generator.renderFile(0x0?, {0xc0001e484b?, 0xa?}, 0xc0005b6b00?)
        /Users/user/go/pkg/mod/github.com/cloudwego/kitex@v0.9.1/tool/internal_pkg/generator/custom_template.go:208 +0x5b
github.com/cloudwego/kitex/tool/internal_pkg/generator.(*generator).GenerateCustomPackage(0xc000057c98, 0xc0005b6b00)
        /Users/user/go/pkg/mod/github.com/cloudwego/kitex@v0.9.1/tool/internal_pkg/generator/custom_template.go:192 +0x18c
github.com/cloudwego/kitex/tool/internal_pkg/pluginmode/thriftgo.Run()
        /Users/user/go/pkg/mod/github.com/cloudwego/kitex@v0.9.1/tool/internal_pkg/pluginmode/thriftgo/plugin.go:95 +0x4b0
main.kitexPluginMode()
        /Users/user/go/pkg/mod/github.com/cloudwego/cwgo@v0.1.1/cwgo.go:57 +0x90
main.main()
        /Users/user/go/pkg/mod/github.com/cloudwego/cwgo@v0.1.1/cwgo.go:38 +0x28e

execute plugin 'kitex' failed: exit status 2

请问官方能否将这一个相关的模板文件共享出来?

li-jin-gou commented 3 months ago

我们看下这个问题 @AdrianWangs