proto/goutils/proto_build/tpls/logic_grpc_struct.tpl
huangsimin@fusen.cn c5df2b6478 修改为纯grpc
2023-12-06 17:13:51 +08:00

53 lines
1.2 KiB
Smarty

// 生成代码, 不要在这里添加任何逻辑
package {{.PackageName}}
import (
"context"
"sync"
"fusen-basic/env"
"{{.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{}
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}})
}
{{end}}