修改为纯grpc
This commit is contained in:
@@ -11,15 +11,7 @@ func init() {
|
||||
{{.RequestVar}} = &service.{{.RequestStruct}}{}
|
||||
}
|
||||
|
||||
func Test{{.MethodName}}HTTP(t *testing.T) {
|
||||
// 构建请求体
|
||||
resp, err := {{.MethodName}}Http(nil)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
log.Println(resp)
|
||||
}
|
||||
|
||||
|
||||
func Test{{.MethodName}}RPC(t *testing.T) {
|
||||
resp, err := {{.MethodName}}RPC()
|
||||
if err != nil {
|
||||
|
||||
@@ -2,13 +2,12 @@ package test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"{{.ProjectName}}/gen/go/service"
|
||||
"{{.ProjectName}}/server/config"
|
||||
"fusen-basic/env"
|
||||
)
|
||||
|
||||
{{range .HttpGrpcTestStructs}}
|
||||
{{range .GrpcTestStructs}}
|
||||
|
||||
var {{.RequestVar}} *service.{{.RequestStruct}}
|
||||
|
||||
|
||||
@@ -1,16 +1,10 @@
|
||||
package {{.PackageName}}
|
||||
|
||||
import (
|
||||
"fusen-basic/basic"
|
||||
"context"
|
||||
"{{.ProjectName}}/gen/go/service"
|
||||
)
|
||||
|
||||
func (l *{{.StructName}}) {{.MethodName}}Logic(req {{.ParamReq}}, userinfo *basic.UserInfo) (resp *basic.Response[{{.MethodResponse}}]) {
|
||||
return resp.Set(basic.CodeOK)
|
||||
}
|
||||
|
||||
|
||||
// 自定义验证校验方法 VaildUserInfoHandler
|
||||
// func (h *{{.MethodName}}Handler) VaildUserInfoHandler(ctx context.Context) *basic.UserInfo {
|
||||
// return basic.ValidUserInfo(ctx)
|
||||
// }
|
||||
func (l *{{.StructName}}) {{.MethodName}}Logic({{range $index, $param := .Params}}{{if $index}}, {{end}}{{$param}}{{end}}) (resp {{.MethodReturn}},err error) {
|
||||
return resp, err
|
||||
}
|
||||
@@ -3,17 +3,12 @@ package {{.PackageName}}
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"fusen-basic/basic"
|
||||
"fusen-basic/env"
|
||||
"fusen-basic/utils/log"
|
||||
|
||||
|
||||
"{{.ProjectName}}/gen/go/service"
|
||||
"{{.ProjectName}}/server/config"
|
||||
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
|
||||
@@ -50,80 +45,9 @@ type {{.MethodName}}Handler struct {
|
||||
|
||||
type {{.MethodName}}HandlerMust struct{}
|
||||
|
||||
func (must *{{.MethodName}}HandlerMust) ValidUserInfoHandler(ctx context.Context) *basic.UserInfo {
|
||||
return defaultValidUserInfoHandler(ctx)
|
||||
}
|
||||
|
||||
var __hide_handler{{.MethodName}} *{{.MethodName}}Handler = &{{.MethodName}}Handler{}
|
||||
|
||||
func (lgrpc *{{.StructName}}Grpc) {{.MethodName}}(ctx {{.ParamCtx}}, req {{.ParamReq}}) ({{.MethodReturn}}, error) {
|
||||
return New{{.StructName}}(ctx).{{.MethodName}}Logic(req, __hide_handler{{.MethodName}}.ValidUserInfoHandler(ctx)).PassMetaResponse(ctx)
|
||||
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}}
|
||||
func DefaultValidToken(ctx context.Context) *basic.UserInfo {
|
||||
var (
|
||||
err error
|
||||
ui = &basic.UserInfo{}
|
||||
)
|
||||
// log.Println(ctx)
|
||||
if md, ok := metadata.FromIncomingContext(ctx); ok {
|
||||
var authtoken, debugtoken *string
|
||||
var vresult *service.ValidTokenResponse
|
||||
if a, ok := md["authorization"]; ok {
|
||||
token := a[0]
|
||||
if len(token) > 15 {
|
||||
if strings.HasPrefix(token[1:], "earer ") {
|
||||
token = token[7:]
|
||||
}
|
||||
authtoken = &token
|
||||
|
||||
defer func() {
|
||||
if vresult != nil {
|
||||
userinfo := vresult.UserInfo.AsMap()
|
||||
if len(userinfo) != 0 {
|
||||
ui.UserId = int64(userinfo["user_id"].(float64))
|
||||
ui.GuestId = int64(userinfo["guest_id"].(float64))
|
||||
ui.Exp = int64(userinfo["exp"].(float64))
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if a, ok := md["debug-token"]; ok {
|
||||
token := a[0]
|
||||
if len(token) > 15 {
|
||||
if strings.HasPrefix(token[1:], "earer ") {
|
||||
token = token[7:]
|
||||
}
|
||||
debugtoken = &token
|
||||
|
||||
defer func() {
|
||||
if vresult != nil {
|
||||
debug := vresult.DebugInfo.AsMap()
|
||||
if len(debug) != 0 {
|
||||
ui.Debug = &basic.Debug{}
|
||||
if dexp, ok := debug["exp"]; ok {
|
||||
dexpint64 := int64(dexp.(float64))
|
||||
ui.Debug.Exp = &dexpint64
|
||||
}
|
||||
ui.Debug.IsAllTemplateTag = int64(debug["is_all_template_tag"].(float64))
|
||||
ui.Debug.IsCache = int64(debug["is_cache"].(float64))
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
vresult, err = service.AutoAuthClient(ctx).ValidToken(context.TODO(), &service.ValidTokenRequest{
|
||||
UserToken: authtoken,
|
||||
DebugToken: debugtoken,
|
||||
})
|
||||
if err != nil {
|
||||
log.Println(err.Error())
|
||||
}
|
||||
}
|
||||
return ui
|
||||
}
|
||||
|
||||
@@ -3,16 +3,12 @@ package {{.PackageName}}
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fusen-basic/basic"
|
||||
"fusen-model/dbutils"
|
||||
"fusen-model/gmodel"
|
||||
|
||||
"{{.ProjectName}}/server/config"
|
||||
)
|
||||
|
||||
// 统一的验证入口
|
||||
var defaultValidUserInfoHandler func(context.Context) *basic.UserInfo = DefaultValidToken
|
||||
|
||||
// 处理共享数据的上下文
|
||||
type SharedContext struct {
|
||||
models *gmodel.Models
|
||||
|
||||
Reference in New Issue
Block a user