Skip to content

Commit

Permalink
Merge pull request #71 from rogerogers/chore/b-ch10
Browse files Browse the repository at this point in the history
feat(gomall): update ch10
  • Loading branch information
baiyutang authored Jun 3, 2024
2 parents b02fb39 + 407e01d commit 7bf3344
Show file tree
Hide file tree
Showing 120 changed files with 1,696 additions and 2,452 deletions.
1 change: 1 addition & 0 deletions gomall/app/email/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/cloudwego/biz-demo/gomall/common v0.0.0-00010101000000-000000000000
github.com/cloudwego/biz-demo/gomall/rpc_gen v0.0.0-00010101000000-000000000000
github.com/cloudwego/kitex v0.8.0
github.com/joho/godotenv v1.5.1
github.com/kitex-contrib/obs-opentelemetry v0.2.6
github.com/kr/pretty v0.3.1
github.com/nats-io/nats.go v1.31.0
Expand Down
1 change: 1 addition & 0 deletions gomall/app/email/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
Expand Down
2 changes: 2 additions & 0 deletions gomall/tutorial/ch10/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
app/*/tmp
app/*/.env
6 changes: 3 additions & 3 deletions gomall/tutorial/ch10/Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
export ROOT_MOD=github.com/cloudwego/biz-demo/gomall
.PHONY: gen-demo-proto
gen-demo-proto:
@cd demo/demo_proto && cwgo server -I ../../idl --module github.com/cloudwego/biz-demo/gomall/demo/demo_proto --service demo_proto --idl ../../idl/echo.proto
@cd demo/demo_proto && cwgo server -I ../../idl --module ${ROOT_MOD}/demo/demo_proto --service demo_proto --idl ../../idl/echo.proto

.PHONY: gen-demo-thrift
gen-demo-thrift:
@cd demo/demo_thrift && cwgo server --module github.com/cloudwego/biz-demo/gomall/demo/demo_thrift --service demo_thrift --idl ../../idl/echo.thrift
@cd demo/demo_thrift && cwgo server --module ${ROOT_MOD}/demo/demo_thrift --service demo_thrift --idl ../../idl/echo.thrift

.PHONY: demo-link-fix
demo-link-fix:
cd demo/demo_proto && golangci-lint run -E gofumpt --path-prefix=. --fix --timeout=5m

.PHONY: gen-frontend
gen-frontend:
@cd app/frontend && cwgo server -I ../../idl --type HTTP --service frontend --module github.com/cloudwego/biz-demo/gomall/app/frontend --idl ../../idl/frontend/category_page.proto
@cd app/frontend && cwgo server -I ../../idl --type HTTP --service frontend --module ${ROOT_MOD}/app/frontend --idl ../../idl/frontend/category_page.proto

.PHONY: gen-user
gen-user:
Expand Down
35 changes: 16 additions & 19 deletions gomall/tutorial/ch10/app/frontend/biz/handler/auth/auth_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,47 +22,45 @@ import (
auth "github.com/cloudwego/biz-demo/gomall/app/frontend/hertz_gen/frontend/auth"
common "github.com/cloudwego/biz-demo/gomall/app/frontend/hertz_gen/frontend/common"
"github.com/cloudwego/hertz/pkg/app"
hertzUtils "github.com/cloudwego/hertz/pkg/common/utils"
"github.com/cloudwego/hertz/pkg/protocol/consts"
)

// Register .
// @router /auth/register [POST]
func Register(ctx context.Context, c *app.RequestContext) {
// Login .
// @router /auth/login [POST]
func Login(ctx context.Context, c *app.RequestContext) {
var err error
var req auth.RegisterReq
var req auth.LoginReq
err = c.BindAndValidate(&req)
if err != nil {
utils.SendErrResponse(ctx, c, consts.StatusOK, err)
return
}

_, err = service.NewRegisterService(ctx, c).Run(&req)
redirect, err := service.NewLoginService(ctx, c).Run(&req)
if err != nil {
c.HTML(consts.StatusOK, "sign-up", hertzUtils.H{"error": err})
utils.SendErrResponse(ctx, c, consts.StatusOK, err)
return
}
c.Redirect(consts.StatusFound, []byte("/"))
c.Redirect(consts.StatusFound, []byte(redirect))
}

// Login .
// @router /auth/login [POST]
func Login(ctx context.Context, c *app.RequestContext) {
// Register .
// @router /auth/register [POST]
func Register(ctx context.Context, c *app.RequestContext) {
var err error
var req auth.LoginReq
var req auth.RegisterReq
err = c.BindAndValidate(&req)
if err != nil {
utils.SendErrResponse(ctx, c, consts.StatusOK, err)
return
}

resp, err := service.NewLoginService(ctx, c).Run(&req)
_, err = service.NewRegisterService(ctx, c).Run(&req)

if err != nil {
utils.SendErrResponse(ctx, c, consts.StatusOK, err)
return
}

c.Redirect(consts.StatusFound, []byte(resp))
c.Redirect(consts.StatusFound, []byte("/"))
}

// Logout .
Expand All @@ -77,11 +75,10 @@ func Logout(ctx context.Context, c *app.RequestContext) {
}

_, err = service.NewLogoutService(ctx, c).Run(&req)

if err != nil {
utils.SendErrResponse(ctx, c, consts.StatusOK, err)
return
}
redirect := "/"

c.Redirect(consts.StatusFound, []byte(redirect))
c.Redirect(consts.StatusFound, []byte("/"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import (
"github.com/cloudwego/hertz/pkg/common/ut"
)

func TestRegister(t *testing.T) {
func TestLogin(t *testing.T) {
h := server.Default()
h.POST("/auth/register", Register)
path := "/auth/register" // todo: you can customize query
h.POST("/auth/login", Login)
path := "/auth/login" // todo: you can customize query
body := &ut.Body{Body: bytes.NewBufferString(""), Len: 1} // todo: you can customize body
header := ut.Header{} // todo: you can customize header
w := ut.PerformRequest(h.Engine, "POST", path, body, header)
Expand All @@ -38,10 +38,10 @@ func TestRegister(t *testing.T) {
// assert.DeepEqual(t, "null", string(resp.Body()))
}

func TestLogin(t *testing.T) {
func TestRegister(t *testing.T) {
h := server.Default()
h.POST("/auth/login", Login)
path := "/auth/login" // todo: you can customize query
h.POST("/auth/register", Register)
path := "/auth/register" // todo: you can customize query
body := &ut.Body{Body: bytes.NewBufferString(""), Len: 1} // todo: you can customize body
header := ut.Header{} // todo: you can customize header
w := ut.PerformRequest(h.Engine, "POST", path, body, header)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@ func Home(ctx context.Context, c *app.RequestContext) {
return
}

// resp, err :=
resp, err := service.NewHomeService(ctx, c).Run(&req)
if err != nil {
utils.SendErrResponse(ctx, c, consts.StatusOK, err)
return
}

c.HTML(consts.StatusOK, "home", utils.WarpResponse(ctx, c, resp))
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,26 @@ func GetProduct(ctx context.Context, c *app.RequestContext) {
utils.SendErrResponse(ctx, c, consts.StatusOK, err)
return
}
c.HTML(consts.StatusOK, "product", utils.WarpResponse(ctx, c, resp))

c.HTML(consts.StatusOK, "product", resp)
}

// SearchProducts .
// @router /search [GET]
func SearchProducts(ctx context.Context, c *app.RequestContext) {
var err error
var req product.SearchProductsReq
err = c.BindAndValidate(&req)
if err != nil {
utils.SendErrResponse(ctx, c, consts.StatusOK, err)
return
}

resp, err := service.NewSearchProductsService(ctx, c).Run(&req)
if err != nil {
utils.SendErrResponse(ctx, c, consts.StatusOK, err)
return
}

c.HTML(consts.StatusOK, "search", resp)
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,18 @@ func TestGetProduct(t *testing.T) {
// assert.DeepEqual(t, 200, resp.StatusCode())
// assert.DeepEqual(t, "null", string(resp.Body()))
}

func TestSearchProducts(t *testing.T) {
h := server.Default()
h.GET("/search", SearchProducts)
path := "/search" // todo: you can customize query
body := &ut.Body{Body: bytes.NewBufferString(""), Len: 1} // todo: you can customize body
header := ut.Header{} // todo: you can customize header
w := ut.PerformRequest(h.Engine, "GET", path, body, header)
resp := w.Result()
t.Log(string(resp.Body()))

// todo edit your unit test.
// assert.DeepEqual(t, 200, resp.StatusCode())
// assert.DeepEqual(t, "null", string(resp.Body()))
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions gomall/tutorial/ch10/app/frontend/biz/router/register.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions gomall/tutorial/ch10/app/frontend/biz/service/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ func NewCategoryService(Context context.Context, RequestContext *app.RequestCont
}

func (h *CategoryService) Run(req *category.CategoryReq) (resp map[string]any, err error) {
p, _ := rpc.ProductClient.ListProducts(h.Context, &product.ListProductsReq{CategoryName: req.Category})
p, err := rpc.ProductClient.ListProducts(h.Context, &product.ListProductsReq{CategoryName: req.Category})
if err != nil {
return nil, err
}
return utils.H{
"title": "Category",
"items": p.Products,
"cart_num": 10,
"title": "Category",
"items": p.Products,
}, nil
}
3 changes: 2 additions & 1 deletion gomall/tutorial/ch10/app/frontend/biz/service/get_product.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ func NewGetProductService(Context context.Context, RequestContext *app.RequestCo
}

func (h *GetProductService) Run(req *product.ProductReq) (resp map[string]any, err error) {
p, err := rpc.ProductClient.GetProduct(h.Context, &rpcproduct.GetProductReq{Id: req.GetId()})
p, err := rpc.ProductClient.GetProduct(h.Context, &rpcproduct.GetProductReq{Id: req.Id})
if err != nil {
return nil, err
}

return utils.H{
"item": p.Product,
}, nil
Expand Down
13 changes: 5 additions & 8 deletions gomall/tutorial/ch10/app/frontend/biz/service/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/cloudwego/biz-demo/gomall/rpc_gen/kitex_gen/product"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/common/utils"
"github.com/cloudwego/kitex/pkg/klog"
)

type HomeService struct {
Expand All @@ -35,15 +34,13 @@ func NewHomeService(Context context.Context, RequestContext *app.RequestContext)
}

func (h *HomeService) Run(req *common.Empty) (res map[string]any, err error) {
ctx := h.Context
p, err := rpc.ProductClient.ListProducts(ctx, &product.ListProductsReq{})
products, err := rpc.ProductClient.ListProducts(h.Context, &product.ListProductsReq{})
if err != nil {
klog.Error(err)
return nil, err
}
var cartNum int

return utils.H{
"title": "Hot sale",
"cart_num": cartNum,
"items": p.Products,
"title": "Hot sale",
"items": products.Products,
}, nil
}
30 changes: 17 additions & 13 deletions gomall/tutorial/ch10/app/frontend/biz/service/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import (

auth "github.com/cloudwego/biz-demo/gomall/app/frontend/hertz_gen/frontend/auth"
"github.com/cloudwego/biz-demo/gomall/app/frontend/infra/rpc"
frontendutils "github.com/cloudwego/biz-demo/gomall/app/frontend/utils"
rpcuser "github.com/cloudwego/biz-demo/gomall/rpc_gen/kitex_gen/user"
"github.com/cloudwego/biz-demo/gomall/rpc_gen/kitex_gen/user"
"github.com/cloudwego/hertz/pkg/app"
"github.com/hertz-contrib/sessions"
)
Expand All @@ -34,23 +33,28 @@ func NewLoginService(Context context.Context, RequestContext *app.RequestContext
return &LoginService{RequestContext: RequestContext, Context: Context}
}

func (h *LoginService) Run(req *auth.LoginReq) (resp string, err error) {
res, err := rpc.UserClient.Login(h.Context, &rpcuser.LoginReq{Email: req.Email, Password: req.Password})
func (h *LoginService) Run(req *auth.LoginReq) (redirect string, err error) {
//defer func() {
// hlog.CtxInfof(h.Context, "req = %+v", req)
// hlog.CtxInfof(h.Context, "resp = %+v", resp)
//}()
resp, err := rpc.UserClient.Login(h.Context, &user.LoginReq{
Email: req.Email,
Password: req.Password,
})
if err != nil {
return
return "", err
}

session := sessions.Default(h.RequestContext)
session.Set("user_id", res.UserId)
session.Set("user_id", resp.UserId)
err = session.Save()
frontendutils.MustHandleError(err)
redirect := "/"
if frontendutils.ValidateNext(req.Next) {
redirect = req.Next
}
if err != nil {
return "", err
}

return redirect, nil
redirect = "/"
if req.Next != "" {
redirect = req.Next
}
return
}
9 changes: 8 additions & 1 deletion gomall/tutorial/ch10/app/frontend/biz/service/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,15 @@ func NewLogoutService(Context context.Context, RequestContext *app.RequestContex
}

func (h *LogoutService) Run(req *common.Empty) (resp *common.Empty, err error) {
//defer func() {
// hlog.CtxInfof(h.Context, "req = %+v", req)
// hlog.CtxInfof(h.Context, "resp = %+v", resp)
//}()
session := sessions.Default(h.RequestContext)
session.Clear()
session.Save() //nolint:errcheck
err = session.Save()
if err != nil {
return nil, err
}
return
}
Loading

0 comments on commit 7bf3344

Please sign in to comment.