Skip to content

Commit

Permalink
ginx 支持新版的 session 设计,实验特性
Browse files Browse the repository at this point in the history
  • Loading branch information
flycash committed Feb 11, 2024
1 parent 1c17895 commit 6518825
Show file tree
Hide file tree
Showing 31 changed files with 7,648 additions and 29 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 8 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ ut:

.PHONY: setup
setup:
@sh ./script/setup.sh
@sh ./.script/setup.sh

.PHONY: fmt
fmt:
@sh ./script/goimports.sh
@sh ./.script/goimports.sh

.PHONY: lint
lint:
Expand All @@ -30,12 +30,15 @@ check:
# e2e 测试
.PHONY: e2e
e2e:
sh ./script/integrate_test.sh
sh ./.script/integrate_test.sh

.PHONY: e2e_up
e2e_up:
docker compose -f script/integration_test_compose.yml up -d
docker compose -f .script/integration_test_compose.yml up -d

.PHONY: e2e_down
e2e_down:
docker compose -f script/integration_test_compose.yml down
docker compose -f .script/integration_test_compose.yml down
mock:
mockgen -package=mocks -destination=internal/mocks/pipeline.mock.go github.com/redis/go-redis/v9 Pipeliner
mockgen -source=session/types.go -package=session -destination=session/provider.mock_test.go Provider
30 changes: 30 additions & 0 deletions gctx/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package gctx

import (
"github.com/ecodeclub/ekit"
"github.com/gin-gonic/gin"
)

type Context struct {
*gin.Context
}

func (c *Context) Param(key string) ekit.AnyValue {
return ekit.AnyValue{
Val: c.Context.Param(key),
}
}

func (c *Context) Query(key string) ekit.AnyValue {
return ekit.AnyValue{
Val: c.Context.Query(key),
}
}

func (c *Context) Cookie(key string) ekit.AnyValue {
val, err := c.Context.Cookie(key)
return ekit.AnyValue{
Val: val,
Err: err,
}
}
152 changes: 152 additions & 0 deletions gctx/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package gctx

import (
"net/http"
"net/http/httptest"
"net/url"
"testing"

"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestContext_Query(t *testing.T) {
testCases := []struct {
name string
req func(t *testing.T) *http.Request
key string
wantErr error
wantVal any
}{
{
name: "获得数据",
req: func(t *testing.T) *http.Request {
req, err := http.NewRequest(http.MethodGet, "http://localhost/abc?name=123&age=18", nil)
require.NoError(t, err)
return req
},
key: "name",
wantVal: "123",
},
{
name: "没有数据",
req: func(t *testing.T) *http.Request {
req, err := http.NewRequest(http.MethodGet, "http://localhost/abc?name=123&age=18", nil)
require.NoError(t, err)
return req
},
key: "nickname",
wantVal: "",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ctx := &Context{Context: &gin.Context{
Request: tc.req(t),
}}
name := ctx.Query(tc.key)
val, err := name.String()
assert.Equal(t, tc.wantErr, err)
assert.Equal(t, tc.wantVal, val)
})
}
}

func TestContext_Param(t *testing.T) {
testCases := []struct {
name string
req func(t *testing.T) *http.Request
key string
wantErr error
wantVal any
}{
{
name: "获得数据",
req: func(t *testing.T) *http.Request {
req, err := http.NewRequest(http.MethodGet, "http://localhost/hello?name=123&age=18", nil)
req.Form = url.Values{}
req.Form.Set("name", "world")
require.NoError(t, err)
return req
},
key: "name",
wantVal: "world",
},
{
name: "没有数据",
req: func(t *testing.T) *http.Request {
req, err := http.NewRequest(http.MethodPost, "http://localhost/hello?name=123&age=18", nil)
require.NoError(t, err)
return req
},
key: "nickname",
wantVal: "",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
server := gin.Default()
server.POST("/hello", func(context *gin.Context) {
ctx := &Context{Context: context}
name := ctx.Param(tc.key)
val, err := name.String()
assert.Equal(t, tc.wantErr, err)
assert.Equal(t, tc.wantVal, val)
})
recorder := httptest.NewRecorder()
server.ServeHTTP(recorder, tc.req(t))
})
}
}

func TestContext_Cookie(t *testing.T) {
testCases := []struct {
name string
req func(t *testing.T) *http.Request
key string
wantErr error
wantVal any
}{
{
name: "有cookie",
req: func(t *testing.T) *http.Request {
req, err := http.NewRequest(http.MethodGet, "http://localhost/hello?name=123&age=18", nil)
req.AddCookie(&http.Cookie{
Name: "name",
Value: "world",
})
require.NoError(t, err)
return req
},
key: "name",
wantVal: "world",
},
{
name: "没有 cookie",
req: func(t *testing.T) *http.Request {
req, err := http.NewRequest(http.MethodPost, "http://localhost/hello?name=123&age=18", nil)
require.NoError(t, err)
return req
},
key: "nickname",
wantVal: "",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
server := gin.Default()
server.POST("/hello", func(context *gin.Context) {
ctx := &Context{Context: context}
name := ctx.Param(tc.key)
val, err := name.String()
assert.Equal(t, tc.wantErr, err)
assert.Equal(t, tc.wantVal, val)
})
recorder := httptest.NewRecorder()
server.ServeHTTP(recorder, tc.req(t))
})
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/ecodeclub/ekit v0.0.8
github.com/gin-gonic/gin v1.9.1
github.com/golang-jwt/jwt/v5 v5.0.0
github.com/google/uuid v1.6.0
github.com/redis/go-redis/v9 v9.2.1
github.com/stretchr/testify v1.8.4
go.uber.org/atomic v1.11.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
Expand Down
6 changes: 6 additions & 0 deletions internal/errs/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package errs

import "errors"

var ErrUnauthorized = errors.New("未授权")
var ErrSessionKeyNotFound = errors.New("session 中没找到对应的 key")
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion jwt/management.go → internal/jwt/management.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ func (m *Management[T]) GenerateAccessToken(data T) (string, error) {
ID: m.accessJWTOptions.genIDFn(),
},
}

token := jwt.NewWithClaims(m.accessJWTOptions.Method, claims)
return token.SignedString([]byte(m.accessJWTOptions.EncryptionKey))
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion jwt/types.go → internal/jwt/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Manager[T any] interface {

// Refresh 刷新 token 的 gin.HandlerFunc.
// 需要设置 refreshJWTOptions 否则会出现 500 的 http 状态码.
Refresh(ctx *gin.Context)
//Refresh(ctx *gin.Context)

// GenerateAccessToken 生成资源 token.
GenerateAccessToken(data T) (string, error)
Expand Down
Loading

0 comments on commit 6518825

Please sign in to comment.