Skip to content

Commit 29e6422

Browse files
committed
feat: add the logic client service
1 parent 6429860 commit 29e6422

File tree

15 files changed

+107
-41
lines changed

15 files changed

+107
-41
lines changed

api/user/v1/user_grpc.pb.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/pkg/options/mysql.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (so *MySQLOptions) Validate() []error {
4545
return errs
4646
}
4747

48-
// AddFlags adds flags related to server storage for a specific APIServer to the specified FlagSet.
48+
// AddFlags adds flags related to service storage for a specific APIServer to the specified FlagSet.
4949
func (mo *MySQLOptions) AddFlags(fs *pflag.FlagSet) {
5050
fs.StringVar(&mo.Host, "mysql.host", mo.Host, ""+
5151
"MySQL service host address. If left blank, the following related mysql options will be ignored.")
@@ -60,7 +60,7 @@ func (mo *MySQLOptions) AddFlags(fs *pflag.FlagSet) {
6060
"Password for access to mysql, should be used pair with password.")
6161

6262
fs.StringVar(&mo.Database, "mysql.database", mo.Database, ""+
63-
"Database name for the server to use.")
63+
"Database name for the service to use.")
6464

6565
fs.IntVar(&mo.MaxIdleConnections, "mysql.max-idle-connections", mo.MaxOpenConnections, ""+
6666
"Maximum idle connections allowed to connect to mysql.")

app/pkg/options/server.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,16 @@ func (so *ServerOptions) Validate() (errs []error) {
3535
}
3636

3737
func (so *ServerOptions) AddFlags(fs *pflag.FlagSet) {
38-
fs.BoolVar(&so.EnableProfiling, "server.enable-profiling", so.EnableProfiling,
38+
fs.BoolVar(&so.EnableProfiling, "service.enable-profiling", so.EnableProfiling,
3939
"enable-profiling, if true, will add <host>:<port>/debug/pprof/, default is true")
40-
fs.BoolVar(&so.EnableMetrics, "server.enable-metrics", so.EnableMetrics,
40+
fs.BoolVar(&so.EnableMetrics, "service.enable-metrics", so.EnableMetrics,
4141
"enable-metrics, if true, will add /metrics, default is true")
42-
fs.BoolVar(&so.EnableHealthCheck, "server.enable-health-check", so.EnableHealthCheck,
42+
fs.BoolVar(&so.EnableHealthCheck, "service.enable-health-check", so.EnableHealthCheck,
4343
"enable-health-check, if true, will add health check route, default is true")
4444
//fs.StringVarP 带有简写命令
4545
// (接收值的变量,命令名称,默认值,描述)
46-
fs.StringVar(&so.Host, "server.host", so.Host, "server host default is 127.0.0.1")
47-
fs.IntVar(&so.Port, "server.port", so.Port, "server port default is 8078")
48-
fs.IntVar(&so.HttpPort, "server.http-port", so.HttpPort, "server http port default is 8079")
49-
fs.StringVar(&so.Name, "server.name", so.Name, "server name default is user-srv")
46+
fs.StringVar(&so.Host, "service.host", so.Host, "service host default is 127.0.0.1")
47+
fs.IntVar(&so.Port, "service.port", so.Port, "service port default is 8078")
48+
fs.IntVar(&so.HttpPort, "service.http-port", so.HttpPort, "service http port default is 8079")
49+
fs.StringVar(&so.Name, "service.name", so.Name, "service name default is user-srv")
5050
}

app/shop/admin/config/config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func New() *Config {
2020

2121
type Config struct {
2222
Log *log.Options `json:"log" mapstructure:"log"`
23-
Server *options.ServerOptions `json:"server" mapstructure:"server"` // 服务发现
23+
Server *options.ServerOptions `json:"service" mapstructure:"service"` // 服务发现
2424
Registry *options.RegistryOptions `json:"registry" mapstructure:"registry"` // 注册中心
2525
Jwt *options.JwtOptions `json:"jwt" mapstructure:"jwt"` // jwt
2626
}
@@ -29,7 +29,7 @@ type Config struct {
2929
func (c *Config) Flags() (fss cliflag.NamedFlagSets) {
3030
// fss.FlagSet("logs") -> 创建一个FlagSet对象,命名为logs,做为专属的 logs 传递给 Config.Log
3131
c.Log.AddFlags(fss.FlagSet("logs"))
32-
c.Server.AddFlags(fss.FlagSet("server"))
32+
c.Server.AddFlags(fss.FlagSet("service"))
3333
c.Registry.AddFlags(fss.FlagSet("registry"))
3434
c.Jwt.AddFlags(fss.FlagSet("jwt"))
3535
return fss

app/shop/admin/http.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"github.com/coderi421/goshop/app/shop/admin/config"
66
)
77

8-
// NewUserHTTPServer 创建一个http server
8+
// NewUserHTTPServer 创建一个http service
99
func NewUserHTTPServer(conf *config.Config) (*restserver.Server, error) {
1010
//trace.InitAgent(trace.Options{
1111
// Name: conf.Telemetry.Name,

app/shop/custom/config/config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func New() *Config {
2020

2121
type Config struct {
2222
Log *log.Options `json:"log" mapstructure:"log"`
23-
Server *options.ServerOptions `json:"server" mapstructure:"server"` // 服务发现
23+
Server *options.ServerOptions `json:"service" mapstructure:"service"` // 服务发现
2424
Registry *options.RegistryOptions `json:"registry" mapstructure:"registry"` // 注册中心
2525
Jwt *options.JwtOptions `json:"jwt" mapstructure:"jwt"` // jwt
2626
}
@@ -29,7 +29,7 @@ type Config struct {
2929
func (c *Config) Flags() (fss cliflag.NamedFlagSets) {
3030
// fss.FlagSet("logs") -> 创建一个FlagSet对象,命名为logs,做为专属的 logs 传递给 Config.Log
3131
c.Log.AddFlags(fss.FlagSet("logs"))
32-
c.Server.AddFlags(fss.FlagSet("server"))
32+
c.Server.AddFlags(fss.FlagSet("service"))
3333
c.Registry.AddFlags(fss.FlagSet("registry"))
3434
c.Jwt.AddFlags(fss.FlagSet("jwt"))
3535
return fss

app/shop/custom/http.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"github.com/coderi421/goshop/app/shop/custom/config"
66
)
77

8-
// NewUserHTTPServer 创建一个http server
8+
// NewUserHTTPServer 创建一个http service
99
func NewCustomHTTPServer(conf *config.Config) (*restserver.Server, error) {
1010
//trace.InitAgent(trace.Options{
1111
// Name: conf.Telemetry.Name,

app/shop/custom/internal/data/rpc/user.go

+52-14
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import (
55
"github.com/coderi421/gframework/gmicro/registry"
66
"github.com/coderi421/gframework/gmicro/server/rpcserver"
77
"github.com/coderi421/gframework/gmicro/server/rpcserver/clientinterceptors"
8+
itime "github.com/coderi421/gframework/pkg/common/time"
89
"github.com/coderi421/gframework/pkg/errors"
910
upbv1 "github.com/coderi421/goshop/api/user/v1"
1011
"github.com/coderi421/goshop/app/pkg/code"
1112
"github.com/coderi421/goshop/app/pkg/options"
1213
"github.com/coderi421/goshop/app/shop/custom/internal/data"
14+
"time"
1315
)
1416

1517
const (
@@ -40,27 +42,63 @@ type users struct {
4042

4143
var _ data.UserData = &users{}
4244

43-
func (u *users) Create(ctx context.Context, user *data.User) error {
44-
//TODO implement me
45-
panic("implement me")
45+
func (u *users) Create(ctx context.Context, user *data.User) (int32, error) {
46+
protoUser := &upbv1.CreateUserInfo{
47+
NickName: user.NickName,
48+
PassWord: user.PassWord,
49+
Mobile: user.Mobile,
50+
}
51+
resp, err := u.uc.CreateUser(ctx, protoUser)
52+
return resp.Id, err
4653
}
4754

4855
func (u *users) Update(ctx context.Context, user *data.User) error {
49-
//TODO implement me
50-
panic("implement me")
56+
protoUser := &upbv1.UpdateUserInfo{
57+
Id: user.ID,
58+
NickName: user.NickName,
59+
Gender: user.Gender,
60+
BirthDay: uint64(user.Birthday.Unix()),
61+
}
62+
_, err := u.uc.UpdateUser(ctx, protoUser)
63+
return err
5164
}
5265

53-
func (u *users) Get(ctx context.Context, userID uint64) (data.User, error) {
54-
//TODO implement me
55-
panic("implement me")
66+
func (u *users) Get(ctx context.Context, userID int32) (*data.User, error) {
67+
user, err := u.uc.GetUserById(ctx, &upbv1.IdRequest{Id: userID})
68+
if err != nil {
69+
return nil, err
70+
}
71+
return &data.User{
72+
ID: user.Id,
73+
Mobile: user.Mobile,
74+
NickName: user.NickName,
75+
Birthday: itime.Time{time.Unix(int64(user.BirthDay), 0)}.Time,
76+
Gender: user.Gender,
77+
Role: user.Role,
78+
PassWord: user.PassWord,
79+
}, nil
5680
}
5781

58-
func (u *users) GetByMobile(ctx context.Context, mobile string) (data.User, error) {
59-
//TODO implement me
60-
panic("implement me")
82+
func (u *users) GetByMobile(ctx context.Context, mobile string) (*data.User, error) {
83+
user, err := u.uc.GetUserByMobile(ctx, &upbv1.MobileRequest{Mobile: mobile})
84+
if err != nil {
85+
return nil, err
86+
}
87+
return &data.User{
88+
ID: user.Id,
89+
Mobile: user.Mobile,
90+
NickName: user.NickName,
91+
Birthday: itime.Time{time.Unix(int64(user.BirthDay), 0)}.Time,
92+
Gender: user.Gender,
93+
Role: user.Role,
94+
PassWord: user.PassWord,
95+
}, nil
6196
}
6297

63-
func (u *users) CheckPassWord(ctx context.Context, password, encryptedPwd string) error {
64-
//TODO implement me
65-
panic("implement me")
98+
func (u *users) CheckPassWord(ctx context.Context, password, encryptedPwd string) (ok bool, err error) {
99+
cres, err := u.uc.CheckPassWord(ctx, &upbv1.PasswordCheckInfo{
100+
Password: password,
101+
EncryptedPassword: encryptedPwd,
102+
})
103+
return cres.Success, err
66104
}

app/shop/custom/internal/data/user.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
type User struct {
9-
ID uint64 `json:"id"`
9+
ID int32 `json:"id"`
1010
Mobile string `json:"mobile"`
1111
NickName string `json:"nick_name"`
1212
Birthday time.Time `gorm:"type:datetime"`
@@ -21,9 +21,9 @@ type UserListDO struct {
2121
}
2222

2323
type UserData interface {
24-
Create(ctx context.Context, user *User) error
24+
Create(ctx context.Context, user *User) (id int32, err error)
2525
Update(ctx context.Context, user *User) error
26-
Get(ctx context.Context, userID uint64) (User, error)
27-
GetByMobile(ctx context.Context, mobile string) (User, error)
28-
CheckPassWord(ctx context.Context, password, encryptedPwd string) error
26+
Get(ctx context.Context, userID int32) (*User, error)
27+
GetByMobile(ctx context.Context, mobile string) (*User, error)
28+
CheckPassWord(ctx context.Context, password, encryptedPwd string) (ok bool, err error)
2929
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package service
2+
3+
import user "github.com/coderi421/goshop/app/shop/custom/internal/service/user/v1"
4+
5+
type ServiceFactory interface {
6+
User() user.UserSrv
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package v1
2+
3+
import (
4+
"context"
5+
"github.com/coderi421/goshop/app/shop/custom/internal/data"
6+
)
7+
8+
type UserDTO struct {
9+
data.User
10+
11+
Token string `json:"token"`
12+
ExpiresAt int64 `json:"expires_at"`
13+
}
14+
type UserSrv interface {
15+
MobileLogin(ctx context.Context, mobile, password string) (*UserDTO, error)
16+
Register(ctx context.Context, mobile, password string, codes string) (*UserDTO, error)
17+
Updata(ctx context.Context, userDTO *UserDTO) error
18+
Get(ctx context.Context, userID uint64) (*UserDTO, error)
19+
GetByMobile(ctx context.Context, mobile string) (*UserDTO, error)
20+
CheckPassWord(ctx context.Context, password, EncryptedPassword string) (bool, error)
21+
}

app/user/client/client.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
func main() {
1717
//客户端,设置全局负载均衡策略,这里选择了 random
18-
// 这个逻辑中,balancerName是selector,在 gmicro/server/rpcserver/balancer.go中规定的
18+
// 这个逻辑中,balancerName是selector,在 gmicro/service/rpcserver/balancer.go中规定的
1919
selector.SetGlobalSelector(random.NewBuilder())
2020
// selector.SetGlobalSelector(random.NewBuilder()) 设定的全局的 selector 然后这里,调用 selector 进行注册
2121
rpcserver.InitBuilder()
@@ -52,7 +52,7 @@ func main() {
5252
uc := v1.NewUserClient(conn)
5353

5454
// 测试 grpc 服务的负载均衡用例
55-
// 在终端中,使用 --server.port=*** --server.http-port -c configs/user/srv.yaml 命令启动启动多个,这里就会轮询调用,测试负载均衡
55+
// 在终端中,使用 --service.port=*** --service.http-port -c configs/user/srv.yaml 命令启动启动多个,这里就会轮询调用,测试负载均衡
5656
for {
5757
re, err := uc.GetUserList(context.Background(), &v1.PageInfo{
5858
Pn: 1,

app/user/srv/config/config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func New() *Config {
2222
type Config struct {
2323
Log *log.Options `json:"log" mapstructure:"log"`
2424
// 服务发现
25-
Server *options.ServerOptions `json:"server" mapstructure:"server"`
25+
Server *options.ServerOptions `json:"service" mapstructure:"service"`
2626
// 注册中心
2727
Registry *options.RegistryOptions `json:"registry" mapstructure:"registry"`
2828
// 链路追踪
@@ -34,7 +34,7 @@ type Config struct {
3434
func (c *Config) Flags() (fss cliflag.NamedFlagSets) {
3535
// fss.FlagSet("logs") -> 创建一个FlagSet对象,命名为logs,做为专属的 logs 传递给 Config.Log
3636
c.Log.AddFlags(fss.FlagSet("logs"))
37-
c.Server.AddFlags(fss.FlagSet("server"))
37+
c.Server.AddFlags(fss.FlagSet("service"))
3838
c.Registry.AddFlags(fss.FlagSet("registry"))
3939
c.Telemetry.AddFlags(fss.FlagSet("telemetry"))
4040
c.MySQLOptions.AddFlags(fss.FlagSet("mysql"))

cmd/shop/admin/admin.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ func main() {
1616
if len(os.Getenv("GOMAXPROCS")) == 0 {
1717
runtime.GOMAXPROCS(runtime.NumCPU())
1818
}
19-
admin.NewApp("admin-server").Run()
19+
admin.NewApp("admin-service").Run()
2020
}

cmd/user/user.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ func main() {
1818
}
1919

2020
// 通过 NewApp 创建一个 app.App 对象
21-
srv.NewApp("user-server").Run()
21+
srv.NewApp("user-service").Run()
2222
// 通过 Run 方法启动服务
2323
}

0 commit comments

Comments
 (0)