mirror of
https://github.com/kataras/iris.git
synced 2025-12-20 03:17:04 +00:00
Update to 8.2.0 | BoltDB session database, fix file sessiondb, faster, simpler and improvement Session Database API
Former-commit-id: 4034737a65b78a77277e4283fd9289c17f4a452e
This commit is contained in:
@@ -194,9 +194,10 @@ iris session manager lives on its own [package](https://github.com/kataras/iris/
|
||||
- [Standalone](sessions/standalone/main.go)
|
||||
- [Secure Cookie](sessions/securecookie/main.go)
|
||||
- [Flash Messages](sessions/flash-messages/main.go)
|
||||
- [Database](sessions/database)
|
||||
* [Redis](sessions/database/redis/main.go)
|
||||
- [Databases](sessions/database)
|
||||
* [File](sessions/database/file/main.go)
|
||||
* [BoltDB](sessions/database/boltdb/main.go)
|
||||
* [Redis](sessions/database/redis/main.go)
|
||||
|
||||
> You're free to use your own favourite sessions package if you'd like so.
|
||||
|
||||
|
||||
@@ -16,9 +16,9 @@ func main() {
|
||||
// Prefix: "With", code editors will help you navigate through all
|
||||
// configuration options without even a glitch to the documentation.
|
||||
|
||||
app.Run(iris.Addr(":8080"), iris.WithoutBanner, iris.WithCharset("UTF-8"))
|
||||
app.Run(iris.Addr(":8080"), iris.WithoutStartupLog, iris.WithCharset("UTF-8"))
|
||||
|
||||
// or before run:
|
||||
// app.Configure(iris.WithoutBanner, iris.WithCharset("UTF-8"))
|
||||
// app.Configure(iris.WithoutStartupLog, iris.WithCharset("UTF-8"))
|
||||
// app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
@@ -26,7 +26,6 @@ func Configurator(app *iris.Application) {
|
||||
}()
|
||||
|
||||
app.Get("/counter", func(ctx context.Context) {
|
||||
ctx.Header("Content-Type", "text/plain")
|
||||
ctx.Writef("Counter value = %d", counterValue)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@ func main() {
|
||||
ctx.Writef("The path after /anything is: %s", paramValue)
|
||||
})
|
||||
|
||||
myroute.Name = "myroute"
|
||||
|
||||
// useful for links, although iris' view engine has the {{ urlpath "routename" "path values"}} already.
|
||||
app.Get("/reverse_myroute", func(ctx context.Context) {
|
||||
myrouteRequestPath := rv.Path(myroute.Name, "any/path")
|
||||
@@ -32,4 +34,5 @@ func main() {
|
||||
// http://localhost:8080/execute_myroute
|
||||
// http://localhost:8080/anything/any/path/here
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
} // See view/template_html_4 example for more.
|
||||
|
||||
91
_examples/sessions/database/boltdb/main.go
Normal file
91
_examples/sessions/database/boltdb/main.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
"github.com/kataras/iris/sessions"
|
||||
"github.com/kataras/iris/sessions/sessiondb/boltdb"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db, _ := boltdb.New("./sessions/sessions.db", 0666, "users")
|
||||
|
||||
// close and unlock the database when control+C/cmd+C pressed
|
||||
iris.RegisterOnInterrupt(func() {
|
||||
db.Close()
|
||||
})
|
||||
|
||||
sess := sessions.New(sessions.Config{
|
||||
Cookie: "sessionscookieid",
|
||||
Expires: 1 * time.Minute, // <=0 means unlimited life
|
||||
})
|
||||
|
||||
//
|
||||
// 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("/set/{key}/{value}", func(ctx context.Context) {
|
||||
key, value := ctx.Params().Get("key"), ctx.Params().Get("value")
|
||||
s := sess.Start(ctx)
|
||||
// set session values
|
||||
s.Set(key, value)
|
||||
|
||||
// test if setted here
|
||||
ctx.Writef("All ok session setted to: %s", s.GetString(key))
|
||||
})
|
||||
|
||||
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("/get/{key}", func(ctx context.Context) {
|
||||
// get a specific key, as string, if no found returns just an empty string
|
||||
name := sess.Start(ctx).GetString(ctx.Params().Get("key"))
|
||||
|
||||
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"))
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
@@ -9,9 +11,12 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := file.New("./sessions/")
|
||||
db, _ := file.New("./sessions/", 0666)
|
||||
|
||||
sess := sessions.New(sessions.Config{Cookie: "sessionscookieid"})
|
||||
sess := sessions.New(sessions.Config{
|
||||
Cookie: "sessionscookieid",
|
||||
Expires: 45 * time.Minute, // <=0 means unlimited life
|
||||
})
|
||||
|
||||
//
|
||||
// IMPORTANT:
|
||||
@@ -33,6 +38,16 @@ func main() {
|
||||
ctx.Writef("All ok session setted to: %s", s.GetString("name"))
|
||||
})
|
||||
|
||||
app.Get("/set/{key}/{value}", func(ctx context.Context) {
|
||||
key, value := ctx.Params().Get("key"), ctx.Params().Get("value")
|
||||
s := sess.Start(ctx)
|
||||
// set session values
|
||||
s.Set(key, value)
|
||||
|
||||
// test if setted here
|
||||
ctx.Writef("All ok session setted to: %s", s.GetString(key))
|
||||
})
|
||||
|
||||
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")
|
||||
@@ -40,6 +55,13 @@ func main() {
|
||||
ctx.Writef("The name on the /set was: %s", name)
|
||||
})
|
||||
|
||||
app.Get("/get/{key}", func(ctx context.Context) {
|
||||
// get a specific key, as string, if no found returns just an empty string
|
||||
name := sess.Start(ctx).GetString(ctx.Params().Get("key"))
|
||||
|
||||
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")
|
||||
|
||||
Reference in New Issue
Block a user