Skip to content

Commit

Permalink
add migrate interface
Browse files Browse the repository at this point in the history
  • Loading branch information
sunjiayu committed Sep 15, 2024
1 parent c043256 commit 9a901a3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
26 changes: 16 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,6 @@ func run() error {
if err != nil {
return err
}

wxPaymentServer, err := wx_payment_service.WxPaymentServiceInitialize(&ctx)
if err != nil {
grpclog.Fatal("WxPaymentServiceInitialize failed error:", err)
return err
}
err = wx_payment_pb.RegisterWxPaymentServiceHandlerServer(ctx, mux, wxPaymentServer)
if err != nil {
return err
}
// Generated routes end

// Custom routes begin
Expand Down Expand Up @@ -230,6 +220,22 @@ func run() error {
grpclog.Fatalf("PlatformService RedisSRem HandlePath failed error:%+v", err)
return err
}
if err := mux.HandlePath("POST", "/platform/migrate", func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
platformServer.Migrate(&ctx, w, r)
}); err != nil {
grpclog.Fatalf("PlatformService RedisSRem HandlePath failed error:%+v", err)
return err
}
// 微信支付
wxPaymentServer, err := wx_payment_service.WxPaymentServiceInitialize(&ctx, platformServer)
if err != nil {
grpclog.Fatal("WxPaymentServiceInitialize failed error:", err)
return err
}
err = wx_payment_pb.RegisterWxPaymentServiceHandlerServer(ctx, mux, wxPaymentServer)
if err != nil {
return err
}

// 转发数据接口
forwardServer, err := platform.ForwardServiceInitialize(&ctx)
Expand Down
19 changes: 19 additions & 0 deletions service/platform/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,22 @@ func (server PlatformService) RedisSRem(ctx *context.Context, w http.ResponseWri
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(resp))
}

func (server PlatformService) Migrate(ctx *context.Context, w http.ResponseWriter, r *http.Request) {
grpclog.Infof("Migrate triggered")
key := "mikiai_whitelist_user"
whitelist := server.redisClient.SMembers(*ctx, key)
if whitelist == nil {
grpclog.Errorf("error exec smembers cmd")
return
}
openids := whitelist.Val()
for _, openid := range openids {
input := WhitelistUserData{OpenID: &openid}
server.WhitelistMySqlInsert(ctx, &input)
}
resp := fmt.Sprintf("{\"res\":%v}", 1)
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(resp))
}
10 changes: 9 additions & 1 deletion service/wx_payment/wx_payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ type WxPaymentServiceImpl struct {
WxSerialNo string `yaml:"wx_serial_no"`
RedisClient *redis.Client
WxClient *core.Client
Platform *platform.PlatformService
wx_payment.UnimplementedWxPaymentServiceServer
}

func WxPaymentServiceInitialize(ctx *context.Context) (*WxPaymentServiceImpl, error) {
func WxPaymentServiceInitialize(ctx *context.Context, platform *platform.PlatformService) (*WxPaymentServiceImpl, error) {
// load keys
content, err := os.ReadFile(*authFile)
if err != nil {
Expand Down Expand Up @@ -75,6 +76,7 @@ func WxPaymentServiceInitialize(ctx *context.Context) (*WxPaymentServiceImpl, er
})

server.WxClient = wxClient
server.Platform = platform
return &server, nil
}

Expand Down Expand Up @@ -130,6 +132,12 @@ func (server WxPaymentServiceImpl) Jsapi(ctx context.Context, req *wx_payment.Js
grpclog.Errorf("error exec smembers cmd")
return &resp, nil
}
// dbQueryData := platform.WhitelistUserData{OpenID: &openid}
// _, err := server.Platform.WhitelistMySqlQuery(&ctx, &dbQueryData)
// if err != nil {
// grpclog.Errorf("whitelist query openid: %v fail err:%v", dbQueryData.OpenID, err)
// return &resp, nil
// }
if isMember.Val() {
grpclog.Infof("openid:%v is in whitelist, order_type=3", openid)
// Edit order db
Expand Down

0 comments on commit 9a901a3

Please sign in to comment.