add bookstore example

This commit is contained in:
kevin
2020-09-03 23:26:04 +08:00
parent 167422ac4f
commit 11dd3d75ec
44 changed files with 1741 additions and 121 deletions

View 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
}

View 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
}

View 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)
}

View 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),
}
}