Categorygithub.com/kevincobain2000/session
modulepackage
2.4.0+incompatible
Repository: https://github.com/kevincobain2000/session.git
Documentation: pkg.go.dev

# 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

# Functions

Destroy Destroy a session.
FromReqContext returns the Request value stored in ctx, if any.
FromResContext returns the ResponseWriter value stored in ctx, if any.
InitManager Initialize the global session management instance.
NewFileStore Create an instance of a file store.
NewManager Create a session management instance.
NewMemoryStore Create an instance of a memory store.
Refresh a session and return to session storage.
SetCookieLifeTime Set the cookie expiration time (in seconds).
SetCookieName Set the cookie name.
SetDomain Set the domain name of the cookie.
SetExpired Set session expiration time (in seconds).
SetSecure Set cookie security.
SetSessionID Set callback function to generate session id.
SetSign Set the session id signature value.
SetStore Set session management storage.
Start Start a session and return to session storage.

# Variables

ErrInvalidSessionID invalid session id.

# Structs

Manager A session management instance, including start and destroy operations.

# Interfaces

ManagerStore Management of session storage, including creation, update, and delete operations.
Store A session id storage operation.

# Type aliases

Option A session parameter options.