Skip to content

Commit a96787b

Browse files
authored
Merge pull request #252 from devfeel/develop
For my birthday! Update to Version 1.7.21
2 parents 575fa18 + 53f13a9 commit a96787b

6 files changed

Lines changed: 50 additions & 13 deletions

File tree

consts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package dotweb
33
// Global define
44
const (
55
// Version current version
6-
Version = "1.7.20"
6+
Version = "1.7.21"
77
)
88

99
// Log define

context.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ type (
5454
ViewData() core.ConcurrenceMap
5555
SessionID() string
5656
Session() (state *session.SessionState)
57+
DestorySession() error
5758
Hijack() (*HijackConn, error)
5859
IsHijack() bool
5960
IsWebSocket() bool
@@ -299,7 +300,7 @@ func (ctx *HttpContext) Tools() *Tools {
299300
return ctx.tools
300301
}
301302

302-
// AppSetConfig get appset from config file
303+
// ConfigSet get appset from config file
303304
// update for issue #16 Config file
304305
func (ctx *HttpContext) ConfigSet() core.ReadonlyMap {
305306
return ctx.HttpServer().DotApp.Config.ConfigSet
@@ -317,17 +318,28 @@ func (ctx *HttpContext) ViewData() core.ConcurrenceMap {
317318
// Session get session state in current context
318319
func (ctx *HttpContext) Session() (state *session.SessionState) {
319320
if ctx.httpServer == nil {
320-
// return nil, errors.New("no effective http-server")
321321
panic("no effective http-server")
322322
}
323323
if !ctx.httpServer.SessionConfig().EnabledSession {
324-
// return nil, errors.New("http-server not enabled session")
325324
panic("http-server not enabled session")
326325
}
327326
state, _ = ctx.httpServer.sessionManager.GetSessionState(ctx.sessionID)
328327
return state
329328
}
330329

330+
// DestorySession delete all contents of the session and set the sessionId to empty
331+
func (ctx *HttpContext) DestorySession() error {
332+
if ctx.httpServer != nil {
333+
ctx.Session().Clear()
334+
if err := ctx.HttpServer().sessionManager.RemoveSessionState(ctx.SessionID()); err != nil {
335+
return err
336+
}
337+
ctx.sessionID = ""
338+
ctx.SetCookieValue(session.DefaultSessionCookieName, "", -1)
339+
}
340+
return nil
341+
}
342+
331343
// Hijack make current connection to hijack mode
332344
func (ctx *HttpContext) Hijack() (*HijackConn, error) {
333345
hj, ok := ctx.response.Writer().(http.Hijacker)
@@ -353,7 +365,7 @@ func (ctx *HttpContext) IsEnd() bool {
353365
return ctx.isEnd
354366
}
355367

356-
// Redirect redirect replies to the request with a redirect to url and with httpcode
368+
// Redirect replies to the request with a redirect to url and with httpcode
357369
// default you can use http.StatusFound
358370
func (ctx *HttpContext) Redirect(code int, targetUrl string) error {
359371
return ctx.response.Redirect(code, targetUrl)

example/main.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,19 @@ func Time(ctx dotweb.Context) error {
121121
return nil
122122
}
123123

124+
func LogOut(ctx dotweb.Context) error {
125+
err := ctx.DestorySession()
126+
if err != nil {
127+
ctx.WriteString(err)
128+
return err
129+
}
130+
err = ctx.Redirect(http.StatusMovedPermanently, "index?fromlogout")
131+
if err != nil {
132+
ctx.WriteString(err)
133+
}
134+
return err
135+
}
136+
124137
func OutputTestInfo(ctx dotweb.Context) error {
125138
return ctx.WriteString(ctx.(*testContext).TestInfo)
126139
}
@@ -194,6 +207,7 @@ func InitRoute(server *dotweb.HttpServer) {
194207
server.GET("/redirect", Redirect)
195208
server.POST("/readpost", ReadPost)
196209
server.GET("/pretty", IndexPretty)
210+
server.GET("/logout", LogOut)
197211
//server.Router().RegisterRoute(dotweb.RouteMethod_GET, "/index", IndexReg)
198212
}
199213

session/session.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ func (manager *SessionManager) GetSessionState(sessionId string) (session *Sessi
159159
return session, nil
160160
}
161161

162+
// RemoveSessionState delete the session state associated with a specific session ID
163+
func (manager *SessionManager) RemoveSessionState(sessionId string) error {
164+
return manager.store.SessionRemove(sessionId)
165+
}
166+
162167
// GC loop gc session data
163168
func (manager *SessionManager) GC() {
164169
num := manager.store.SessionGC()

session/sessionstate.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func NewSessionState(store SessionStore, sessionId string, values map[interface{
3232
return state
3333
}
3434

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

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

5151
}
5252

53-
// Get get value by key in current state
53+
// Get value by key in current state
5454
func (state *SessionState) Get(key interface{}) interface{} {
5555
state.lock.RLock()
5656
defer state.lock.RUnlock()
@@ -60,33 +60,33 @@ func (state *SessionState) Get(key interface{}) interface{} {
6060
return nil
6161
}
6262

63-
// Get get value as string by key in current state
63+
// GetString Get value as string by key in current state
6464
func (state *SessionState) GetString(key interface{}) string {
6565
v := state.Get(key)
6666
return fmt.Sprint(v)
6767
}
6868

69-
// Get get value as int by key in current state
69+
// GetInt Get value as int by key in current state
7070
func (state *SessionState) GetInt(key interface{}) int {
7171
v, _ := strconv.Atoi(state.GetString(key))
7272
return v
7373
}
7474

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

81-
// Remove remove value by key in current state
81+
// Remove value by key in current state
8282
func (state *SessionState) Remove(key interface{}) error {
8383
state.lock.Lock()
8484
defer state.lock.Unlock()
8585
delete(state.values, key)
8686
return nil
8787
}
8888

89-
// Clear clear all values in current store
89+
// Clear delete all values in current store
9090
func (state *SessionState) Clear() error {
9191
state.lock.Lock()
9292
defer state.lock.Unlock()

version.MD

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## dotweb版本记录:
22

3+
####Version 1.7.21
4+
* feature: add SessionManager.RemoveSessionState to delete the session state associated with a specific session ID
5+
* feature: add HttpContext.DestorySession() to delete all contents of the session and set the sessionId to empty
6+
* For my birthday!
7+
* 2023-04-15 16:00 at ShangHai
8+
39
####Version 1.7.20
410
* Bug fix: delete minor unreachable code caused by log.Fatal
511
* Thanks to @Abirdcfly for PR #248

0 commit comments

Comments
 (0)