iris/_benchmarks/iris-sessions/main.go
Gerasimos (Makis) Maropoulos 3945fa68d1 obey the vote of @1370 (77-111 at this point) - add import suffix on iris repository
We have to do the same on iris-contrib/examples, iris-contrib/middleware and e.t.c.


Former-commit-id: 0860688158f374bc137bc934b81b26dcd0e10964
2019-10-25 01:27:02 +03:00

68 lines
1.1 KiB
Go

package main
import (
"time"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/context"
"github.com/kataras/iris/v12/sessions"
)
var sess = sessions.New(sessions.Config{
Cookie: ".cookiesession.id",
Expires: time.Minute,
})
func main() {
app := iris.New()
app.Get("/setget", h)
/*
Test them one by one by these methods:
app.Get("/get", getHandler)
app.Post("/set", postHandler)
app.Delete("/del", delHandler)
*/
app.Run(iris.Addr(":5000"))
}
// Set and Get
func h(ctx context.Context) {
session := sess.Start(ctx)
session.Set("key", "value")
value := session.GetString("key")
if value == "" {
ctx.WriteString("NOT_OK")
return
}
ctx.WriteString(value)
}
// Get
func getHandler(ctx context.Context) {
session := sess.Start(ctx)
value := session.GetString("key")
if value == "" {
ctx.WriteString("NOT_OK")
return
}
ctx.WriteString(value)
}
// Set
func postHandler(ctx context.Context) {
session := sess.Start(ctx)
session.Set("key", "value")
ctx.WriteString("OK")
}
// Delete
func delHandler(ctx context.Context) {
session := sess.Start(ctx)
session.Delete("key")
ctx.WriteString("OK")
}