Skip to main content
Version: 2.8.x(Latest)

While HTTP service controllers are generated using gf gen ctrl, microservices also have controllers, generated using gf gen pb.

$ gf gen pb

The gen pb command requires all dependencies to be properly set up. When executed successfully, it generates several Go files. We only need to focus on the controller files; the framework maintains the rest. The subsequent development process is similar to HTTP services - calling logic functions.

app/user/internal/controller/account/account.go

package account  

import (
"context"

"google.golang.org/protobuf/types/known/timestamppb"
v1 "proxima/app/user/api/account/v1"
"proxima/app/user/api/pbentity"
"proxima/app/user/internal/logic/account"
"github.com/gogf/gf/contrib/rpc/grpcx/v2"
)

type Controller struct {
v1.UnimplementedAccountServer
}

func Register(s *grpcx.GrpcServer) {
v1.RegisterAccountServer(s.Server, &Controller{})
}

func (*Controller) UserRegister(ctx context.Context, req *v1.UserRegisterReq) (res *v1.UserRegisterRes, err error) {
id, err := account.Register(ctx)
if err != nil {
return nil, err
}
return &v1.UserRegisterRes{
Id: int32(id),
}, nil
}

func (*Controller) UserLogin(ctx context.Context, req *v1.UserLoginReq) (res *v1.UserLoginRes, err error) {
token, err := account.Login(ctx)
if err != nil {
return nil, err
}
return &v1.UserLoginRes{
Token: token,
}, nil
}

func (*Controller) UserInfo(ctx context.Context, req *v1.UserInfoReq) (res *v1.UserInfoRes, err error) {
data, err := account.Info(ctx, req.Token)
if err != nil {
return nil, err
}
return &v1.UserInfoRes{
User: &pbentity.Users{
Id: uint32(data.Id),
Username: data.Username,
Password: data.Password,
Email: data.Email,
CreatedAt: timestamppb.New(data.CreatedAt.Time),
UpdatedAt: timestamppb.New(data.UpdatedAt.Time),
},
}, nil
}