跳到主要内容
版本:2.8.x(Latest)

HTTP服务的控制器由gf gen ctrl生成,微服务也有控制器,由gf gen pb生成。

$ gf gen pb

gen pb命令需要各类依赖都正常。如果执行成功,会生成若干go文件。我们只需要关注控制器文件即可,其他由框架维护。后续的开发过程,和HTTP服务一样——调用logic

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
}