Skip to content

Commit

Permalink
####Version 1.7.21
Browse files Browse the repository at this point in the history
* feature: add SessionManager.RemoveSessionState to delete the session state associated with a specific session ID
* feature: add HttpContext.DestorySession() to delete all contents of the session and set the sessionId to empty
* For my birthday!
* 2023-04-15 16:00 at ShangHai
  • Loading branch information
devfeel committed Apr 15, 2023
1 parent 8e442d4 commit 53f13a9
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 13 deletions.
2 changes: 1 addition & 1 deletion consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dotweb
// Global define
const (
// Version current version
Version = "1.7.20"
Version = "1.7.21"
)

// Log define
Expand Down
20 changes: 16 additions & 4 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type (
ViewData() core.ConcurrenceMap
SessionID() string
Session() (state *session.SessionState)
DestorySession() error
Hijack() (*HijackConn, error)
IsHijack() bool
IsWebSocket() bool
Expand Down Expand Up @@ -299,7 +300,7 @@ func (ctx *HttpContext) Tools() *Tools {
return ctx.tools
}

// AppSetConfig get appset from config file
// ConfigSet get appset from config file
// update for issue #16 Config file
func (ctx *HttpContext) ConfigSet() core.ReadonlyMap {
return ctx.HttpServer().DotApp.Config.ConfigSet
Expand All @@ -317,17 +318,28 @@ func (ctx *HttpContext) ViewData() core.ConcurrenceMap {
// Session get session state in current context
func (ctx *HttpContext) Session() (state *session.SessionState) {
if ctx.httpServer == nil {
// return nil, errors.New("no effective http-server")
panic("no effective http-server")
}
if !ctx.httpServer.SessionConfig().EnabledSession {
// return nil, errors.New("http-server not enabled session")
panic("http-server not enabled session")
}
state, _ = ctx.httpServer.sessionManager.GetSessionState(ctx.sessionID)
return state
}

// DestorySession delete all contents of the session and set the sessionId to empty
func (ctx *HttpContext) DestorySession() error {
if ctx.httpServer != nil {
ctx.Session().Clear()
if err := ctx.HttpServer().sessionManager.RemoveSessionState(ctx.SessionID()); err != nil {
return err
}
ctx.sessionID = ""
ctx.SetCookieValue(session.DefaultSessionCookieName, "", -1)
}
return nil
}

// Hijack make current connection to hijack mode
func (ctx *HttpContext) Hijack() (*HijackConn, error) {
hj, ok := ctx.response.Writer().(http.Hijacker)
Expand All @@ -353,7 +365,7 @@ func (ctx *HttpContext) IsEnd() bool {
return ctx.isEnd
}

// Redirect redirect replies to the request with a redirect to url and with httpcode
// Redirect replies to the request with a redirect to url and with httpcode
// default you can use http.StatusFound
func (ctx *HttpContext) Redirect(code int, targetUrl string) error {
return ctx.response.Redirect(code, targetUrl)
Expand Down
14 changes: 14 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ func Time(ctx dotweb.Context) error {
return nil
}

func LogOut(ctx dotweb.Context) error {
err := ctx.DestorySession()
if err != nil {
ctx.WriteString(err)
return err
}
err = ctx.Redirect(http.StatusMovedPermanently, "index?fromlogout")
if err != nil {
ctx.WriteString(err)
}
return err
}

func OutputTestInfo(ctx dotweb.Context) error {
return ctx.WriteString(ctx.(*testContext).TestInfo)
}
Expand Down Expand Up @@ -194,6 +207,7 @@ func InitRoute(server *dotweb.HttpServer) {
server.GET("/redirect", Redirect)
server.POST("/readpost", ReadPost)
server.GET("/pretty", IndexPretty)
server.GET("/logout", LogOut)
//server.Router().RegisterRoute(dotweb.RouteMethod_GET, "/index", IndexReg)
}

Expand Down
5 changes: 5 additions & 0 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ func (manager *SessionManager) GetSessionState(sessionId string) (session *Sessi
return session, nil
}

// RemoveSessionState delete the session state associated with a specific session ID
func (manager *SessionManager) RemoveSessionState(sessionId string) error {
return manager.store.SessionRemove(sessionId)
}

// GC loop gc session data
func (manager *SessionManager) GC() {
num := manager.store.SessionGC()
Expand Down
16 changes: 8 additions & 8 deletions session/sessionstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewSessionState(store SessionStore, sessionId string, values map[interface{
return state
}

// Set set key-value to current state
// Set key-value to current state
func (state *SessionState) reset(store SessionStore, sessionId string, values map[interface{}]interface{}, accessTime time.Time) {
state.values = values
state.sessionId = sessionId
Expand All @@ -41,7 +41,7 @@ func (state *SessionState) reset(store SessionStore, sessionId string, values ma
state.lock = new(sync.RWMutex)
}

// Set set key-value to current state
// Set key-value to current state
func (state *SessionState) Set(key, value interface{}) error {
state.lock.Lock()
defer state.lock.Unlock()
Expand All @@ -50,7 +50,7 @@ func (state *SessionState) Set(key, value interface{}) error {

}

// Get get value by key in current state
// Get value by key in current state
func (state *SessionState) Get(key interface{}) interface{} {
state.lock.RLock()
defer state.lock.RUnlock()
Expand All @@ -60,33 +60,33 @@ func (state *SessionState) Get(key interface{}) interface{} {
return nil
}

// Get get value as string by key in current state
// GetString Get value as string by key in current state
func (state *SessionState) GetString(key interface{}) string {
v := state.Get(key)
return fmt.Sprint(v)
}

// Get get value as int by key in current state
// GetInt Get value as int by key in current state
func (state *SessionState) GetInt(key interface{}) int {
v, _ := strconv.Atoi(state.GetString(key))
return v
}

// Get get value as int64 by key in current state
// GetInt64 Get value as int64 by key in current state
func (state *SessionState) GetInt64(key interface{}) int64 {
v, _ := strconv.ParseInt(state.GetString(key), 10, 64)
return v
}

// Remove remove value by key in current state
// Remove value by key in current state
func (state *SessionState) Remove(key interface{}) error {
state.lock.Lock()
defer state.lock.Unlock()
delete(state.values, key)
return nil
}

// Clear clear all values in current store
// Clear delete all values in current store
func (state *SessionState) Clear() error {
state.lock.Lock()
defer state.lock.Unlock()
Expand Down
6 changes: 6 additions & 0 deletions version.MD
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## dotweb版本记录:

####Version 1.7.21
* feature: add SessionManager.RemoveSessionState to delete the session state associated with a specific session ID
* feature: add HttpContext.DestorySession() to delete all contents of the session and set the sessionId to empty
* For my birthday!
* 2023-04-15 16:00 at ShangHai

####Version 1.7.20
* Bug fix: delete minor unreachable code caused by log.Fatal
* Thanks to @Abirdcfly for PR #248
Expand Down

0 comments on commit 53f13a9

Please sign in to comment.