add bookstore example
This commit is contained in:
13
example/bookstore/rpc/check/internal/config/config.go
Executable file
13
example/bookstore/rpc/check/internal/config/config.go
Executable file
@@ -0,0 +1,13 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"github.com/tal-tech/go-zero/core/stores/cache"
|
||||
"github.com/tal-tech/go-zero/rpcx"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
rpcx.RpcServerConf
|
||||
DataSource string
|
||||
Table string
|
||||
Cache cache.CacheConf
|
||||
}
|
||||
35
example/bookstore/rpc/check/internal/logic/checklogic.go
Executable file
35
example/bookstore/rpc/check/internal/logic/checklogic.go
Executable file
@@ -0,0 +1,35 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"bookstore/rpc/check/internal/svc"
|
||||
check "bookstore/rpc/check/pb"
|
||||
"context"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type CheckLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewCheckLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CheckLogic {
|
||||
return &CheckLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *CheckLogic) Check(in *check.CheckReq) (*check.CheckResp, error) {
|
||||
resp, err := l.svcCtx.Model.FindOne(in.Book)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &check.CheckResp{
|
||||
Found: true,
|
||||
Price: resp.Price,
|
||||
}, nil
|
||||
}
|
||||
26
example/bookstore/rpc/check/internal/server/checkerserver.go
Executable file
26
example/bookstore/rpc/check/internal/server/checkerserver.go
Executable file
@@ -0,0 +1,26 @@
|
||||
// Code generated by goctl. DO NOT EDIT!
|
||||
// Source: check.proto
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"bookstore/rpc/check/internal/logic"
|
||||
"bookstore/rpc/check/internal/svc"
|
||||
check "bookstore/rpc/check/pb"
|
||||
"context"
|
||||
)
|
||||
|
||||
type CheckerServer struct {
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewCheckerServer(svcCtx *svc.ServiceContext) *CheckerServer {
|
||||
return &CheckerServer{
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *CheckerServer) Check(ctx context.Context, in *check.CheckReq) (*check.CheckResp, error) {
|
||||
l := logic.NewCheckLogic(ctx, s.svcCtx)
|
||||
return l.Check(in)
|
||||
}
|
||||
20
example/bookstore/rpc/check/internal/svc/servicecontext.go
Executable file
20
example/bookstore/rpc/check/internal/svc/servicecontext.go
Executable file
@@ -0,0 +1,20 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"bookstore/rpc/check/internal/config"
|
||||
"bookstore/rpc/model"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/stores/sqlx"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
c config.Config
|
||||
Model *model.BookModel
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
return &ServiceContext{
|
||||
c: c,
|
||||
Model: model.NewBookModel(sqlx.NewMysql(c.DataSource), c.Cache, c.Table),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user