session

package module
v0.0.0-...-26ecb68 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 4, 2020 License: MIT Imports: 4 Imported by: 7

README

Session

Session Management for QOR

It wrapped other libs like SCS, Gorilla Session into a common interface, which will be used for QOR libs and your application.

Basic Usage

import (
	"github.com/gorilla/sessions"
	"github.com/ecletus/session/gorilla"
	// "github.com/alexedwards/scs/engine/memstore"
)

var SessionManager = session.ManagerInterface

func main() {
	// Use gorilla session as the backend
	engine := sessions.NewCookieStore([]byte("something-very-secret"))
	SessionManager = gorilla.New("_session", engine)
	// Use SCS as the backend
	// engine := memstore.New(0)
	// SessionManager := scs.New(engine)

	mux := http.NewServeMux()
	mux.HandleFunc("/put", putHandler)
	mux.HandleFunc("/get", getHandler)
	// Your routes

	// Wrap your application's handlers or router with session manager's middleware
	http.ListenAndServe(":7000", manager.Middleware(mux))
}

func putHandler(w http.ResponseWriter, req *http.Request) {
	// Store a key and associated value into session data
	SessionManager.Add(w, req, "key", "value")
}

func getHandler(w http.ResponseWriter, req *http.Request) {
	// Get saved session data with key
	value := SessionManager.Get(req, "key")
	io.WriteString(w, value)
}

Session Manager's Interface

type ManagerInterface interface {
	// Add value to session data, if value is not string, will marshal it into JSON encoding and save it into session data.
	Add(w http.ResponseWriter, req *http.Request, key string, value interface{}) error

	// Get value from session data
	Get(req *http.Request, key string) string

	// Pop value from session data
	Pop(w http.ResponseWriter, req *http.Request, key string) string

	// Flash add flash message to session data
	Flash(w http.ResponseWriter, req *http.Request, message Message) error

	// Flashes returns a slice of flash messages from session data
	Flashes(w http.ResponseWriter, req *http.Request) []Message

	// Load get value from session data and unmarshal it into result
	Load(req *http.Request, key string, result interface{}) error

	// PopLoad pop value from session data and unmarshal it into result
	PopLoad(w http.ResponseWriter, req *http.Request, key string, result interface{}) error

	// Middleware returns a new session manager middleware instance.
	Middleware(http.Handler) http.Handler
}

QOR Integration

We have created a default session manager in package github.com/ecletus/session/manager, which is used in some QOR libs like QOR Admin, QOR Auth by default to manage session, flash messages.

It is defined like below:

var SessionManager session.ManagerInterface = gorilla.New("_session", sessions.NewCookieStore([]byte("secret")))

You should change it to your own session storage or use your own secret code.

import (
	"github.com/ecletus/session/manager"
)

func main() {
	// Overwrite session manager
	engine := sessions.NewCookieStore([]byte("your-own-secret-code"))
	manager.SessionManager = gorilla.New("_gorilla_session", engine)
}

License

Released under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ManagerInterface

type ManagerInterface interface {
	// Add value to session data, if value is not string, will marshal it into JSON encoding and save it into session data.
	Add(w http.ResponseWriter, req *http.Request, key string, value interface{}) error
	// Get value from session data
	Get(req *http.Request, key string) string
	// Pop value from session data
	Pop(w http.ResponseWriter, req *http.Request, key string) string

	// Flash add flash message to session data
	Flash(w http.ResponseWriter, req *http.Request, message Message) error
	// Flashes returns a slice of flash messages from session data
	Flashes(w http.ResponseWriter, req *http.Request) []Message

	// Load get value from session data and unmarshal it into result
	Load(req *http.Request, key string, result interface{}) error
	// PopLoad pop value from session data and unmarshal it into result
	PopLoad(w http.ResponseWriter, req *http.Request, key string, result interface{}) error

	// Middleware returns a new session manager middleware instance.
	Middleware(http.Handler) http.Handler
}

ManagerInterface session manager interface

type Message

type Message struct {
	Message template.HTML
	Type    string
}

Message message struct

func TranslatedMessage

func TranslatedMessage(ctx i18nmod.Context, msg interface{}, typ string) Message

func TranslatedMessageE

func TranslatedMessageE(ctx i18nmod.Context, msg interface{}) Message

type RequestSessionManager

type RequestSessionManager interface {
	// Add value to session data, if value is not string, will marshal it into JSON encoding and save it into session data.
	Add(key string, value interface{}) error
	// Get value from session data
	Get(key string) string
	// Pop value from session data
	Pop(key string) string

	// Flash add flash message to session data
	Flash(message Message) error
	// Flashes returns a slice of flash messages from session data
	Flashes() []Message

	// Load get value from session data and unmarshal it into result
	Load(key string, result interface{}) error
	// PopLoad pop value from session data and unmarshal it into result
	PopLoad(key string, result interface{}) error

	Middleware(http.Handler) http.Handler

	ResponseWriter() http.ResponseWriter

	Request() *http.Request
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL