initial commit
This commit is contained in:
13
user/etc/user.yaml
Normal file
13
user/etc/user.yaml
Normal file
@ -0,0 +1,13 @@
|
||||
Name: user.rpc
|
||||
ListenOn: 0.0.0.0:60100
|
||||
Mode: dev
|
||||
Etcd:
|
||||
Hosts:
|
||||
- localhost:2379
|
||||
Key: user.rpc
|
||||
|
||||
DataSource: "postgres://postgres:postgres@localhost:5432/godemo?sslmode=disable"
|
||||
|
||||
JwtAuth:
|
||||
AccessSecret: "your-secure-secret"
|
||||
AccessExpire: 900 # 可选,单位秒,15分钟
|
||||
14
user/internal/config/config.go
Normal file
14
user/internal/config/config.go
Normal file
@ -0,0 +1,14 @@
|
||||
package config
|
||||
|
||||
import "github.com/zeromicro/go-zero/zrpc"
|
||||
|
||||
type JwtAuth struct {
|
||||
AccessSecret string
|
||||
AccessExpire int64
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
zrpc.RpcServerConf
|
||||
DataSource string
|
||||
JwtAuth JwtAuth
|
||||
}
|
||||
50
user/internal/logic/getuserinfologic.go
Normal file
50
user/internal/logic/getuserinfologic.go
Normal file
@ -0,0 +1,50 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"godemo/user/internal/model"
|
||||
"godemo/user/internal/svc"
|
||||
"godemo/user/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type GetUserInfoLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewGetUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserInfoLogic {
|
||||
return &GetUserInfoLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 获取用户信息
|
||||
func (l *GetUserInfoLogic) GetUserInfo(in *user.GetUserInfoRequest) (*user.GetUserInfoResponse, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
var userModel model.User
|
||||
err := l.svcCtx.DB.Get(&userModel, "SELECT * FROM users WHERE user_id = $1", in.UserId)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, status.Error(status.Code(err), fmt.Sprintf("用户 %s 不存在", in.UserId))
|
||||
}
|
||||
return nil, status.Error(status.Code(err), err.Error())
|
||||
}
|
||||
|
||||
return &user.GetUserInfoResponse{
|
||||
UserId: userModel.UserId,
|
||||
Username: userModel.Username,
|
||||
Email: userModel.Email.String,
|
||||
CreatedAt: userModel.CreatedAt.Unix(),
|
||||
Roles: userModel.Roles,
|
||||
}, nil
|
||||
}
|
||||
69
user/internal/logic/loginlogic.go
Normal file
69
user/internal/logic/loginlogic.go
Normal file
@ -0,0 +1,69 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"godemo/user/internal/model"
|
||||
"godemo/user/internal/svc"
|
||||
"godemo/user/user"
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type LoginLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
|
||||
return &LoginLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *LoginLogic) Login(in *user.LoginRequest) (*user.LoginResponse, error) {
|
||||
// 1. 查找用户
|
||||
var u model.User
|
||||
err := l.svcCtx.DB.Get(&u, "SELECT * FROM users WHERE username = $1", in.Username)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, status.Error(codes.NotFound, "用户不存在")
|
||||
}
|
||||
return nil, status.Error(codes.Internal, err.Error())
|
||||
}
|
||||
|
||||
// 2. 校验密码(假设密码已加密存储)
|
||||
err = bcrypt.CompareHashAndPassword([]byte(u.PasswordHash), []byte(in.Password))
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Unauthenticated, "密码错误")
|
||||
}
|
||||
|
||||
// 3. 签发 JWT Token
|
||||
claims := jwt.MapClaims{
|
||||
"sub": u.UserId,
|
||||
"exp": time.Now().Add(time.Minute * 15).Unix(), // 默认 15 分钟有效期
|
||||
"iat": time.Now().Unix(),
|
||||
"roles": u.Roles,
|
||||
}
|
||||
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
signedToken, err := token.SignedString([]byte(l.svcCtx.Config.JwtAuth.AccessSecret))
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Unavailable, "token生成失败")
|
||||
}
|
||||
|
||||
// 4. 返回响应
|
||||
return &user.LoginResponse{
|
||||
Token: signedToken,
|
||||
ExpiresAt: claims["exp"].(int64),
|
||||
}, nil
|
||||
}
|
||||
31
user/internal/logic/logoutlogic.go
Normal file
31
user/internal/logic/logoutlogic.go
Normal file
@ -0,0 +1,31 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"godemo/user/internal/svc"
|
||||
"godemo/user/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type LogoutLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewLogoutLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LogoutLogic {
|
||||
return &LogoutLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 用户登出,可选实现
|
||||
func (l *LogoutLogic) Logout(in *user.LogoutRequest) (*user.LogoutResponse, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return &user.LogoutResponse{}, nil
|
||||
}
|
||||
31
user/internal/logic/pinglogic.go
Normal file
31
user/internal/logic/pinglogic.go
Normal file
@ -0,0 +1,31 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"godemo/user/internal/svc"
|
||||
"godemo/user/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type PingLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewPingLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PingLogic {
|
||||
return &PingLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 健康检查
|
||||
func (l *PingLogic) Ping(in *user.PingRequest) (*user.PingResponse, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return &user.PingResponse{}, nil
|
||||
}
|
||||
90
user/internal/logic/registerlogic.go
Normal file
90
user/internal/logic/registerlogic.go
Normal file
@ -0,0 +1,90 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"godemo/user/internal/model"
|
||||
"godemo/user/internal/svc"
|
||||
"godemo/user/user"
|
||||
"time"
|
||||
|
||||
"github.com/lib/pq"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type RegisterLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
|
||||
return &RegisterLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 用户注册
|
||||
func (l *RegisterLogic) Register(in *user.RegisterRequest) (*user.RegisterResponse, error) {
|
||||
// 1. 检查用户名是否已存在
|
||||
var existingUser model.User
|
||||
err := l.svcCtx.DB.Get(&existingUser, "SELECT * FROM users WHERE username = $1", in.Username)
|
||||
if err == nil {
|
||||
// 用户名已存在
|
||||
return nil, status.Error(codes.AlreadyExists, "用户名已存在")
|
||||
}
|
||||
|
||||
// 2. 加密密码
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(in.Password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
// 密码加密失败
|
||||
return nil, status.Error(codes.Internal, err.Error())
|
||||
}
|
||||
|
||||
// 3. 转换 email 为 sql.NullString
|
||||
var email sql.NullString
|
||||
if in.Email != "" {
|
||||
email = sql.NullString{
|
||||
String: in.Email,
|
||||
Valid: true,
|
||||
}
|
||||
} else {
|
||||
email = sql.NullString{Valid: false}
|
||||
}
|
||||
|
||||
// 4. 保存新用户到数据库
|
||||
userModel := model.User{
|
||||
Username: in.Username,
|
||||
PasswordHash: string(hashedPassword),
|
||||
Email: email, // 使用 sql.NullString 类型存储 email
|
||||
Roles: []string{"user"}, // 默认角色为 "user"
|
||||
CreatedAt: time.Now(), // 当前时间戳
|
||||
}
|
||||
|
||||
// 执行插入数据库操作
|
||||
_, err = l.svcCtx.DB.Exec(
|
||||
"INSERT INTO users (username, password_hash, email, roles, created_at) VALUES ($1, $2, $3, $4, $5)",
|
||||
userModel.Username,
|
||||
userModel.PasswordHash,
|
||||
userModel.Email,
|
||||
pq.Array(userModel.Roles), // pq.Array 用于处理 PostgreSQL 数组类型
|
||||
userModel.CreatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
// 数据库插入失败
|
||||
return nil, status.Error(codes.Internal, err.Error())
|
||||
}
|
||||
|
||||
// 获取插入的记录的 UserId
|
||||
l.svcCtx.DB.Get(&existingUser, "SELECT * FROM users WHERE username = $1", in.Username)
|
||||
|
||||
// 5. 返回成功响应
|
||||
return &user.RegisterResponse{
|
||||
UserId: existingUser.UserId, // 将 UserId 转换为字符串
|
||||
}, nil
|
||||
}
|
||||
17
user/internal/model/user.go
Normal file
17
user/internal/model/user.go
Normal file
@ -0,0 +1,17 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
"github.com/lib/pq"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
UserId string `db:"user_id"`
|
||||
Username string `db:"username"`
|
||||
Email sql.NullString `db:"email"`
|
||||
PasswordHash string `db:"password_hash"`
|
||||
Roles pq.StringArray `db:"roles"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
}
|
||||
54
user/internal/server/userserver.go
Normal file
54
user/internal/server/userserver.go
Normal file
@ -0,0 +1,54 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.8.3
|
||||
// Source: user.proto
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"godemo/user/internal/logic"
|
||||
"godemo/user/internal/svc"
|
||||
"godemo/user/user"
|
||||
)
|
||||
|
||||
type UserServer struct {
|
||||
svcCtx *svc.ServiceContext
|
||||
user.UnimplementedUserServer
|
||||
}
|
||||
|
||||
func NewUserServer(svcCtx *svc.ServiceContext) *UserServer {
|
||||
return &UserServer{
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 健康检查
|
||||
func (s *UserServer) Ping(ctx context.Context, in *user.PingRequest) (*user.PingResponse, error) {
|
||||
l := logic.NewPingLogic(ctx, s.svcCtx)
|
||||
return l.Ping(in)
|
||||
}
|
||||
|
||||
// 用户注册
|
||||
func (s *UserServer) Register(ctx context.Context, in *user.RegisterRequest) (*user.RegisterResponse, error) {
|
||||
l := logic.NewRegisterLogic(ctx, s.svcCtx)
|
||||
return l.Register(in)
|
||||
}
|
||||
|
||||
// 用户登录,返回 JWT Token
|
||||
func (s *UserServer) Login(ctx context.Context, in *user.LoginRequest) (*user.LoginResponse, error) {
|
||||
l := logic.NewLoginLogic(ctx, s.svcCtx)
|
||||
return l.Login(in)
|
||||
}
|
||||
|
||||
// 用户登出,可选实现
|
||||
func (s *UserServer) Logout(ctx context.Context, in *user.LogoutRequest) (*user.LogoutResponse, error) {
|
||||
l := logic.NewLogoutLogic(ctx, s.svcCtx)
|
||||
return l.Logout(in)
|
||||
}
|
||||
|
||||
// 获取用户信息
|
||||
func (s *UserServer) GetUserInfo(ctx context.Context, in *user.GetUserInfoRequest) (*user.GetUserInfoResponse, error) {
|
||||
l := logic.NewGetUserInfoLogic(ctx, s.svcCtx)
|
||||
return l.GetUserInfo(in)
|
||||
}
|
||||
23
user/internal/svc/servicecontext.go
Normal file
23
user/internal/svc/servicecontext.go
Normal file
@ -0,0 +1,23 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"godemo/user/internal/config"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
DB *sqlx.DB
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
db := sqlx.MustConnect("postgres", c.DataSource)
|
||||
db.SetMaxOpenConns(10)
|
||||
db.SetMaxIdleConns(5)
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
DB: db,
|
||||
}
|
||||
}
|
||||
39
user/user.go
Normal file
39
user/user.go
Normal file
@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
|
||||
"godemo/user/internal/config"
|
||||
"godemo/user/internal/server"
|
||||
"godemo/user/internal/svc"
|
||||
"godemo/user/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
"github.com/zeromicro/go-zero/core/service"
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/reflection"
|
||||
)
|
||||
|
||||
var configFile = flag.String("f", "etc/user.yaml", "the config file")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
var c config.Config
|
||||
conf.MustLoad(*configFile, &c)
|
||||
ctx := svc.NewServiceContext(c)
|
||||
|
||||
s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
|
||||
user.RegisterUserServer(grpcServer, server.NewUserServer(ctx))
|
||||
|
||||
if c.Mode == service.DevMode || c.Mode == service.TestMode {
|
||||
reflection.Register(grpcServer)
|
||||
}
|
||||
})
|
||||
defer s.Stop()
|
||||
|
||||
fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
|
||||
s.Start()
|
||||
}
|
||||
645
user/user/user.pb.go
Normal file
645
user/user/user.pb.go
Normal file
@ -0,0 +1,645 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.36.6
|
||||
// protoc v3.20.3
|
||||
// source: rpc/user.proto
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||
reflect "reflect"
|
||||
sync "sync"
|
||||
unsafe "unsafe"
|
||||
)
|
||||
|
||||
const (
|
||||
// Verify that this generated code is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
|
||||
// Verify that runtime/protoimpl is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
|
||||
)
|
||||
|
||||
// 健康检查请求
|
||||
type PingRequest struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Ping string `protobuf:"bytes,1,opt,name=ping,proto3" json:"ping,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *PingRequest) Reset() {
|
||||
*x = PingRequest{}
|
||||
mi := &file_rpc_user_proto_msgTypes[0]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *PingRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*PingRequest) ProtoMessage() {}
|
||||
|
||||
func (x *PingRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_rpc_user_proto_msgTypes[0]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use PingRequest.ProtoReflect.Descriptor instead.
|
||||
func (*PingRequest) Descriptor() ([]byte, []int) {
|
||||
return file_rpc_user_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
func (x *PingRequest) GetPing() string {
|
||||
if x != nil {
|
||||
return x.Ping
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// 健康检查响应
|
||||
type PingResponse struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Pong string `protobuf:"bytes,1,opt,name=pong,proto3" json:"pong,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *PingResponse) Reset() {
|
||||
*x = PingResponse{}
|
||||
mi := &file_rpc_user_proto_msgTypes[1]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *PingResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*PingResponse) ProtoMessage() {}
|
||||
|
||||
func (x *PingResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_rpc_user_proto_msgTypes[1]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use PingResponse.ProtoReflect.Descriptor instead.
|
||||
func (*PingResponse) Descriptor() ([]byte, []int) {
|
||||
return file_rpc_user_proto_rawDescGZIP(), []int{1}
|
||||
}
|
||||
|
||||
func (x *PingResponse) GetPong() string {
|
||||
if x != nil {
|
||||
return x.Pong
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// 注册请求
|
||||
type RegisterRequest struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Username string `protobuf:"bytes,1,opt,name=username,proto3" json:"username,omitempty"` // 用户名
|
||||
Password string `protobuf:"bytes,2,opt,name=password,proto3" json:"password,omitempty"` // 密码(明文或哈希后)
|
||||
Email string `protobuf:"bytes,3,opt,name=email,proto3" json:"email,omitempty"` // 邮箱,可选
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *RegisterRequest) Reset() {
|
||||
*x = RegisterRequest{}
|
||||
mi := &file_rpc_user_proto_msgTypes[2]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *RegisterRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*RegisterRequest) ProtoMessage() {}
|
||||
|
||||
func (x *RegisterRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_rpc_user_proto_msgTypes[2]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use RegisterRequest.ProtoReflect.Descriptor instead.
|
||||
func (*RegisterRequest) Descriptor() ([]byte, []int) {
|
||||
return file_rpc_user_proto_rawDescGZIP(), []int{2}
|
||||
}
|
||||
|
||||
func (x *RegisterRequest) GetUsername() string {
|
||||
if x != nil {
|
||||
return x.Username
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *RegisterRequest) GetPassword() string {
|
||||
if x != nil {
|
||||
return x.Password
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *RegisterRequest) GetEmail() string {
|
||||
if x != nil {
|
||||
return x.Email
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// 注册响应
|
||||
type RegisterResponse struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
UserId string `protobuf:"bytes,1,opt,name=user_id,json=userId,proto3" json:"user_id,omitempty"` // 新注册用户的唯一 ID
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *RegisterResponse) Reset() {
|
||||
*x = RegisterResponse{}
|
||||
mi := &file_rpc_user_proto_msgTypes[3]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *RegisterResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*RegisterResponse) ProtoMessage() {}
|
||||
|
||||
func (x *RegisterResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_rpc_user_proto_msgTypes[3]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use RegisterResponse.ProtoReflect.Descriptor instead.
|
||||
func (*RegisterResponse) Descriptor() ([]byte, []int) {
|
||||
return file_rpc_user_proto_rawDescGZIP(), []int{3}
|
||||
}
|
||||
|
||||
func (x *RegisterResponse) GetUserId() string {
|
||||
if x != nil {
|
||||
return x.UserId
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// 登录请求
|
||||
type LoginRequest struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Username string `protobuf:"bytes,1,opt,name=username,proto3" json:"username,omitempty"` // 用户名
|
||||
Password string `protobuf:"bytes,2,opt,name=password,proto3" json:"password,omitempty"` // 密码
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *LoginRequest) Reset() {
|
||||
*x = LoginRequest{}
|
||||
mi := &file_rpc_user_proto_msgTypes[4]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *LoginRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*LoginRequest) ProtoMessage() {}
|
||||
|
||||
func (x *LoginRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_rpc_user_proto_msgTypes[4]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use LoginRequest.ProtoReflect.Descriptor instead.
|
||||
func (*LoginRequest) Descriptor() ([]byte, []int) {
|
||||
return file_rpc_user_proto_rawDescGZIP(), []int{4}
|
||||
}
|
||||
|
||||
func (x *LoginRequest) GetUsername() string {
|
||||
if x != nil {
|
||||
return x.Username
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *LoginRequest) GetPassword() string {
|
||||
if x != nil {
|
||||
return x.Password
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// 登录响应
|
||||
type LoginResponse struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Token string `protobuf:"bytes,1,opt,name=token,proto3" json:"token,omitempty"` // JWT 访问令牌
|
||||
ExpiresAt int64 `protobuf:"varint,2,opt,name=expires_at,json=expiresAt,proto3" json:"expires_at,omitempty"` // 过期时间(Unix 时间戳)
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *LoginResponse) Reset() {
|
||||
*x = LoginResponse{}
|
||||
mi := &file_rpc_user_proto_msgTypes[5]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *LoginResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*LoginResponse) ProtoMessage() {}
|
||||
|
||||
func (x *LoginResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_rpc_user_proto_msgTypes[5]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use LoginResponse.ProtoReflect.Descriptor instead.
|
||||
func (*LoginResponse) Descriptor() ([]byte, []int) {
|
||||
return file_rpc_user_proto_rawDescGZIP(), []int{5}
|
||||
}
|
||||
|
||||
func (x *LoginResponse) GetToken() string {
|
||||
if x != nil {
|
||||
return x.Token
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *LoginResponse) GetExpiresAt() int64 {
|
||||
if x != nil {
|
||||
return x.ExpiresAt
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// 登出请求
|
||||
type LogoutRequest struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Token string `protobuf:"bytes,1,opt,name=token,proto3" json:"token,omitempty"` // 要废弃的 JWT
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *LogoutRequest) Reset() {
|
||||
*x = LogoutRequest{}
|
||||
mi := &file_rpc_user_proto_msgTypes[6]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *LogoutRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*LogoutRequest) ProtoMessage() {}
|
||||
|
||||
func (x *LogoutRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_rpc_user_proto_msgTypes[6]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use LogoutRequest.ProtoReflect.Descriptor instead.
|
||||
func (*LogoutRequest) Descriptor() ([]byte, []int) {
|
||||
return file_rpc_user_proto_rawDescGZIP(), []int{6}
|
||||
}
|
||||
|
||||
func (x *LogoutRequest) GetToken() string {
|
||||
if x != nil {
|
||||
return x.Token
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// 登出响应
|
||||
type LogoutResponse struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Success bool `protobuf:"varint,1,opt,name=success,proto3" json:"success,omitempty"` // 是否登出成功
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *LogoutResponse) Reset() {
|
||||
*x = LogoutResponse{}
|
||||
mi := &file_rpc_user_proto_msgTypes[7]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *LogoutResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*LogoutResponse) ProtoMessage() {}
|
||||
|
||||
func (x *LogoutResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_rpc_user_proto_msgTypes[7]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use LogoutResponse.ProtoReflect.Descriptor instead.
|
||||
func (*LogoutResponse) Descriptor() ([]byte, []int) {
|
||||
return file_rpc_user_proto_rawDescGZIP(), []int{7}
|
||||
}
|
||||
|
||||
func (x *LogoutResponse) GetSuccess() bool {
|
||||
if x != nil {
|
||||
return x.Success
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// 获取用户信息请求
|
||||
type GetUserInfoRequest struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
UserId string `protobuf:"bytes,1,opt,name=user_id,json=userId,proto3" json:"user_id,omitempty"` // 目标用户 ID
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *GetUserInfoRequest) Reset() {
|
||||
*x = GetUserInfoRequest{}
|
||||
mi := &file_rpc_user_proto_msgTypes[8]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *GetUserInfoRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*GetUserInfoRequest) ProtoMessage() {}
|
||||
|
||||
func (x *GetUserInfoRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_rpc_user_proto_msgTypes[8]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use GetUserInfoRequest.ProtoReflect.Descriptor instead.
|
||||
func (*GetUserInfoRequest) Descriptor() ([]byte, []int) {
|
||||
return file_rpc_user_proto_rawDescGZIP(), []int{8}
|
||||
}
|
||||
|
||||
func (x *GetUserInfoRequest) GetUserId() string {
|
||||
if x != nil {
|
||||
return x.UserId
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// 获取用户信息响应
|
||||
type GetUserInfoResponse struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
UserId string `protobuf:"bytes,1,opt,name=user_id,json=userId,proto3" json:"user_id,omitempty"` // 用户唯一 ID
|
||||
Username string `protobuf:"bytes,2,opt,name=username,proto3" json:"username,omitempty"` // 用户名
|
||||
Email string `protobuf:"bytes,3,opt,name=email,proto3" json:"email,omitempty"` // 用户邮箱
|
||||
CreatedAt int64 `protobuf:"varint,4,opt,name=created_at,json=createdAt,proto3" json:"created_at,omitempty"` // 账号创建时间(Unix 时间戳)
|
||||
Roles []string `protobuf:"bytes,5,rep,name=roles,proto3" json:"roles,omitempty"` // 用户角色列表,可选
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *GetUserInfoResponse) Reset() {
|
||||
*x = GetUserInfoResponse{}
|
||||
mi := &file_rpc_user_proto_msgTypes[9]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *GetUserInfoResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*GetUserInfoResponse) ProtoMessage() {}
|
||||
|
||||
func (x *GetUserInfoResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_rpc_user_proto_msgTypes[9]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use GetUserInfoResponse.ProtoReflect.Descriptor instead.
|
||||
func (*GetUserInfoResponse) Descriptor() ([]byte, []int) {
|
||||
return file_rpc_user_proto_rawDescGZIP(), []int{9}
|
||||
}
|
||||
|
||||
func (x *GetUserInfoResponse) GetUserId() string {
|
||||
if x != nil {
|
||||
return x.UserId
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *GetUserInfoResponse) GetUsername() string {
|
||||
if x != nil {
|
||||
return x.Username
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *GetUserInfoResponse) GetEmail() string {
|
||||
if x != nil {
|
||||
return x.Email
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *GetUserInfoResponse) GetCreatedAt() int64 {
|
||||
if x != nil {
|
||||
return x.CreatedAt
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *GetUserInfoResponse) GetRoles() []string {
|
||||
if x != nil {
|
||||
return x.Roles
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var File_rpc_user_proto protoreflect.FileDescriptor
|
||||
|
||||
const file_rpc_user_proto_rawDesc = "" +
|
||||
"\n" +
|
||||
"\x0erpc/user.proto\x12\x04user\"!\n" +
|
||||
"\vPingRequest\x12\x12\n" +
|
||||
"\x04ping\x18\x01 \x01(\tR\x04ping\"\"\n" +
|
||||
"\fPingResponse\x12\x12\n" +
|
||||
"\x04pong\x18\x01 \x01(\tR\x04pong\"_\n" +
|
||||
"\x0fRegisterRequest\x12\x1a\n" +
|
||||
"\busername\x18\x01 \x01(\tR\busername\x12\x1a\n" +
|
||||
"\bpassword\x18\x02 \x01(\tR\bpassword\x12\x14\n" +
|
||||
"\x05email\x18\x03 \x01(\tR\x05email\"+\n" +
|
||||
"\x10RegisterResponse\x12\x17\n" +
|
||||
"\auser_id\x18\x01 \x01(\tR\x06userId\"F\n" +
|
||||
"\fLoginRequest\x12\x1a\n" +
|
||||
"\busername\x18\x01 \x01(\tR\busername\x12\x1a\n" +
|
||||
"\bpassword\x18\x02 \x01(\tR\bpassword\"D\n" +
|
||||
"\rLoginResponse\x12\x14\n" +
|
||||
"\x05token\x18\x01 \x01(\tR\x05token\x12\x1d\n" +
|
||||
"\n" +
|
||||
"expires_at\x18\x02 \x01(\x03R\texpiresAt\"%\n" +
|
||||
"\rLogoutRequest\x12\x14\n" +
|
||||
"\x05token\x18\x01 \x01(\tR\x05token\"*\n" +
|
||||
"\x0eLogoutResponse\x12\x18\n" +
|
||||
"\asuccess\x18\x01 \x01(\bR\asuccess\"-\n" +
|
||||
"\x12GetUserInfoRequest\x12\x17\n" +
|
||||
"\auser_id\x18\x01 \x01(\tR\x06userId\"\x95\x01\n" +
|
||||
"\x13GetUserInfoResponse\x12\x17\n" +
|
||||
"\auser_id\x18\x01 \x01(\tR\x06userId\x12\x1a\n" +
|
||||
"\busername\x18\x02 \x01(\tR\busername\x12\x14\n" +
|
||||
"\x05email\x18\x03 \x01(\tR\x05email\x12\x1d\n" +
|
||||
"\n" +
|
||||
"created_at\x18\x04 \x01(\x03R\tcreatedAt\x12\x14\n" +
|
||||
"\x05roles\x18\x05 \x03(\tR\x05roles2\x9b\x02\n" +
|
||||
"\x04User\x12-\n" +
|
||||
"\x04Ping\x12\x11.user.PingRequest\x1a\x12.user.PingResponse\x129\n" +
|
||||
"\bRegister\x12\x15.user.RegisterRequest\x1a\x16.user.RegisterResponse\x120\n" +
|
||||
"\x05Login\x12\x12.user.LoginRequest\x1a\x13.user.LoginResponse\x123\n" +
|
||||
"\x06Logout\x12\x13.user.LogoutRequest\x1a\x14.user.LogoutResponse\x12B\n" +
|
||||
"\vGetUserInfo\x12\x18.user.GetUserInfoRequest\x1a\x19.user.GetUserInfoResponseB\bZ\x06./userb\x06proto3"
|
||||
|
||||
var (
|
||||
file_rpc_user_proto_rawDescOnce sync.Once
|
||||
file_rpc_user_proto_rawDescData []byte
|
||||
)
|
||||
|
||||
func file_rpc_user_proto_rawDescGZIP() []byte {
|
||||
file_rpc_user_proto_rawDescOnce.Do(func() {
|
||||
file_rpc_user_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_rpc_user_proto_rawDesc), len(file_rpc_user_proto_rawDesc)))
|
||||
})
|
||||
return file_rpc_user_proto_rawDescData
|
||||
}
|
||||
|
||||
var file_rpc_user_proto_msgTypes = make([]protoimpl.MessageInfo, 10)
|
||||
var file_rpc_user_proto_goTypes = []any{
|
||||
(*PingRequest)(nil), // 0: user.PingRequest
|
||||
(*PingResponse)(nil), // 1: user.PingResponse
|
||||
(*RegisterRequest)(nil), // 2: user.RegisterRequest
|
||||
(*RegisterResponse)(nil), // 3: user.RegisterResponse
|
||||
(*LoginRequest)(nil), // 4: user.LoginRequest
|
||||
(*LoginResponse)(nil), // 5: user.LoginResponse
|
||||
(*LogoutRequest)(nil), // 6: user.LogoutRequest
|
||||
(*LogoutResponse)(nil), // 7: user.LogoutResponse
|
||||
(*GetUserInfoRequest)(nil), // 8: user.GetUserInfoRequest
|
||||
(*GetUserInfoResponse)(nil), // 9: user.GetUserInfoResponse
|
||||
}
|
||||
var file_rpc_user_proto_depIdxs = []int32{
|
||||
0, // 0: user.User.Ping:input_type -> user.PingRequest
|
||||
2, // 1: user.User.Register:input_type -> user.RegisterRequest
|
||||
4, // 2: user.User.Login:input_type -> user.LoginRequest
|
||||
6, // 3: user.User.Logout:input_type -> user.LogoutRequest
|
||||
8, // 4: user.User.GetUserInfo:input_type -> user.GetUserInfoRequest
|
||||
1, // 5: user.User.Ping:output_type -> user.PingResponse
|
||||
3, // 6: user.User.Register:output_type -> user.RegisterResponse
|
||||
5, // 7: user.User.Login:output_type -> user.LoginResponse
|
||||
7, // 8: user.User.Logout:output_type -> user.LogoutResponse
|
||||
9, // 9: user.User.GetUserInfo:output_type -> user.GetUserInfoResponse
|
||||
5, // [5:10] is the sub-list for method output_type
|
||||
0, // [0:5] is the sub-list for method input_type
|
||||
0, // [0:0] is the sub-list for extension type_name
|
||||
0, // [0:0] is the sub-list for extension extendee
|
||||
0, // [0:0] is the sub-list for field type_name
|
||||
}
|
||||
|
||||
func init() { file_rpc_user_proto_init() }
|
||||
func file_rpc_user_proto_init() {
|
||||
if File_rpc_user_proto != nil {
|
||||
return
|
||||
}
|
||||
type x struct{}
|
||||
out := protoimpl.TypeBuilder{
|
||||
File: protoimpl.DescBuilder{
|
||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||
RawDescriptor: unsafe.Slice(unsafe.StringData(file_rpc_user_proto_rawDesc), len(file_rpc_user_proto_rawDesc)),
|
||||
NumEnums: 0,
|
||||
NumMessages: 10,
|
||||
NumExtensions: 0,
|
||||
NumServices: 1,
|
||||
},
|
||||
GoTypes: file_rpc_user_proto_goTypes,
|
||||
DependencyIndexes: file_rpc_user_proto_depIdxs,
|
||||
MessageInfos: file_rpc_user_proto_msgTypes,
|
||||
}.Build()
|
||||
File_rpc_user_proto = out.File
|
||||
file_rpc_user_proto_goTypes = nil
|
||||
file_rpc_user_proto_depIdxs = nil
|
||||
}
|
||||
287
user/user/user_grpc.pb.go
Normal file
287
user/user/user_grpc.pb.go
Normal file
@ -0,0 +1,287 @@
|
||||
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
||||
// versions:
|
||||
// - protoc-gen-go-grpc v1.5.1
|
||||
// - protoc v3.20.3
|
||||
// source: rpc/user.proto
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
context "context"
|
||||
grpc "google.golang.org/grpc"
|
||||
codes "google.golang.org/grpc/codes"
|
||||
status "google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the grpc package it is being compiled against.
|
||||
// Requires gRPC-Go v1.64.0 or later.
|
||||
const _ = grpc.SupportPackageIsVersion9
|
||||
|
||||
const (
|
||||
User_Ping_FullMethodName = "/user.User/Ping"
|
||||
User_Register_FullMethodName = "/user.User/Register"
|
||||
User_Login_FullMethodName = "/user.User/Login"
|
||||
User_Logout_FullMethodName = "/user.User/Logout"
|
||||
User_GetUserInfo_FullMethodName = "/user.User/GetUserInfo"
|
||||
)
|
||||
|
||||
// UserClient is the client API for User service.
|
||||
//
|
||||
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
|
||||
//
|
||||
// 用户服务 - 注册、登录、登出、获取用户信息、健康检查
|
||||
type UserClient interface {
|
||||
// 健康检查
|
||||
Ping(ctx context.Context, in *PingRequest, opts ...grpc.CallOption) (*PingResponse, error)
|
||||
// 用户注册
|
||||
Register(ctx context.Context, in *RegisterRequest, opts ...grpc.CallOption) (*RegisterResponse, error)
|
||||
// 用户登录,返回 JWT Token
|
||||
Login(ctx context.Context, in *LoginRequest, opts ...grpc.CallOption) (*LoginResponse, error)
|
||||
// 用户登出,可选实现
|
||||
Logout(ctx context.Context, in *LogoutRequest, opts ...grpc.CallOption) (*LogoutResponse, error)
|
||||
// 获取用户信息
|
||||
GetUserInfo(ctx context.Context, in *GetUserInfoRequest, opts ...grpc.CallOption) (*GetUserInfoResponse, error)
|
||||
}
|
||||
|
||||
type userClient struct {
|
||||
cc grpc.ClientConnInterface
|
||||
}
|
||||
|
||||
func NewUserClient(cc grpc.ClientConnInterface) UserClient {
|
||||
return &userClient{cc}
|
||||
}
|
||||
|
||||
func (c *userClient) Ping(ctx context.Context, in *PingRequest, opts ...grpc.CallOption) (*PingResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(PingResponse)
|
||||
err := c.cc.Invoke(ctx, User_Ping_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *userClient) Register(ctx context.Context, in *RegisterRequest, opts ...grpc.CallOption) (*RegisterResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(RegisterResponse)
|
||||
err := c.cc.Invoke(ctx, User_Register_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *userClient) Login(ctx context.Context, in *LoginRequest, opts ...grpc.CallOption) (*LoginResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(LoginResponse)
|
||||
err := c.cc.Invoke(ctx, User_Login_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *userClient) Logout(ctx context.Context, in *LogoutRequest, opts ...grpc.CallOption) (*LogoutResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(LogoutResponse)
|
||||
err := c.cc.Invoke(ctx, User_Logout_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *userClient) GetUserInfo(ctx context.Context, in *GetUserInfoRequest, opts ...grpc.CallOption) (*GetUserInfoResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(GetUserInfoResponse)
|
||||
err := c.cc.Invoke(ctx, User_GetUserInfo_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// UserServer is the server API for User service.
|
||||
// All implementations must embed UnimplementedUserServer
|
||||
// for forward compatibility.
|
||||
//
|
||||
// 用户服务 - 注册、登录、登出、获取用户信息、健康检查
|
||||
type UserServer interface {
|
||||
// 健康检查
|
||||
Ping(context.Context, *PingRequest) (*PingResponse, error)
|
||||
// 用户注册
|
||||
Register(context.Context, *RegisterRequest) (*RegisterResponse, error)
|
||||
// 用户登录,返回 JWT Token
|
||||
Login(context.Context, *LoginRequest) (*LoginResponse, error)
|
||||
// 用户登出,可选实现
|
||||
Logout(context.Context, *LogoutRequest) (*LogoutResponse, error)
|
||||
// 获取用户信息
|
||||
GetUserInfo(context.Context, *GetUserInfoRequest) (*GetUserInfoResponse, error)
|
||||
mustEmbedUnimplementedUserServer()
|
||||
}
|
||||
|
||||
// UnimplementedUserServer must be embedded to have
|
||||
// forward compatible implementations.
|
||||
//
|
||||
// NOTE: this should be embedded by value instead of pointer to avoid a nil
|
||||
// pointer dereference when methods are called.
|
||||
type UnimplementedUserServer struct{}
|
||||
|
||||
func (UnimplementedUserServer) Ping(context.Context, *PingRequest) (*PingResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method Ping not implemented")
|
||||
}
|
||||
func (UnimplementedUserServer) Register(context.Context, *RegisterRequest) (*RegisterResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method Register not implemented")
|
||||
}
|
||||
func (UnimplementedUserServer) Login(context.Context, *LoginRequest) (*LoginResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method Login not implemented")
|
||||
}
|
||||
func (UnimplementedUserServer) Logout(context.Context, *LogoutRequest) (*LogoutResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method Logout not implemented")
|
||||
}
|
||||
func (UnimplementedUserServer) GetUserInfo(context.Context, *GetUserInfoRequest) (*GetUserInfoResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method GetUserInfo not implemented")
|
||||
}
|
||||
func (UnimplementedUserServer) mustEmbedUnimplementedUserServer() {}
|
||||
func (UnimplementedUserServer) testEmbeddedByValue() {}
|
||||
|
||||
// UnsafeUserServer may be embedded to opt out of forward compatibility for this service.
|
||||
// Use of this interface is not recommended, as added methods to UserServer will
|
||||
// result in compilation errors.
|
||||
type UnsafeUserServer interface {
|
||||
mustEmbedUnimplementedUserServer()
|
||||
}
|
||||
|
||||
func RegisterUserServer(s grpc.ServiceRegistrar, srv UserServer) {
|
||||
// If the following call pancis, it indicates UnimplementedUserServer was
|
||||
// embedded by pointer and is nil. This will cause panics if an
|
||||
// unimplemented method is ever invoked, so we test this at initialization
|
||||
// time to prevent it from happening at runtime later due to I/O.
|
||||
if t, ok := srv.(interface{ testEmbeddedByValue() }); ok {
|
||||
t.testEmbeddedByValue()
|
||||
}
|
||||
s.RegisterService(&User_ServiceDesc, srv)
|
||||
}
|
||||
|
||||
func _User_Ping_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(PingRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(UserServer).Ping(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: User_Ping_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(UserServer).Ping(ctx, req.(*PingRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _User_Register_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(RegisterRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(UserServer).Register(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: User_Register_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(UserServer).Register(ctx, req.(*RegisterRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _User_Login_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(LoginRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(UserServer).Login(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: User_Login_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(UserServer).Login(ctx, req.(*LoginRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _User_Logout_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(LogoutRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(UserServer).Logout(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: User_Logout_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(UserServer).Logout(ctx, req.(*LogoutRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _User_GetUserInfo_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(GetUserInfoRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(UserServer).GetUserInfo(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: User_GetUserInfo_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(UserServer).GetUserInfo(ctx, req.(*GetUserInfoRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
// User_ServiceDesc is the grpc.ServiceDesc for User service.
|
||||
// It's only intended for direct use with grpc.RegisterService,
|
||||
// and not to be introspected or modified (even as a copy)
|
||||
var User_ServiceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "user.User",
|
||||
HandlerType: (*UserServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "Ping",
|
||||
Handler: _User_Ping_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "Register",
|
||||
Handler: _User_Register_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "Login",
|
||||
Handler: _User_Login_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "Logout",
|
||||
Handler: _User_Logout_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "GetUserInfo",
|
||||
Handler: _User_GetUserInfo_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "rpc/user.proto",
|
||||
}
|
||||
80
user/userclient/user.go
Normal file
80
user/userclient/user.go
Normal file
@ -0,0 +1,80 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.8.3
|
||||
// Source: user.proto
|
||||
|
||||
package userclient
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"godemo/user/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type (
|
||||
GetUserInfoRequest = user.GetUserInfoRequest
|
||||
GetUserInfoResponse = user.GetUserInfoResponse
|
||||
LoginRequest = user.LoginRequest
|
||||
LoginResponse = user.LoginResponse
|
||||
LogoutRequest = user.LogoutRequest
|
||||
LogoutResponse = user.LogoutResponse
|
||||
PingRequest = user.PingRequest
|
||||
PingResponse = user.PingResponse
|
||||
RegisterRequest = user.RegisterRequest
|
||||
RegisterResponse = user.RegisterResponse
|
||||
|
||||
User interface {
|
||||
// 健康检查
|
||||
Ping(ctx context.Context, in *PingRequest, opts ...grpc.CallOption) (*PingResponse, error)
|
||||
// 用户注册
|
||||
Register(ctx context.Context, in *RegisterRequest, opts ...grpc.CallOption) (*RegisterResponse, error)
|
||||
// 用户登录,返回 JWT Token
|
||||
Login(ctx context.Context, in *LoginRequest, opts ...grpc.CallOption) (*LoginResponse, error)
|
||||
// 用户登出,可选实现
|
||||
Logout(ctx context.Context, in *LogoutRequest, opts ...grpc.CallOption) (*LogoutResponse, error)
|
||||
// 获取用户信息
|
||||
GetUserInfo(ctx context.Context, in *GetUserInfoRequest, opts ...grpc.CallOption) (*GetUserInfoResponse, error)
|
||||
}
|
||||
|
||||
defaultUser struct {
|
||||
cli zrpc.Client
|
||||
}
|
||||
)
|
||||
|
||||
func NewUser(cli zrpc.Client) User {
|
||||
return &defaultUser{
|
||||
cli: cli,
|
||||
}
|
||||
}
|
||||
|
||||
// 健康检查
|
||||
func (m *defaultUser) Ping(ctx context.Context, in *PingRequest, opts ...grpc.CallOption) (*PingResponse, error) {
|
||||
client := user.NewUserClient(m.cli.Conn())
|
||||
return client.Ping(ctx, in, opts...)
|
||||
}
|
||||
|
||||
// 用户注册
|
||||
func (m *defaultUser) Register(ctx context.Context, in *RegisterRequest, opts ...grpc.CallOption) (*RegisterResponse, error) {
|
||||
client := user.NewUserClient(m.cli.Conn())
|
||||
return client.Register(ctx, in, opts...)
|
||||
}
|
||||
|
||||
// 用户登录,返回 JWT Token
|
||||
func (m *defaultUser) Login(ctx context.Context, in *LoginRequest, opts ...grpc.CallOption) (*LoginResponse, error) {
|
||||
client := user.NewUserClient(m.cli.Conn())
|
||||
return client.Login(ctx, in, opts...)
|
||||
}
|
||||
|
||||
// 用户登出,可选实现
|
||||
func (m *defaultUser) Logout(ctx context.Context, in *LogoutRequest, opts ...grpc.CallOption) (*LogoutResponse, error) {
|
||||
client := user.NewUserClient(m.cli.Conn())
|
||||
return client.Logout(ctx, in, opts...)
|
||||
}
|
||||
|
||||
// 获取用户信息
|
||||
func (m *defaultUser) GetUserInfo(ctx context.Context, in *GetUserInfoRequest, opts ...grpc.CallOption) (*GetUserInfoResponse, error) {
|
||||
client := user.NewUserClient(m.cli.Conn())
|
||||
return client.GetUserInfo(ctx, in, opts...)
|
||||
}
|
||||
Reference in New Issue
Block a user