Session
Middleware session provides session management for Macaron Instances.
Installation
go get github.com/go-macaron/sessionUsage
import (
"github.com/go-macaron/session"
"gopkg.in/macaron.v1"
)
func main() {
m := macaron.Classic()
m.Use(macaron.Renderer())
m.Use(session.Sessioner())
m.Get("/", func(sess session.Store) string {
sess.Set("session", "session middleware")
return sess.Get("session").(string)
})
m.Get("/signup", func(ctx *macaron.Context, f *session.Flash) {
f.Success("yes!!!")
f.Error("opps...")
f.Info("aha?!")
f.Warning("Just be careful.")
ctx.HTML(200, "signup")
})
m.Run()
}Pongo2
If you're using pongo2 as template engine, you will use flash in HTML as follows:
Output flash in current response
By default, flash will be only used for the next coming response corresponding to the session, but functions Success, Error, Info and Warning are all accept a second argument to indicate whether output flash in current response or not.
But remember, flash can only be used once no matter which way you use.
Options
session.Sessioner comes with a variety of configuration options(session.Options):
Providers
There are 9 built-in implementations of session provider, you have to import provider driver explicitly except for memory and file providers.
Following are some basic usage examples for providers.
Memory
File
Redis
Memcache
PostgreSQL
Use following SQL to create database(make sure key length matches your Options.IDLength):
MySQL
Use following SQL to create database:
Couchbase
Ledis
Nodb
Implement Provider Interface
In case you need to have your own implementation of session storage and provider, you can implement following two interfaces and take memory provider as a study example.
Last updated
Was this helpful?