mirror of
https://github.com/kataras/iris.git
synced 2025-03-15 17:36:29 +01:00
Session DB file storage: https://github.com/kataras/iris/issues/702 example added
Tested on Windows and worked, waiting for more testers. Former-commit-id: 2b5be0202c1124f424d2276948c3203677f26a56
This commit is contained in:
parent
d08c8aa1d6
commit
8a7e718bb1
|
@ -194,7 +194,9 @@ iris session manager lives on its own [package](https://github.com/kataras/iris/
|
||||||
- [Standalone](sessions/standalone/main.go)
|
- [Standalone](sessions/standalone/main.go)
|
||||||
- [Secure Cookie](sessions/securecookie/main.go)
|
- [Secure Cookie](sessions/securecookie/main.go)
|
||||||
- [Flash Messages](sessions/flash-messages/main.go)
|
- [Flash Messages](sessions/flash-messages/main.go)
|
||||||
- [Database](sessions/database/main.go)
|
- [Database](sessions/database)
|
||||||
|
* [Redis](sessions/database/redis/main.go)
|
||||||
|
* [File](sessions/database/file/main.go)
|
||||||
|
|
||||||
> You're free to use your own favourite sessions package if you'd like so.
|
> You're free to use your own favourite sessions package if you'd like so.
|
||||||
|
|
||||||
|
|
64
_examples/sessions/database/file/main.go
Normal file
64
_examples/sessions/database/file/main.go
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/kataras/iris"
|
||||||
|
"github.com/kataras/iris/context"
|
||||||
|
|
||||||
|
"github.com/kataras/iris/sessions"
|
||||||
|
"github.com/kataras/iris/sessions/sessiondb/file"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
db := file.New("./sessions/")
|
||||||
|
|
||||||
|
sess := sessions.New(sessions.Config{Cookie: "sessionscookieid"})
|
||||||
|
|
||||||
|
//
|
||||||
|
// IMPORTANT:
|
||||||
|
//
|
||||||
|
sess.UseDatabase(db)
|
||||||
|
|
||||||
|
// the rest of the code stays the same.
|
||||||
|
app := iris.New()
|
||||||
|
|
||||||
|
app.Get("/", func(ctx context.Context) {
|
||||||
|
ctx.Writef("You should navigate to the /set, /get, /delete, /clear,/destroy instead")
|
||||||
|
})
|
||||||
|
app.Get("/set", func(ctx context.Context) {
|
||||||
|
s := sess.Start(ctx)
|
||||||
|
//set session values
|
||||||
|
s.Set("name", "iris")
|
||||||
|
|
||||||
|
//test if setted here
|
||||||
|
ctx.Writef("All ok session setted to: %s", s.GetString("name"))
|
||||||
|
})
|
||||||
|
|
||||||
|
app.Get("/get", func(ctx context.Context) {
|
||||||
|
// get a specific key, as string, if no found returns just an empty string
|
||||||
|
name := sess.Start(ctx).GetString("name")
|
||||||
|
|
||||||
|
ctx.Writef("The name on the /set was: %s", name)
|
||||||
|
})
|
||||||
|
|
||||||
|
app.Get("/delete", func(ctx context.Context) {
|
||||||
|
// delete a specific key
|
||||||
|
sess.Start(ctx).Delete("name")
|
||||||
|
})
|
||||||
|
|
||||||
|
app.Get("/clear", func(ctx context.Context) {
|
||||||
|
// removes all entries
|
||||||
|
sess.Start(ctx).Clear()
|
||||||
|
})
|
||||||
|
|
||||||
|
app.Get("/destroy", func(ctx context.Context) {
|
||||||
|
//destroy, removes the entire session data and cookie
|
||||||
|
sess.Destroy(ctx)
|
||||||
|
})
|
||||||
|
|
||||||
|
app.Get("/update", func(ctx context.Context) {
|
||||||
|
// updates expire date with a new date
|
||||||
|
sess.ShiftExpiraton(ctx)
|
||||||
|
})
|
||||||
|
|
||||||
|
app.Run(iris.Addr(":8080"))
|
||||||
|
}
|
|
@ -35,12 +35,12 @@ func main() {
|
||||||
ctx.Writef("You should navigate to the /set, /get, /delete, /clear,/destroy instead")
|
ctx.Writef("You should navigate to the /set, /get, /delete, /clear,/destroy instead")
|
||||||
})
|
})
|
||||||
app.Get("/set", func(ctx context.Context) {
|
app.Get("/set", func(ctx context.Context) {
|
||||||
|
s := sess.Start(ctx)
|
||||||
//set session values
|
//set session values
|
||||||
sess.Start(ctx).Set("name", "iris")
|
s.Set("name", "iris")
|
||||||
|
|
||||||
//test if setted here
|
//test if setted here
|
||||||
ctx.Writef("All ok session setted to: %s", sess.Start(ctx).GetString("name"))
|
ctx.Writef("All ok session setted to: %s", s.GetString("name"))
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Get("/get", func(ctx context.Context) {
|
app.Get("/get", func(ctx context.Context) {
|
|
@ -11,6 +11,12 @@ import (
|
||||||
"github.com/kataras/golog"
|
"github.com/kataras/golog"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// PathFileMode for creating the sessions directory path, opening and write the session file.
|
||||||
|
// Defaults to 0666.
|
||||||
|
PathFileMode uint32 = 0666
|
||||||
|
)
|
||||||
|
|
||||||
// Database is the basic file-storage session database.
|
// Database is the basic file-storage session database.
|
||||||
type Database struct {
|
type Database struct {
|
||||||
path string
|
path string
|
||||||
|
@ -22,7 +28,8 @@ func New(path string) *Database {
|
||||||
if lindex != os.PathSeparator && lindex != '/' {
|
if lindex != os.PathSeparator && lindex != '/' {
|
||||||
path += string(os.PathSeparator)
|
path += string(os.PathSeparator)
|
||||||
}
|
}
|
||||||
|
// create directories if necessary
|
||||||
|
os.MkdirAll(path, os.FileMode(PathFileMode))
|
||||||
return &Database{path: path}
|
return &Database{path: path}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,18 +39,32 @@ func (d *Database) sessPath(sid string) string {
|
||||||
|
|
||||||
// Load loads the values to the underline
|
// Load loads the values to the underline
|
||||||
func (d *Database) Load(sid string) (values map[string]interface{}, expireDate *time.Time) {
|
func (d *Database) Load(sid string) (values map[string]interface{}, expireDate *time.Time) {
|
||||||
val, err := ioutil.ReadFile(d.sessPath(sid))
|
sessPath := d.sessPath(sid)
|
||||||
|
f, err := os.OpenFile(sessPath, os.O_RDONLY, os.FileMode(PathFileMode))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
// we don't care if filepath doesn't exists yet, it will be created on Update.
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
val, err := ioutil.ReadAll(f)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
// we don't care if filepath doesn't exists yet, it will be created on Update.
|
||||||
|
golog.Errorf("error while reading the session file's data: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err = DeserializeBytes(val, &values)
|
err = DeserializeBytes(val, &values)
|
||||||
}
|
if err != nil { // we care for this error only
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
golog.Errorf("load error: %v", err)
|
golog.Errorf("load error: %v", err)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// no expiration
|
return // no expiration
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// serialize the values to be stored as strings inside the session file-storage.
|
// serialize the values to be stored as strings inside the session file-storage.
|
||||||
|
@ -62,8 +83,7 @@ func (d *Database) expireSess(sid string) {
|
||||||
|
|
||||||
// Update updates the session file-storage.
|
// Update updates the session file-storage.
|
||||||
func (d *Database) Update(sid string, newValues map[string]interface{}, expireDate *time.Time) {
|
func (d *Database) Update(sid string, newValues map[string]interface{}, expireDate *time.Time) {
|
||||||
now := time.Now()
|
|
||||||
sessPath := d.sessPath(sid)
|
|
||||||
if len(newValues) == 0 { // means delete by call
|
if len(newValues) == 0 { // means delete by call
|
||||||
d.expireSess(sid)
|
d.expireSess(sid)
|
||||||
return
|
return
|
||||||
|
@ -71,6 +91,8 @@ func (d *Database) Update(sid string, newValues map[string]interface{}, expireDa
|
||||||
|
|
||||||
// delete the file on expiration
|
// delete the file on expiration
|
||||||
if expireDate != nil && !expireDate.IsZero() {
|
if expireDate != nil && !expireDate.IsZero() {
|
||||||
|
now := time.Now()
|
||||||
|
|
||||||
if expireDate.Before(now) {
|
if expireDate.Before(now) {
|
||||||
// already expirated, delete it now and return.
|
// already expirated, delete it now and return.
|
||||||
d.expireSess(sid)
|
d.expireSess(sid)
|
||||||
|
@ -79,11 +101,11 @@ func (d *Database) Update(sid string, newValues map[string]interface{}, expireDa
|
||||||
// otherwise set a timer to delete the file automatically
|
// otherwise set a timer to delete the file automatically
|
||||||
afterDur := expireDate.Sub(now)
|
afterDur := expireDate.Sub(now)
|
||||||
time.AfterFunc(afterDur, func() {
|
time.AfterFunc(afterDur, func() {
|
||||||
go os.Remove(sessPath)
|
d.expireSess(sid)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
ioutil.WriteFile(sessPath, serialize(newValues), 0666)
|
ioutil.WriteFile(d.sessPath(sid), serialize(newValues), os.FileMode(PathFileMode))
|
||||||
}
|
}
|
||||||
|
|
||||||
// SerializeBytes serializes the "m" into bytes using gob encoder and and returns the result.
|
// SerializeBytes serializes the "m" into bytes using gob encoder and and returns the result.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user