| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- package server
- import (
- "context"
- "errors"
- "github.com/go-kit/kit/endpoint"
- transport"github.com/go-kit/kit/transport/grpc"
- "standard/etcd/go-kit/pb"
- )
- type BookServer struct {
- BookListHandler transport.Handler
- BookInfoHandler transport.Handler
- }
- //通过grpc调用GetBookInfo时,GetBookInfo只做数据透传, 调用BookServer中对应Handler.ServeGRPC转交给go-kit处理
- func (s *BookServer) GetBookInfo(ctx context.Context, in *pb.Request) (*pb.Book, error) {
- //ServeGRPC转交给go-kit处理
- _, rsp, err := s.BookInfoHandler.ServeGRPC(ctx, in)
- if err != nil {
- return nil, err
- }
- if _,ok:=rsp.(*pb.BookInfo);!ok {
- return nil,errors.New("rsp.(*book.BookInfo)断言出错")
- }
- return rsp.(*pb.Book), err //直接返回断言的结果
- }
- func (s *BookServer) GetBookList(ctx context.Context, in *pb.Request) (*pb.BookList, error) {
- _, rsp, err := s.BookListHandler.ServeGRPC(ctx, in)
- if err != nil {
- return nil, err
- }
- return rsp.(*pb.BookList), err
- }
- //endpoint
- //定义 Request、Response 格式
- //创建关于业务的构造函数
- func MakeGetBookInfoEndpoint() endpoint.Endpoint {
- return func(ctx context.Context, request interface{}) (interface{}, error) {
- //请求详情时返回 书籍信息
- req := request.(*pb.Request)
- b := new(pb.BookInfo)
- b.Book.Id= req.Par
- return b, nil
- }
- }
- func MakeGetBookListEndpoint()endpoint.Endpoint {
- return func(ctx context.Context, request interface{}) (response interface{}, err error) {
- b:=new(pb.BookList)
- b.Books=append(b.Books,&pb.Book{Id:"1",Name:"Go语言入门到精通"})
- b.Books=append(b.Books,&pb.Book{Id:"2",Name:"微服务入门到精通"})
- b.Books=append(b.Books,&pb.Book{Id:"3",Name:"区块链入门到精通"})
- return b,nil
- }
- }
- func DecodeRequest(_ context.Context, req interface{}) (interface{}, error) {
- return req, nil
- }
- func EncodeResponse(_ context.Context, rsp interface{}) (interface{}, error) {
- return rsp, nil
- }
|