Code Monkey home page Code Monkey logo

session's Introduction

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/qor/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/qor/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/qor/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.

session's People

Contributors

jinzhu avatar

Watchers

James Cloos avatar fredbi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.