proto/goutils/proto_build/tpls/logic_grpc_struct.tpl

63 lines
1.5 KiB
Smarty
Raw Normal View History

2023-11-27 09:36:02 +00:00
// 生成代码, 不要在这里添加任何逻辑
package {{.PackageName}}
import (
"context"
"sync"
"fusen-basic/env"
2023-12-06 09:13:51 +00:00
2023-11-27 09:36:02 +00:00
"{{.ProjectName}}/gen/go/service"
"{{.ProjectName}}/server/config"
)
type {{.StructName}} struct {
ctx context.Context
cfg *config.Config
svcCtx *SharedContext
}
func New{{.StructName}}(ctx context.Context) *{{.StructName}} {
l := &{{.StructName}}{}
l.ctx = ctx // 初始化上下文
l.cfg = ctx.Value(env.CtxServiceConfig{}).(*config.Config) // 初始化拿到的配置结构
l.svcCtx = svcCtx
onceSharedContext.Do(func() {
svcCtx.Init(ctx, l.cfg)
})
l.svcCtx.Handler(ctx, l.cfg)
return l
}
var onceSharedContext sync.Once
var svcCtx = &SharedContext{}
type {{.StructName}}Grpc struct {
{{.UnimplementedStructName}}
}
{{range .Methods}}
type {{.MethodName}}Handler struct {
{{.MethodName}}HandlerMust
}
type {{.MethodName}}HandlerMust struct{}
{{if eq .MethodType "rpc"}}
2023-12-06 09:13:51 +00:00
func (lgrpc *{{.StructName}}Grpc) {{.MethodName}}({{range $index, $param := .Params}}{{if $index}}, {{end}}{{$param}}{{end}}) ({{.MethodReturn}}, error) {
return New{{.StructName}}(ctx).{{.MethodName}}Logic({{range $index, $param := .ParamsName}}{{if $index}}, {{end}}{{$param}}{{end}})
2023-11-27 09:36:02 +00:00
}
{{else if eq .MethodType "stream"}}
func (lgrpc *{{.StructName}}Grpc) {{.MethodName}}(stream {{range $index, $param := .Params}}{{if $index}}, {{end}}{{$param}}{{end}}) error {
return New{{.StructName}}(stream.Context()).{{.MethodName}}Logic(stream)
}
{{end}}
2023-11-27 09:36:02 +00:00
{{end}}
2023-12-06 09:13:51 +00:00