Skip to content

Commit 741b5ad

Browse files
juniaoshaonianflycash
authored andcommitted
make check
1 parent 59bb5f9 commit 741b5ad

File tree

12 files changed

+69
-44
lines changed

12 files changed

+69
-44
lines changed

go.mod

+1-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ require (
1717
github.com/gotomicro/ego v1.1.19
1818
github.com/lithammer/shortuuid/v4 v4.0.0
1919
github.com/olivere/elastic/v7 v7.0.32
20+
github.com/pkg/errors v0.9.1
2021
github.com/redis/go-redis/v9 v9.4.0
2122
github.com/stretchr/testify v1.9.0
2223
github.com/tencentyun/qcloud-cos-sts-sdk v0.0.0-20231121073521-dd65d8941a16
@@ -64,7 +65,6 @@ require (
6465
github.com/golang/protobuf v1.5.3 // indirect
6566
github.com/google/cel-go v0.11.3 // indirect
6667
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd // indirect
67-
github.com/google/subcommands v1.2.0 // indirect
6868
github.com/google/uuid v1.6.0 // indirect
6969
github.com/gotomicro/logrotate v0.0.0-20211108034117-46d53eedc960 // indirect
7070
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
@@ -93,7 +93,6 @@ require (
9393
github.com/modern-go/reflect2 v1.0.2 // indirect
9494
github.com/pelletier/go-toml/v2 v2.2.0 // indirect
9595
github.com/pierrec/lz4/v4 v4.1.21 // indirect
96-
github.com/pkg/errors v0.9.1 // indirect
9796
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
9897
github.com/prometheus/client_golang v1.12.1 // indirect
9998
github.com/prometheus/client_model v0.2.0 // indirect
@@ -126,11 +125,9 @@ require (
126125
golang.org/x/arch v0.7.0 // indirect
127126
golang.org/x/crypto v0.23.0 // indirect
128127
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
129-
golang.org/x/mod v0.14.0 // indirect
130128
golang.org/x/net v0.25.0 // indirect
131129
golang.org/x/sys v0.20.0 // indirect
132130
golang.org/x/text v0.15.0 // indirect
133-
golang.org/x/tools v0.17.0 // indirect
134131
google.golang.org/genproto/googleapis/api v0.0.0-20230711160842-782d3b101e98 // indirect
135132
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
136133
google.golang.org/grpc v1.58.1 // indirect

go.sum

-3
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,6 @@ github.com/google/pprof v0.0.0-20200708004538-1a94d8640e99/go.mod h1:ZgVRPoUq/hf
290290
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd h1:1FjCyPC+syAzJ5/2S8fqdZK1R22vvA0J7JZKcuOIQ7Y=
291291
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd/go.mod h1:KgnwoLYCZ8IQu3XUZ8Nc/bM9CCZFOyjUNOSygVozoDg=
292292
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
293-
github.com/google/subcommands v1.2.0 h1:vWQspBTo2nEqTUFita5/KeEWlUL8kQObDFbub/EN9oE=
294293
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
295294
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
296295
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
@@ -785,7 +784,6 @@ golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
785784
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
786785
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
787786
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
788-
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
789787
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
790788
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
791789
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@@ -1004,7 +1002,6 @@ golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc
10041002
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
10051003
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
10061004
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
1007-
golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc=
10081005
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=
10091006
golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
10101007
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

internal/pkg/middleware/add_appid_builder.go

+22-7
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,53 @@ package middleware
22

33
import (
44
"context"
5+
"net/http"
6+
"strconv"
7+
58
"github.com/ecodeclub/ginx"
69
"github.com/gin-gonic/gin"
710
"github.com/gotomicro/ego/core/elog"
8-
"net/http"
9-
"strconv"
1011
)
1112

1213
type AddAppIdBuilder struct {
1314
}
15+
16+
type AppContextType string
17+
1418
const (
15-
appCtxKey = "app"
19+
AppCtxKey AppContextType = "app"
1620
)
1721

18-
func NewAddAppIdBuilder()*AddAppIdBuilder {
22+
func NewAddAppIdBuilder() *AddAppIdBuilder {
1923
return &AddAppIdBuilder{}
2024
}
2125
func (a *AddAppIdBuilder) Build() gin.HandlerFunc {
2226
return func(ctx *gin.Context) {
2327
gctx := &ginx.Context{Context: ctx}
24-
appid := ctx.GetHeader(appCtxKey)
28+
appid := ctx.GetHeader(string(AppCtxKey))
2529
if appid != "" {
2630
c := ctx.Request.Context()
27-
app,err := strconv.Atoi(appid)
31+
app, err := strconv.Atoi(appid)
2832
if err != nil {
2933
gctx.AbortWithStatus(http.StatusBadRequest)
3034
elog.Error("appid设置失败", elog.FieldErr(err))
3135
return
3236
}
33-
newCtx := context.WithValue(c,appCtxKey,uint(app))
37+
newCtx := CtxWithAppId(c, uint(app))
3438
ctx.Request = ctx.Request.WithContext(newCtx)
3539
}
3640
}
3741
}
3842

43+
func AppID(ctx context.Context) (uint, bool) {
44+
app := ctx.Value(AppCtxKey)
45+
if app == nil {
46+
return 0, false
47+
}
48+
v, ok := app.(uint)
49+
return v, ok
50+
}
3951

52+
func CtxWithAppId(ctx context.Context, appid uint) context.Context {
53+
return context.WithValue(ctx, AppCtxKey, appid)
54+
}

internal/pkg/middleware/add_appid_builder_test.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package middleware
22

33
import (
4-
"github.com/gin-gonic/gin"
5-
"github.com/stretchr/testify/assert"
6-
"github.com/stretchr/testify/require"
74
"net/http"
85
"net/http/httptest"
96
"testing"
7+
8+
"github.com/gin-gonic/gin"
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
1011
)
1112

1213
func TestAddAppId(t *testing.T) {
@@ -21,13 +22,13 @@ func TestAddAppId(t *testing.T) {
2122
wantCode: 200,
2223
before: func(t *testing.T, ctx *gin.Context) {
2324
header := make(http.Header)
24-
header.Set(appCtxKey, "1")
25+
header.Set(string(AppCtxKey), "1")
2526
ctx.Request = httptest.NewRequest(http.MethodPost, "/users/profile", nil)
2627
ctx.Request.Header = header
2728
},
2829
afterFunc: func(t *testing.T, ctx *gin.Context) {
2930
c := ctx.Request.Context()
30-
v := c.Value(appCtxKey)
31+
v := c.Value(AppCtxKey)
3132
res, ok := v.(uint)
3233
require.True(t, ok)
3334
assert.Equal(t, uint(1), res)
@@ -43,7 +44,7 @@ func TestAddAppId(t *testing.T) {
4344
},
4445
afterFunc: func(t *testing.T, ctx *gin.Context) {
4546
c := ctx.Request.Context()
46-
v := c.Value(appCtxKey)
47+
v := c.Value(AppCtxKey)
4748
require.Nil(t, v)
4849
},
4950
},
@@ -52,7 +53,7 @@ func TestAddAppId(t *testing.T) {
5253
wantCode: 400,
5354
before: func(t *testing.T, ctx *gin.Context) {
5455
header := make(http.Header)
55-
header.Set(appCtxKey, "dasdsa")
56+
header.Set(string(AppCtxKey), "dasdsa")
5657
ctx.Request = httptest.NewRequest(http.MethodPost, "/users/profile", nil)
5758
ctx.Request.Header = header
5859
},

internal/pkg/snowflake/snowflake.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package snowflake
33
import (
44
"errors"
55
"fmt"
6+
67
"github.com/bwmarrin/snowflake"
78
"github.com/ecodeclub/ekit/syncx"
89
)
@@ -13,7 +14,7 @@ type SnowFlake interface {
1314

1415
type CustomSnowFlake struct {
1516
// 键为appid
16-
nodes syncx.Map[uint, *snowflake.Node]
17+
nodes *syncx.Map[uint, *snowflake.Node]
1718
}
1819

1920
const (
@@ -33,7 +34,7 @@ var (
3334

3435
// node表示第几个节点,appid表示有几个应用 从0开始排序 0-ietls 最多到31
3536
func NewCustomSnowFlake(nodeId uint, apps uint) (*CustomSnowFlake, error) {
36-
nodeMap := syncx.Map[uint, *snowflake.Node]{}
37+
nodeMap := &syncx.Map[uint, *snowflake.Node]{}
3738
if nodeId > maxNode {
3839
return nil, fmt.Errorf("%w", ErrExceedNode)
3940
}

internal/pkg/snowflake/snowflake_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package snowflake
22

33
import (
4+
"testing"
5+
46
"github.com/bwmarrin/snowflake"
57
"github.com/stretchr/testify/assert"
68
"github.com/stretchr/testify/require"
7-
"testing"
89
)
910

1011
func Test_NewGenerate(t *testing.T) {

internal/user/internal/integration/handler_test.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ import (
2020
"context"
2121
"database/sql"
2222
"fmt"
23-
"github.com/ecodeclub/webook/internal/pkg/snowflake"
24-
"github.com/ecodeclub/webook/internal/user"
2523
"net/http"
2624
"testing"
2725

26+
"github.com/ecodeclub/webook/internal/pkg/middleware"
27+
28+
"github.com/ecodeclub/webook/internal/pkg/snowflake"
29+
"github.com/ecodeclub/webook/internal/user"
30+
2831
"github.com/ecodeclub/ekit/sqlx"
2932

3033
permissionmocks "github.com/ecodeclub/webook/internal/permission/mocks"
@@ -1159,7 +1162,7 @@ func (s *HandlerWithAppTestSuite) TestFindOrCreateByWechat() {
11591162
},
11601163
before: func(t *testing.T) {},
11611164
ctx: func() context.Context {
1162-
return context.WithValue(context.Background(), "app", uint(1))
1165+
return middleware.CtxWithAppId(context.Background(), uint(1))
11631166
},
11641167
after: func(t *testing.T) {
11651168
var u dao.User

internal/user/internal/integration/startup/wire_gen.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/user/internal/repository/dao/init.go

+17-5
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,22 @@ func InitTables(db *egorm.Component) error {
1010
if err != nil {
1111
panic(err)
1212
}
13-
db.Callback().Query().Before("*").Register("user_query", NewUserCallBackBuilder().Build())
14-
db.Callback().Delete().Before("*").Register("user_delete", NewUserCallBackBuilder().Build())
15-
db.Callback().Create().Before("*").Register("user_create", insertBuilder.Build())
16-
db.Callback().Update().Before("*").Register("user_update", NewUserCallBackBuilder().Build())
13+
err = db.Callback().Query().Before("*").Register("user_query", NewUserCallBackBuilder().Build())
14+
if err != nil {
15+
panic(err)
16+
}
17+
err = db.Callback().Delete().Before("*").Register("user_delete", NewUserCallBackBuilder().Build())
18+
if err != nil {
19+
panic(err)
20+
}
21+
err = db.Callback().Create().Before("*").Register("user_create", insertBuilder.Build())
22+
if err != nil {
23+
panic(err)
24+
}
25+
err = db.Callback().Update().Before("*").Register("user_update", NewUserCallBackBuilder().Build())
26+
if err != nil {
27+
panic(err)
28+
}
1729
return db.AutoMigrate(
1830
&User{},
1931
&UsersIelts{},
@@ -22,6 +34,6 @@ func InitTables(db *egorm.Component) error {
2234

2335
type UsersIelts User
2436

25-
func (u *UsersIelts ) TableName() string {
37+
func (u *UsersIelts) TableName() string {
2638
return "users_ielts"
2739
}

internal/user/internal/repository/dao/user_callback.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package dao
33
import (
44
"context"
55
"fmt"
6+
7+
"github.com/ecodeclub/webook/internal/pkg/middleware"
8+
69
"github.com/ecodeclub/webook/internal/pkg/snowflake"
710
"github.com/gotomicro/ego/core/elog"
811
"github.com/pkg/errors"
@@ -11,7 +14,6 @@ import (
1114

1215
const (
1316
uidCtxKey = "uid"
14-
appCtxKey = "app"
1517
)
1618

1719
var appMap = map[uint]string{
@@ -114,7 +116,6 @@ func build(db *gorm.DB, logger *elog.Component) {
114116
db.Statement.Table = tableName
115117
return
116118
}
117-
return
118119
}
119120

120121
func appIdFromUserId(ctx context.Context) (uint, bool) {
@@ -136,12 +137,7 @@ func userId(ctx context.Context) (int64, bool) {
136137
}
137138

138139
func appId(ctx context.Context) (uint, bool) {
139-
v := ctx.Value(appCtxKey)
140-
if v == nil {
141-
return 0, false
142-
}
143-
appId, ok := v.(uint)
144-
return appId, ok
140+
return middleware.AppID(ctx)
145141
}
146142

147143
func tableNameFromAppId(appid uint) (string, error) {

internal/user/internal/web/handler.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ package web
1717
import (
1818
"context"
1919
"encoding/json"
20-
"github.com/ecodeclub/webook/internal/pkg/middleware"
2120
"strconv"
2221
"strings"
2322

23+
"github.com/ecodeclub/webook/internal/pkg/middleware"
24+
2425
"github.com/ecodeclub/ekit/slice"
2526
"github.com/ecodeclub/ginx"
2627
"github.com/ecodeclub/ginx/session"

internal/user/types.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ import (
2121

2222
// Handler 暴露出去给 ioc 使用
2323
type Handler = web.Handler
24+
2425
// UserService 方便测试
2526
type UserService = service.UserService
2627

2728
type Module struct {
28-
Hdl *Handler
29-
Svc UserService
29+
Hdl *Handler
30+
Svc UserService
3031
}
3132

3233
// 规避 wire 的坑

0 commit comments

Comments
 (0)