successfully run facade
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package facade
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"github.com/cloudwego/hertz/pkg/protocol/consts"
|
||||
"github.com/suyiiyii/hertz101/app/facade/biz/service"
|
||||
"github.com/suyiiyii/hertz101/app/facade/biz/utils"
|
||||
facade "github.com/suyiiyii/hertz101/app/facade/hertz_gen/facade"
|
||||
)
|
||||
|
||||
// Register .
|
||||
// @router /register [POST]
|
||||
func Register(ctx context.Context, c *app.RequestContext) {
|
||||
var err error
|
||||
var req facade.RegisterReq
|
||||
err = c.BindAndValidate(&req)
|
||||
if err != nil {
|
||||
utils.SendErrResponse(ctx, c, consts.StatusOK, err)
|
||||
return
|
||||
}
|
||||
|
||||
resp := &facade.RegisterResp{}
|
||||
resp, err = service.NewRegisterService(ctx, c).Run(&req)
|
||||
if err != nil {
|
||||
utils.SendErrResponse(ctx, c, consts.StatusOK, err)
|
||||
return
|
||||
}
|
||||
|
||||
utils.SendSuccessResponse(ctx, c, consts.StatusOK, resp)
|
||||
}
|
||||
|
||||
// Login .
|
||||
// @router /login [POST]
|
||||
func Login(ctx context.Context, c *app.RequestContext) {
|
||||
var err error
|
||||
var req facade.LoginReq
|
||||
err = c.BindAndValidate(&req)
|
||||
if err != nil {
|
||||
utils.SendErrResponse(ctx, c, consts.StatusOK, err)
|
||||
return
|
||||
}
|
||||
|
||||
resp := &facade.LoginResp{}
|
||||
resp, err = service.NewLoginService(ctx, c).Run(&req)
|
||||
if err != nil {
|
||||
utils.SendErrResponse(ctx, c, consts.StatusOK, err)
|
||||
return
|
||||
}
|
||||
|
||||
utils.SendSuccessResponse(ctx, c, consts.StatusOK, resp)
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package facade
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/cloudwego/hertz/pkg/app/server"
|
||||
//"github.com/cloudwego/hertz/pkg/common/test/assert"
|
||||
"github.com/cloudwego/hertz/pkg/common/ut"
|
||||
)
|
||||
|
||||
func TestRegister(t *testing.T) {
|
||||
h := server.Default()
|
||||
h.POST("/register", Register)
|
||||
path := "/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)
|
||||
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()))
|
||||
}
|
||||
|
||||
func TestLogin(t *testing.T) {
|
||||
h := server.Default()
|
||||
h.POST("/login", Login)
|
||||
path := "/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)
|
||||
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()))
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package facade
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"github.com/cloudwego/hertz/pkg/protocol/consts"
|
||||
"github.com/suyiiyii/hertz101/app/facade/biz/utils"
|
||||
)
|
||||
@@ -1,10 +0,0 @@
|
||||
package facade
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/cloudwego/hertz/pkg/app/server"
|
||||
//"github.com/cloudwego/hertz/pkg/common/test/assert"
|
||||
"github.com/cloudwego/hertz/pkg/common/ut"
|
||||
)
|
||||
@@ -16,4 +16,7 @@ import (
|
||||
// Register register routes based on the IDL 'api.${HTTP Method}' annotation.
|
||||
func Register(r *server.Hertz) {
|
||||
|
||||
root := r.Group("/", rootMw()...)
|
||||
root.POST("/login", append(_loginMw(), facade.Login)...)
|
||||
root.POST("/register", append(_registerMw(), facade.Register)...)
|
||||
}
|
||||
|
||||
@@ -5,3 +5,18 @@ package facade
|
||||
import (
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
)
|
||||
|
||||
func rootMw() []app.HandlerFunc {
|
||||
// your code...
|
||||
return nil
|
||||
}
|
||||
|
||||
func _loginMw() []app.HandlerFunc {
|
||||
// your code...
|
||||
return nil
|
||||
}
|
||||
|
||||
func _registerMw() []app.HandlerFunc {
|
||||
// your code...
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
facade "github.com/suyiiyii/hertz101/app/facade/hertz_gen/facade"
|
||||
)
|
||||
|
||||
type LoginService struct {
|
||||
RequestContext *app.RequestContext
|
||||
Context context.Context
|
||||
}
|
||||
|
||||
func NewLoginService(Context context.Context, RequestContext *app.RequestContext) *LoginService {
|
||||
return &LoginService{RequestContext: RequestContext, Context: Context}
|
||||
}
|
||||
|
||||
func (h *LoginService) Run(req *facade.LoginReq) (resp *facade.LoginResp, err error) {
|
||||
//defer func() {
|
||||
// hlog.CtxInfof(h.Context, "req = %+v", req)
|
||||
// hlog.CtxInfof(h.Context, "resp = %+v", resp)
|
||||
//}()
|
||||
// todo edit your code
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
facade "github.com/suyiiyii/hertz101/app/facade/hertz_gen/facade"
|
||||
)
|
||||
|
||||
type RegisterService struct {
|
||||
RequestContext *app.RequestContext
|
||||
Context context.Context
|
||||
}
|
||||
|
||||
func NewRegisterService(Context context.Context, RequestContext *app.RequestContext) *RegisterService {
|
||||
return &RegisterService{RequestContext: RequestContext, Context: Context}
|
||||
}
|
||||
|
||||
func (h *RegisterService) Run(req *facade.RegisterReq) (resp *facade.RegisterResp, err error) {
|
||||
//defer func() {
|
||||
// hlog.CtxInfof(h.Context, "req = %+v", req)
|
||||
// hlog.CtxInfof(h.Context, "resp = %+v", resp)
|
||||
//}()
|
||||
// todo edit your code
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user