Skip to content

Commit

Permalink
feat: use metainfo to deliver context in RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
FantasyRL committed Jan 11, 2025
1 parent a86959b commit b1b30c5
Show file tree
Hide file tree
Showing 19 changed files with 162 additions and 638 deletions.
6 changes: 1 addition & 5 deletions api/handler/api/academic_service.go

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

4 changes: 1 addition & 3 deletions api/handler/api/academic_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ func TestGetPlan(t *testing.T) {
mockey.Mock(rpc.GetCultivatePlanRPC).To(func(ctx context.Context, req *academic.GetPlanRequest) (*[]byte, error) {
if tc.ExpectedError {
// 根据测试用例的不同,可以自定义返回不同的错误信息
if req.Id == "" || req.Cookies == "" {
return nil, errors.New("GetCultivatePlanRPC: RPC called failed: 缺少必要参数")
}

return nil, errors.New("GetCultivatePlanRPC: RPC called failed: 错误的文件路径")
}
// 成功返回HTML内容
Expand Down
10 changes: 1 addition & 9 deletions api/handler/api/user_service.go

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

18 changes: 18 additions & 0 deletions api/mw/get_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/west2-online/fzuhelper-server/api/pack"
"github.com/west2-online/fzuhelper-server/kitex_gen/model"
metainfoContext "github.com/west2-online/fzuhelper-server/pkg/base/context"
"github.com/west2-online/fzuhelper-server/pkg/base/login_data"
"github.com/west2-online/fzuhelper-server/pkg/errno"
)
Expand All @@ -44,3 +45,20 @@ func GetHeaderParams() app.HandlerFunc {
c.Next(ctx)
}
}

func GetHeaderParamsForRPC() app.HandlerFunc {
return func(ctx context.Context, c *app.RequestContext) {
id := string(c.GetHeader("Id"))
cookies := string(c.GetHeader("Cookies"))
if id == "" || cookies == "" {
pack.RespError(c, errno.ParamMissingHeader)
c.Abort()
return
}
ctx = metainfoContext.WithLoginData(ctx, &model.LoginData{
Id: id,
Cookies: cookies,
})
c.Next(ctx)
}
}
1 change: 1 addition & 0 deletions api/router/api/middleware.go

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

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ require (
github.com/armon/go-metrics v0.4.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bufbuild/protocompile v0.14.1 // indirect
github.com/bytedance/gopkg v0.1.1 // indirect
github.com/bytedance/gopkg v0.1.1
github.com/bytedance/sonic v1.12.6
github.com/bytedance/sonic/loader v0.2.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions idl/academic.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ struct GetUnifiedExamResponse {
}

struct GetPlanRequest{
1: required string id
2: required string cookies
}

struct GetPlanResponse{
Expand Down
3 changes: 0 additions & 3 deletions idl/user.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ struct GetLoginDataResponse {
}

struct GetUserInfoRequest{
1: string id,
2: string cookies,
3: string stu_id,
}

struct GetUserInfoResponse{
Expand Down
2 changes: 1 addition & 1 deletion internal/academic/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (s *AcademicServiceImpl) GetUnifiedExam(ctx context.Context, req *academic.
// GetPlan implements the AcademicServiceImpl interface.
func (s *AcademicServiceImpl) GetPlan(ctx context.Context, req *academic.GetPlanRequest) (resp *academic.GetPlanResponse, err error) {
resp = new(academic.GetPlanResponse)
plan, err := service.NewAcademicService(ctx).GetPlan(req)
plan, err := service.NewAcademicService(ctx).GetPlan()
if err != nil {
logger.Infof("Academic.GetPlan: GetPlan failed, err: %v", err)
return resp, nil
Expand Down
12 changes: 8 additions & 4 deletions internal/academic/service/get_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ import (
"bytes"
"net/http"

"github.com/west2-online/fzuhelper-server/kitex_gen/academic"
"github.com/west2-online/fzuhelper-server/pkg/base/context"
"github.com/west2-online/fzuhelper-server/pkg/constants"
"github.com/west2-online/fzuhelper-server/pkg/errno"
"github.com/west2-online/fzuhelper-server/pkg/utils"
"github.com/west2-online/jwch"
)

func (s *AcademicService) GetPlan(req *academic.GetPlanRequest) (*[]byte, error) {
stu := jwch.NewStudent().WithLoginData(req.Id, utils.ParseCookies(req.Cookies))
func (s *AcademicService) GetPlan() (*[]byte, error) {
userHeader, err := context.GetLoginData(s.ctx)
if err != nil {
return nil, err
}

Check warning on line 34 in internal/academic/service/get_plan.go

View check run for this annotation

Codecov / codecov/patch

internal/academic/service/get_plan.go#L33-L34

Added lines #L33 - L34 were not covered by tests
stu := jwch.NewStudent().WithLoginData(userHeader.Id, utils.ParseCookies(userHeader.Cookies))
url, err := stu.GetCultivatePlan()
if err != nil {
return nil, errno.Errorf(errno.InternalServiceErrorCode, "AcademicService.GetPlan error:%v", err)
Expand All @@ -38,7 +42,7 @@ func (s *AcademicService) GetPlan(req *academic.GetPlanRequest) (*[]byte, error)
if err != nil {
return nil, errno.Errorf(errno.InternalServiceErrorCode, "AcademicService.GetPlan request error:%v", err)
}

Check warning on line 44 in internal/academic/service/get_plan.go

View check run for this annotation

Codecov / codecov/patch

internal/academic/service/get_plan.go#L43-L44

Added lines #L43 - L44 were not covered by tests
urlReq.Header.Set("Cookie", req.Cookies)
urlReq.Header.Set("Cookie", userHeader.Cookies)
htmlSource, err := getHtmlSource(urlReq)
if err != nil {
return nil, errno.Errorf(errno.InternalServiceErrorCode, "AcademicService.GetPlan getHtmlSource error:%v", err)
Expand Down
16 changes: 10 additions & 6 deletions internal/academic/service/get_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package service

import (
"context"
"fmt"
"net/http"
"strings"
Expand All @@ -25,7 +26,8 @@ import (
"github.com/bytedance/mockey"
"github.com/stretchr/testify/assert"

"github.com/west2-online/fzuhelper-server/kitex_gen/academic"
"github.com/west2-online/fzuhelper-server/kitex_gen/model"
meta "github.com/west2-online/fzuhelper-server/pkg/base/context"
"github.com/west2-online/jwch"
)

Expand All @@ -40,10 +42,6 @@ func TestGetPlan(t *testing.T) {
}
mockUrl := "https://www.example.com"
mockHtml := []byte(`body { background-color: #fff; }`)
req := &academic.GetPlanRequest{
Id: "202511012137102301000",
Cookies: "ASP.NET_SessionId=db123abcdefgh5ijklzjv2et",
}
testCases := []testCase{
{
name: "SuccessCase",
Expand Down Expand Up @@ -71,14 +69,20 @@ func TestGetPlan(t *testing.T) {
mockey.Mock((*jwch.Student).WithLoginData).To(func(identifier string, cookies []*http.Cookie) *jwch.Student {
return jwch.NewStudent()
}).Build()
mockey.Mock(meta.GetLoginData).To(func(ctx context.Context) (*model.LoginData, error) {
return &model.LoginData{
Id: "1111111111111111111111111111111111",
Cookies: "",
}, nil
}).Build()
mockey.Mock((*jwch.Student).GetCultivatePlan).To(func() (string, error) {
return tc.mockUrl, tc.mockFileError
}).Build()
mockey.Mock(getHtmlSource).To(func() (*[]byte, error) {
return tc.mockFileResult, tc.mockFileError
}).Build()
academicService := AcademicService{}
result, err := academicService.GetPlan(req)
result, err := academicService.GetPlan()
if tc.expectedError != nil {
assert.Nil(t, result)
assert.Contains(t, err.Error(), tc.expectedError.Error())
Expand Down
10 changes: 8 additions & 2 deletions internal/user/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/west2-online/fzuhelper-server/internal/user/service"
"github.com/west2-online/fzuhelper-server/kitex_gen/user"
"github.com/west2-online/fzuhelper-server/pkg/base"
metainfoContext "github.com/west2-online/fzuhelper-server/pkg/base/context"
"github.com/west2-online/fzuhelper-server/pkg/utils"
)

Expand Down Expand Up @@ -55,8 +56,13 @@ func (s *UserServiceImpl) GetLoginData(ctx context.Context, req *user.GetLoginDa
// GetUserInfo implements the UserServiceImpl interface.
func (s *UserServiceImpl) GetUserInfo(ctx context.Context, request *user.GetUserInfoRequest) (resp *user.GetUserInfoResponse, err error) {
resp = new(user.GetUserInfoResponse)
l := service.NewUserService(ctx, request.Id, utils.ParseCookies(request.Cookies), s.ClientSet)
info, err := l.GetUserInfo(request.StuId)
userHeader, err := metainfoContext.GetLoginData(ctx)
if err != nil {
resp.Base = base.BuildBaseResp(err)
return resp, nil
}
l := service.NewUserService(ctx, userHeader.Id, utils.ParseCookies(userHeader.Cookies), s.ClientSet)
info, err := l.GetUserInfo(userHeader.Id[len(userHeader.Id)-9:])
if err != nil {
resp.Base = base.BuildBaseResp(err)
return resp, nil
Expand Down
18 changes: 13 additions & 5 deletions internal/user/service/get_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ import (
"github.com/stretchr/testify/assert"
"gorm.io/gorm"

"github.com/west2-online/fzuhelper-server/kitex_gen/model"
"github.com/west2-online/fzuhelper-server/pkg/base"
meta "github.com/west2-online/fzuhelper-server/pkg/base/context"
"github.com/west2-online/fzuhelper-server/pkg/db"
"github.com/west2-online/fzuhelper-server/pkg/db/model"
dbmodel "github.com/west2-online/fzuhelper-server/pkg/db/model"
userDB "github.com/west2-online/fzuhelper-server/pkg/db/user"
"github.com/west2-online/fzuhelper-server/pkg/errno"
"github.com/west2-online/fzuhelper-server/pkg/utils"
Expand All @@ -38,7 +40,7 @@ import (
func TestUserService_GetUserInfo(t *testing.T) {
type testCase struct {
name string
expectedInfo *model.Student
expectedInfo *dbmodel.Student
expectedJwch *jwch.StudentDetail
expectedExist bool
mockError error
Expand All @@ -47,7 +49,7 @@ func TestUserService_GetUserInfo(t *testing.T) {
expectingError bool
expectingErrorMsg string
}
info := &model.Student{
info := &dbmodel.Student{
StuId: "102301000",
Sex: "sex",
Birthday: "1970-01-01",
Expand Down Expand Up @@ -113,16 +115,22 @@ func TestUserService_GetUserInfo(t *testing.T) {
mockClientSet.SFClient = new(utils.Snowflake)
mockClientSet.DBClient = new(db.Database)
userService := NewUserService(context.Background(), "", nil, mockClientSet)
mockey.Mock((*userDB.DBUser).GetStudentById).To(func(ctx context.Context, stuId string) (bool, *model.Student, error) {
mockey.Mock((*userDB.DBUser).GetStudentById).To(func(ctx context.Context, stuId string) (bool, *dbmodel.Student, error) {
return tc.expectedExist, tc.expectedInfo, tc.mockError
}).Build()
mockey.Mock((*jwch.Student).WithLoginData).To(func(identifier string, cookies []*http.Cookie) *jwch.Student {
return jwch.NewStudent()
}).Build()
mockey.Mock(meta.GetLoginData).To(func(ctx context.Context) (*model.LoginData, error) {
return &model.LoginData{
Id: "1111111111111111111111111111111111",
Cookies: "",
}, nil
}).Build()
mockey.Mock((*jwch.Student).GetInfo).To(func() (resp *jwch.StudentDetail, err error) {
return tc.expectedJwch, tc.mockJwchError
}).Build()
mockey.Mock((*userDB.DBUser).CreateStudent).To(func(ctx context.Context, userModel *model.Student) error {
mockey.Mock((*userDB.DBUser).CreateStudent).To(func(ctx context.Context, userModel *dbmodel.Student) error {
return tc.mockDBCreateError
}).Build()

Expand Down
Loading

0 comments on commit b1b30c5

Please sign in to comment.