From 6f5eb5fefd7bc649d86e46f78378fb8104299a49 Mon Sep 17 00:00:00 2001 From: steden <1470804@qq.com> Date: Sun, 24 Dec 2023 13:41:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86Session=E4=BD=9C=E4=B8=BA=E5=AD=90?= =?UTF-8?q?=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- applicationBuilder.go | 7 +++++-- context/httpContext.go | 2 +- context/iHttpSession.go | 12 ++++++++++++ go.mod | 4 ---- iSessionMiddlewareCreat.go | 8 ++++++++ middleware/session.go | 12 ------------ module.go | 6 ------ .../context/httpSession.go | 0 {webapi-session-redis => session-redis}/go.mod | 2 +- .../middleware/session.go | 2 +- {webapi-session-redis => session-redis}/module.go | 4 ++-- .../sessionMiddlewareCreat.go | 6 +++--- 12 files changed, 33 insertions(+), 32 deletions(-) create mode 100644 context/iHttpSession.go create mode 100644 iSessionMiddlewareCreat.go delete mode 100644 middleware/session.go rename {webapi-session-redis => session-redis}/context/httpSession.go (100%) rename {webapi-session-redis => session-redis}/go.mod (66%) rename {webapi-session-redis => session-redis}/middleware/session.go (85%) rename {webapi-session-redis => session-redis}/module.go (79%) rename {webapi-session-redis => session-redis}/sessionMiddlewareCreat.go (64%) diff --git a/applicationBuilder.go b/applicationBuilder.go index 00106c1..6e3ed52 100644 --- a/applicationBuilder.go +++ b/applicationBuilder.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/farseer-go/collections" "github.com/farseer-go/fs/configure" + "github.com/farseer-go/fs/container" "github.com/farseer-go/fs/core/eumLogLevel" "github.com/farseer-go/fs/flog" "github.com/farseer-go/fs/modules" @@ -126,8 +127,10 @@ func (r *applicationBuilder) UsePprof() { // UseSession 开启Session func (r *applicationBuilder) UseSession() { - r.RegisterMiddleware(&middleware.Session{}) - go context.ClearSession() + if !container.IsRegister[ISessionMiddlewareCreat]() { + panic("要使用Session,请加载模块:session-redis") + } + r.RegisterMiddleware(container.Resolve[ISessionMiddlewareCreat]().Create()) } func (r *applicationBuilder) UseWebApi() { diff --git a/context/httpContext.go b/context/httpContext.go index 4c7bf89..7253f0a 100644 --- a/context/httpContext.go +++ b/context/httpContext.go @@ -12,7 +12,7 @@ type HttpContext struct { Response *HttpResponse // Response Header collections.ReadonlyDictionary[string, string] // 头部信息 Cookie *HttpCookies // Cookies信息 - Session *HttpSession // Session信息 + Session IHttpSession // Session信息 Route *HttpRoute // 路由信息 URI *HttpURL // URL信息 Data *HttpData // 用于传递值 diff --git a/context/iHttpSession.go b/context/iHttpSession.go new file mode 100644 index 0000000..b60a03f --- /dev/null +++ b/context/iHttpSession.go @@ -0,0 +1,12 @@ +package context + +type IHttpSession interface { + // GetValue 获取Session + GetValue(name string) any + // SetValue 设置Session + SetValue(name string, val any) + // Remove 删除Session + Remove(name string) + // Clear 清空Session + Clear() +} diff --git a/go.mod b/go.mod index 619e4f6..bdd3501 100644 --- a/go.mod +++ b/go.mod @@ -6,25 +6,21 @@ require ( github.com/farseer-go/cacheMemory v0.10.0 github.com/farseer-go/collections v0.10.0 github.com/farseer-go/fs v0.10.0 - github.com/farseer-go/redis v0.10.0 github.com/go-playground/locales v0.14.1 github.com/go-playground/universal-translator v0.18.1 github.com/go-playground/validator/v10 v10.16.0 github.com/golang-jwt/jwt/v5 v5.2.0 github.com/stretchr/testify v1.8.4 - github.com/valyala/fasthttp v1.51.0 ) require ( github.com/andybalholm/brotli v1.0.5 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect - github.com/devfeel/mapper v0.7.13 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/farseer-go/cache v0.10.0 // indirect github.com/farseer-go/mapper v0.10.0 // indirect github.com/gabriel-vasile/mimetype v1.4.2 // indirect - github.com/go-redis/redis/v8 v8.11.5 // indirect github.com/klauspost/compress v1.17.0 // indirect github.com/kr/pretty v0.3.1 // indirect github.com/leodido/go-urn v1.2.4 // indirect diff --git a/iSessionMiddlewareCreat.go b/iSessionMiddlewareCreat.go new file mode 100644 index 0000000..d62e300 --- /dev/null +++ b/iSessionMiddlewareCreat.go @@ -0,0 +1,8 @@ +package webapi + +import "github.com/farseer-go/webapi/context" + +type ISessionMiddlewareCreat interface { + // Create 创建Session中间件 + Create() context.IMiddleware +} diff --git a/middleware/session.go b/middleware/session.go deleted file mode 100644 index 42bef4f..0000000 --- a/middleware/session.go +++ /dev/null @@ -1,12 +0,0 @@ -package middleware - -import "github.com/farseer-go/webapi/context" - -type Session struct { - context.IMiddleware -} - -func (receiver *Session) Invoke(httpContext *context.HttpContext) { - httpContext.Session = context.InitSession(httpContext.Response.W, httpContext.Request.R) - receiver.IMiddleware.Invoke(httpContext) -} diff --git a/module.go b/module.go index 21d1284..9dc7547 100644 --- a/module.go +++ b/module.go @@ -1,7 +1,6 @@ package webapi import ( - "github.com/farseer-go/fs/configure" "github.com/farseer-go/fs/modules" "github.com/farseer-go/webapi/context" "github.com/farseer-go/webapi/controller" @@ -19,11 +18,6 @@ func (module Module) PreInitialize() { controller.Init() minimal.Init() defaultApi = NewApplicationBuilder() - - sessionTimeout := configure.GetInt("Webapi.Session.Age") - if sessionTimeout > 0 { - context.SessionTimeout = sessionTimeout - } } func (module Module) PostInitialize() { diff --git a/webapi-session-redis/context/httpSession.go b/session-redis/context/httpSession.go similarity index 100% rename from webapi-session-redis/context/httpSession.go rename to session-redis/context/httpSession.go diff --git a/webapi-session-redis/go.mod b/session-redis/go.mod similarity index 66% rename from webapi-session-redis/go.mod rename to session-redis/go.mod index eca9f4d..2f756d2 100644 --- a/webapi-session-redis/go.mod +++ b/session-redis/go.mod @@ -1,4 +1,4 @@ -module github.com/farseer-go/webapi/webapi-session-redis +module github.com/farseer-go/webapi/session-redis go 1.19 diff --git a/webapi-session-redis/middleware/session.go b/session-redis/middleware/session.go similarity index 85% rename from webapi-session-redis/middleware/session.go rename to session-redis/middleware/session.go index a679cbc..52d7a63 100644 --- a/webapi-session-redis/middleware/session.go +++ b/session-redis/middleware/session.go @@ -2,7 +2,7 @@ package middleware import ( webapiContext "github.com/farseer-go/webapi/context" - "github.com/farseer-go/webapi/webapi-session-redis/context" + "github.com/farseer-go/webapi/session-redis/context" ) type Session struct { diff --git a/webapi-session-redis/module.go b/session-redis/module.go similarity index 79% rename from webapi-session-redis/module.go rename to session-redis/module.go index ef3e55b..64304d2 100644 --- a/webapi-session-redis/module.go +++ b/session-redis/module.go @@ -1,9 +1,9 @@ -package webapi_session_redis +package session_redis import ( "github.com/farseer-go/fs/configure" "github.com/farseer-go/fs/modules" - "github.com/farseer-go/webapi/webapi-session-redis/context" + "github.com/farseer-go/webapi/session-redis/context" ) type Module struct { diff --git a/webapi-session-redis/sessionMiddlewareCreat.go b/session-redis/sessionMiddlewareCreat.go similarity index 64% rename from webapi-session-redis/sessionMiddlewareCreat.go rename to session-redis/sessionMiddlewareCreat.go index 9f1db7c..fd01ed9 100644 --- a/webapi-session-redis/sessionMiddlewareCreat.go +++ b/session-redis/sessionMiddlewareCreat.go @@ -1,9 +1,9 @@ -package webapi_session_redis +package session_redis import ( webapiContext "github.com/farseer-go/webapi/context" - "github.com/farseer-go/webapi/webapi-session-redis/context" - "github.com/farseer-go/webapi/webapi-session-redis/middleware" + "github.com/farseer-go/webapi/session-redis/context" + "github.com/farseer-go/webapi/session-redis/middleware" ) type SessionMiddlewareCreat struct {