session

package module
v2.4.0+incompatible Latest Latest
Warning

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

Go to latest
Published: May 10, 2018 License: MIT Imports: 14 Imported by: 0

README

session

A efficient, safely and easy-to-use session library for Go.

Build Coverage ReportCard GoDoc License

Quick Start

Download and install
$ go get -u -v gopkg.in/session.v2
Create file server.go
package main

import (
	"context"
	"fmt"
	"net/http"

	"gopkg.in/session.v2"
)

func main() {
	session.InitManager(
		session.SetCookieName("session_id"),
		session.SetSign([]byte("sign")),
	)

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		store, err := session.Start(context.Background(), w, r)
		if err != nil {
			fmt.Fprint(w, err)
			return
		}

		store.Set("foo", "bar")
		err = store.Save()
		if err != nil {
			fmt.Fprint(w, err)
			return
		}

		http.Redirect(w, r, "/foo", 302)
	})

	http.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
		store, err := session.Start(context.Background(), w, r)
		if err != nil {
			fmt.Fprint(w, err)
			return
		}

		foo, ok := store.Get("foo")
		if ok {
			fmt.Fprintf(w, "foo:%s", foo)
			return
		}
		fmt.Fprint(w, "does not exist")
	})

	http.ListenAndServe(":8080", nil)
}
Build and run
$ go build server.go
$ ./server
Open in your web browser

http://localhost:8080

foo:bar

Features

  • Easy to use
  • Multi-storage support
  • More secure, signature-based tamper-proof
  • Context support

Store Implementations

MIT License

Copyright (c) 2018 Lyric

Documentation

Overview

Package session implements a efficient, safely and easy-to-use session library for Go.

Example:

package main

import (
	"context"
	"fmt"
	"net/http"

	"gopkg.in/session.v2"
)

func main() {
	session.InitManager(
		session.SetCookieName("session_id"),
		session.SetSign([]byte("sign")),
	)

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		store, err := session.Start(context.Background(), w, r)
		if err != nil {
			fmt.Fprint(w, err)
			return
		}

		store.Set("foo", "bar")
		err = store.Save()
		if err != nil {
			fmt.Fprint(w, err)
			return
		}

		http.Redirect(w, r, "/foo", 302)
	})

	http.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
		store, err := session.Start(context.Background(), w, r)
		if err != nil {
			fmt.Fprint(w, err)
			return
		}

		foo, ok := store.Get("foo")
		if ok {
			fmt.Fprintf(w, "foo:%s", foo)
			return
		}
		fmt.Fprint(w, "does not exist")
	})

	http.ListenAndServe(":8080", nil)
}

Open in your web browser at http://localhost:8080

Output:

foo:bar

Learn more at https://github.com/go-session/session

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidSessionID invalid session id
	ErrInvalidSessionID = errors.New("invalid session id")
)

Functions

func Destroy

func Destroy(ctx context.Context, w http.ResponseWriter, r *http.Request) error

Destroy Destroy a session

func FromReqContext

func FromReqContext(ctx context.Context) (*http.Request, bool)

FromReqContext returns the Request value stored in ctx, if any.

func FromResContext

func FromResContext(ctx context.Context) (http.ResponseWriter, bool)

FromResContext returns the ResponseWriter value stored in ctx, if any.

func InitManager

func InitManager(opt ...Option)

InitManager Initialize the global session management instance

Types

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager A session management instance, including start and destroy operations

func NewManager

func NewManager(opt ...Option) *Manager

NewManager Create a session management instance

func (*Manager) Destroy

func (m *Manager) Destroy(ctx context.Context, w http.ResponseWriter, r *http.Request) error

Destroy a session

func (*Manager) Refresh

func (m *Manager) Refresh(ctx context.Context, w http.ResponseWriter, r *http.Request) (Store, error)

Refresh a session and return to session storage

func (*Manager) Start

func (m *Manager) Start(ctx context.Context, w http.ResponseWriter, r *http.Request) (Store, error)

Start a session and return to session storage

type ManagerStore

type ManagerStore interface {
	// Check the session store exists
	Check(ctx context.Context, sid string) (bool, error)
	// Create a session store and specify the expiration time (in seconds)
	Create(ctx context.Context, sid string, expired int64) (Store, error)
	// Update a session store and specify the expiration time (in seconds)
	Update(ctx context.Context, sid string, expired int64) (Store, error)
	// Delete a session store
	Delete(ctx context.Context, sid string) error
	// Use sid to replace old sid and return session store
	Refresh(ctx context.Context, oldsid, sid string, expired int64) (Store, error)
	// Close storage, release resources
	Close() error
}

ManagerStore Management of session storage, including creation, update, and delete operations

func NewFileStore

func NewFileStore(path string) ManagerStore

NewFileStore Create an instance of a file store

func NewMemoryStore

func NewMemoryStore() ManagerStore

NewMemoryStore Create an instance of a memory store

type Option

type Option func(*options)

Option A session parameter options

func SetCookieLifeTime

func SetCookieLifeTime(cookieLifeTime int) Option

SetCookieLifeTime Set the cookie expiration time (in seconds)

func SetCookieName

func SetCookieName(cookieName string) Option

SetCookieName Set the cookie name

func SetDomain

func SetDomain(domain string) Option

SetDomain Set the domain name of the cookie

func SetExpired

func SetExpired(expired int64) Option

SetExpired Set session expiration time (in seconds)

func SetSecure

func SetSecure(secure bool) Option

SetSecure Set cookie security

func SetSessionID

func SetSessionID(sessionID func() string) Option

SetSessionID Set callback function to generate session id

func SetSign

func SetSign(sign []byte) Option

SetSign Set the session id signature value

func SetStore

func SetStore(store ManagerStore) Option

SetStore Set session management storage

type Store

type Store interface {
	// Get a session storage context
	Context() context.Context
	// Get the current session id
	SessionID() string
	// Set session value, call save function to take effect
	Set(key string, value interface{})
	// Get session value
	Get(key string) (interface{}, bool)
	// Delete session value, call save function to take effect
	Delete(key string) interface{}
	// Save session data
	Save() error
	// Clear all session data
	Flush() error
}

Store A session id storage operation

func Refresh

func Refresh(ctx context.Context, w http.ResponseWriter, r *http.Request) (Store, error)

Refresh a session and return to session storage

func Start

Start Start a session and return to session storage

Jump to

Keyboard shortcuts

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