proto/goutils/proto_build/tpls/logic_grpc_struct.tpl
2023-12-07 15:34:40 +08:00

63 lines
1.5 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{}
{{if eq .MethodType "rpc"}}
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}})
}
{{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}}
{{end}}