hello

command
v0.0.0-...-b2d3da9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2022 License: Apache-2.0 Imports: 11 Imported by: 0

README

proto file

common.proto

syntax = "proto3";

package pb;
option go_package = "./pb";

message CommonReq {
  string id = 1;
}

hello.proto

syntax = "proto3";

package pb;
option go_package = "./pb";
import "common.proto";

message StreamReq {
  string name = 1;
  CommonReq common = 2;
}

message StreamResp {
  string greet = 1;
}

service StreamGreeter {
  rpc greet(StreamReq) returns (StreamResp);
}

ProtoSet file

// protoset.go
package pb

import _ "embed"

//go:embed common.pb
var ProtoSetCommon []byte

//go:embed hello.pb
var ProtoSetHello []byte

gateway route

参考 gateway.go

参考 middleware

engine := zeroapi.Engine(restConf, gatewatConf, protoSets)
engine.GET("/ping", s.Ping)
svr := engine.Server(options...)
svr.Use(middlewares...)
svr.Start()

rpc logic 如何获取 header 参数

// gateway.go
svr := engine.Server(zeroapi.WithHeaderProcessor(func(header http.Header) []string {
return []string{
"User-Agent:" + header.Get("User-Agent"),
"X-Forwarded-For:" + header.Get("X-Forwarded-For"),
"X-Real-IP:" + header.Get("X-Real-IP"),
"token:" + header.Get("token"),
}
}))
// logic.go
func (l *GreetLogic) Greet(in *pb.StreamReq) (*pb.StreamResp, error) {
return &pb.StreamResp{Greet: zeroapi.GetValueByKey(l.ctx, "User-Agent")}, nil
}

运行 hello.go

go run .
curl http://127.0.0.1:9999/hello/v1/greet
# 得到结果 {"code":0,"msg":"","data":{"greet":"grpc-go/1.48.0"},"server_time":1658829159831}

如何返回自定义错误

  • 增加failedReason字段
message StreamResp {
  string greet = 1;
  string failedReason = 2;
}
  • defaultResponseHandler 增加逻辑
func (h *Handler) defaultResponseHandler(in proto.Message) (code int, msg string, data interface{}) {
	if in == nil {
		return 0, "", nil
	} else {
		message, ok := in.(*dynamic.Message)
		if ok {
			if failedReason := message.GetFieldByName("failedReason"); failedReason != nil {
				if s, ok := failedReason.(string); ok && s != "" {
					return -1, s, nil
				}
			}
		}
		return 0, "", in
	}
}
  • logic 测试
func (l *GreetLogic) Greet(in *pb.StreamReq) (*pb.StreamResp, error) {
	failedReason := ""
	rand.Seed(time.Now().UnixNano())
	if rand.Intn(2) == 0 {
		failedReason = "随机失败"
	}
	return &pb.StreamResp{Greet: zeroapi.GetValueByKey(l.ctx, "User-Agent"), FailedReason: failedReason}, nil
}
go run .
curl http://127.0.0.1:9999/hello/v1/greet
# 得到结果 {"code":-1,"msg":"随机失败","server_time":1658832552397}

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
internal
svc

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL