Skip to content

Commit

Permalink
将Session作为子模块
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Dec 24, 2023
1 parent e2cf36e commit 6f5eb5f
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 32 deletions.
7 changes: 5 additions & 2 deletions applicationBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion context/httpContext.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 // 用于传递值
Expand Down
12 changes: 12 additions & 0 deletions context/iHttpSession.go
Original file line number Diff line number Diff line change
@@ -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()
}
4 changes: 0 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions iSessionMiddlewareCreat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package webapi

import "github.com/farseer-go/webapi/context"

type ISessionMiddlewareCreat interface {
// Create 创建Session中间件
Create() context.IMiddleware
}
12 changes: 0 additions & 12 deletions middleware/session.go

This file was deleted.

6 changes: 0 additions & 6 deletions module.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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() {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion webapi-session-redis/go.mod → session-redis/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/farseer-go/webapi/webapi-session-redis
module github.com/farseer-go/webapi/session-redis

go 1.19

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions webapi-session-redis/module.go → session-redis/module.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down

0 comments on commit 6f5eb5f

Please sign in to comment.