-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package xingyun | ||
|
||
var ( | ||
SessionKey string = "ZQSESSID" | ||
sessionIDLen int = 36 | ||
) | ||
|
||
func newSessionID() string { | ||
return GenRandString(sessionIDLen) | ||
} | ||
|
||
func (ctx *Context) SetSession(key string, data []byte) { | ||
ctx.Server.SessionStorage.SetSession(ctx.GetSessionID(), key, data) | ||
} | ||
|
||
func (ctx *Context) GetSession(key string) []byte { | ||
return ctx.Server.SessionStorage.GetSession(ctx.GetSessionID(), key) | ||
} | ||
|
||
func (ctx *Context) ClearSession(key string) { | ||
ctx.Server.SessionStorage.ClearSession(ctx.GetSessionID(), key) | ||
} | ||
|
||
func (ctx *Context) setNewSessionID() (sessionID string) { | ||
sessionID = newSessionID() | ||
ctx.SetCookie(SessionKey, sessionID) | ||
return | ||
} | ||
|
||
// SetCookie adds a cookie header to the response. | ||
func (ctx *Context) GetSessionID() (sessionID string) { | ||
cookie, _ := ctx.Request.Cookie(SessionKey) | ||
|
||
if cookie == nil || len(cookie.Value) != sessionIDLen { | ||
return ctx.setNewSessionID() | ||
} | ||
return cookie.Value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package xingyun | ||
|
||
// Simple session storage using memory, handy for development | ||
// **NEVER** use it in production!!! | ||
type memoryStore struct { | ||
data map[string][]byte | ||
} | ||
|
||
func NewMemoryStore() *memoryStore { | ||
return &memoryStore{ | ||
data: make(map[string][]byte), | ||
} | ||
} | ||
|
||
func (ms *memoryStore) SetSession(sessionID string, key string, data []byte) { | ||
ms.data[sessionID+key] = data | ||
} | ||
|
||
func (ms *memoryStore) GetSession(sessionID string, key string) []byte { | ||
data, _ := ms.data[sessionID+key] | ||
return data | ||
} | ||
|
||
func (ms *memoryStore) ClearSession(sessionID string, key string) { | ||
delete(ms.data, sessionID+key) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package xingyun | ||
|
||
//This code is adapted from https://github.com/dustin/randbo | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
// Randbo creates a stream of non-crypto quality random bytes | ||
type randbo struct { | ||
rand.Source | ||
} | ||
|
||
// New creates a new random reader with a time source. | ||
func newRandbo() io.Reader { | ||
return newRandoFrom(rand.NewSource(time.Now().UnixNano())) | ||
} | ||
|
||
// NewFrom creates a new reader from your own rand.Source | ||
func newRandoFrom(src rand.Source) io.Reader { | ||
return &randbo{src} | ||
} | ||
|
||
// Read satisfies io.Reader | ||
func (r *randbo) Read(p []byte) (n int, err error) { | ||
todo := len(p) | ||
offset := 0 | ||
for { | ||
val := int64(r.Int63()) | ||
for i := 0; i < 8; i++ { | ||
p[offset] = byte(val) | ||
todo-- | ||
if todo == 0 { | ||
return len(p), nil | ||
} | ||
offset++ | ||
val >>= 8 | ||
} | ||
} | ||
} | ||
|
||
func GenRandString(length int) string { | ||
buf := make([]byte, length) | ||
newRandbo().Read(buf) | ||
return fmt.Sprintf("%x", buf) | ||
} |