75 lines
1.8 KiB
Smarty
75 lines
1.8 KiB
Smarty
// 生成代码, 不要在这里添加任何逻辑
|
|
package {{.PackageName}}
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"fmt"
|
|
|
|
"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}}) (_resp {{.MethodReturn}},_err error) {
|
|
defer func() {
|
|
if _recoverErr := recover(); _recoverErr != nil {
|
|
_resp = nil
|
|
_err = fmt.Errorf("%v", _recoverErr)
|
|
}
|
|
}()
|
|
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}}) (_err error) {
|
|
defer func() {
|
|
if _recoverErr := recover(); _recoverErr != nil {
|
|
_err = fmt.Errorf("%v", _recoverErr)
|
|
}
|
|
}()
|
|
return New{{.StructName}}(stream.Context()).{{.MethodName}}Logic(stream)
|
|
}
|
|
|
|
{{end}}
|
|
|
|
{{end}}
|
|
|