go-zero/example/tracing/portal/server.go

62 lines
1.3 KiB
Go
Raw Normal View History

2020-07-26 17:09:05 +08:00
package main
import (
"context"
"flag"
2020-08-08 16:40:10 +08:00
"github.com/tal-tech/go-zero/core/conf"
"github.com/tal-tech/go-zero/example/tracing/remote/portal"
"github.com/tal-tech/go-zero/example/tracing/remote/user"
"github.com/tal-tech/go-zero/rpcx"
2020-07-26 17:09:05 +08:00
"google.golang.org/grpc"
)
var configFile = flag.String("f", "etc/config.json", "the config file")
type (
Config struct {
rpcx.RpcServerConf
UserRpc rpcx.RpcClientConf
}
PortalServer struct {
userRpc rpcx.Client
2020-07-26 17:09:05 +08:00
}
)
func NewPortalServer(client rpcx.Client) *PortalServer {
2020-07-26 17:09:05 +08:00
return &PortalServer{
userRpc: client,
}
}
func (gs *PortalServer) Portal(ctx context.Context, req *portal.PortalRequest) (*portal.PortalResponse, error) {
2020-08-06 20:55:38 +08:00
conn := gs.userRpc.Conn()
2020-07-26 17:09:05 +08:00
greet := user.NewUserClient(conn)
resp, err := greet.GetGrade(ctx, &user.UserRequest{
Name: req.Name,
})
if err != nil {
return &portal.PortalResponse{
Response: err.Error(),
}, nil
} else {
return &portal.PortalResponse{
Response: resp.Response,
}, nil
}
}
func main() {
flag.Parse()
var c Config
conf.MustLoad(*configFile, &c)
client := rpcx.MustNewClient(c.UserRpc)
server := rpcx.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
portal.RegisterPortalServer(grpcServer, NewPortalServer(client))
})
server.Start()
}