fasthttpsession

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2021 License: MIT Imports: 12 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

logo

Sourcegraph build godoc license go_Report release powered_by platforms

fasthttpsession is a fast and powerful session package for fasthttp servers

中文文档

Description

fasthttpsession is a session manager for Go. It only supports fasthttp, currently support providers:

  • file
  • memcache
  • memory
  • mysql
  • postgres
  • redis
  • sqlite3

Features

  • Focus on the design of the code architecture and expansion.
  • Provide full session storage.
  • Convenient switching of session storage.
  • Customizable data serialization.
  • Implement concurrent map(ccmap.go) to improve performance.

Install

The only requirement is the Go Programming Language, at least v1.7

$ go get -u github.com/phachon/fasthttpsession
$ go get ./...

Used

Quick Start


// fasthttpsession use memory provider

import (
	"github.com/phachon/fasthttpsession"
	"github.com/phachon/fasthttpsession/memory"
	"github.com/valyala/fasthttp"
	"log"
	"os"
)

// default config
var session = fasthttpsession.NewSession(fasthttpsession.NewDefaultConfig())

func main()  {
	// you must set up provider before use
	err := session.SetProvider("memory", &memory.Config{})
	if err != nil {
		log.Println(err.Error())
		os.Exit(1)
	}
	addr := ":8086"
	log.Println("fasthttpsession example server listen: "+addr)
	
	// fasthttp start listen serve
	err = fasthttp.ListenAndServe(addr, requestHandle)
	if err != nil {
		log.Println("listen server error :"+err.Error())
	}
}

// request handler
func requestHandle(ctx *fasthttp.RequestCtx) {
	// start session
	sessionStore, err := session.Start(ctx)
	if err != nil {
		ctx.SetBodyString(err.Error())
		return
	}
	// must defer sessionStore.save(ctx)
	defer sessionStore.Save(ctx)

	sessionStore.Set("name", "fasthttpsession")

	ctx.SetBodyString(fmt.Sprintf("fasthttpsession setted key name= %s ok", sessionStore.Get("name").(string)))
}

Custom configuration

If you don't want to use the default configuration, please use the following struct custom.

type Config struct {

	// cookie name
	CookieName string
	
	// cookie domain
	Domain string
	
	// If you want to delete the cookie when the browser closes, set it to -1.
	//
	//  0 means no expire, (24 years)
	// -1 means when browser closes
	// >0 is the time.Duration which the session cookies should expire.
	Expires time.Duration
	
	// gc life time(s)
	GCLifetime int64
	
	// session life time(s)
	SessionLifetime int64
	
	// set whether to pass this bar cookie only through HTTPS
	Secure bool
	
	// sessionId is in url query
	SessionIdInURLQuery bool
	
	// sessionName in url query
	SessionNameInUrlQuery string
	
	// sessionId is in http header
	SessionIdInHttpHeader bool
	
	// sessionName in http header
	SessionNameInHttpHeader string
	
	// SessionIdGeneratorFunc should returns a random session id.
	SessionIdGeneratorFunc func() string
	
	// Encode the cookie value if not nil.
	EncodeFunc func(cookieValue string) (string, error)
	
	// Decode the cookie value if not nil.
	DecodeFunc func(cookieValue string) (string, error)
}

Different session provider config, please look at the Config.go the provider name directory.

Documents

Document address: http://godoc.org/github.com/phachon/fasthttpsession

Example

Some Example

Feedback

  • If you like the project, please Start.
  • If you have any problems in the process of use, welcome submit Issue.
  • If you find and solve bug, welcome submit Pull Request.
  • If you want to expand session provider, welcome Fork and merge this rep.
  • If you want to make a friend, welcome send email to phachon@163.com.

License

MIT

Thanks

Create By phachon@163.com

Documentation

Index

Constants

View Source
const (
	BASE64TABLE = "1234567890poiuytreqwasdfghjklmnbvcxzQWERTYUIOPLKJHGFDSAZXCVBNM-_"
)

Variables

This section is empty.

Functions

func NewEncrypt

func NewEncrypt() *encrypt

func Register

func Register(providerName string, provider Provider)

register session provider

func Version

func Version() string

Types

type CCMap

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

Concurrent Map

func NewCCMap

func NewCCMap(sliceNumber int) *CCMap

New slice number ccMap

func NewDefaultCCMap

func NewDefaultCCMap() *CCMap

New default slice number ccMap

func (*CCMap) Clear

func (c *CCMap) Clear()

clear all values

func (*CCMap) Count

func (c *CCMap) Count() int

values count

func (*CCMap) Delete

func (c *CCMap) Delete(key string)

delete by key

func (*CCMap) Get

func (c *CCMap) Get(key string) interface{}

get by key

func (*CCMap) GetAll

func (c *CCMap) GetAll() map[string]interface{}

get all values

func (*CCMap) GetOnce

func (c *CCMap) GetOnce(key string) interface{}

get value by key and delete key

func (*CCMap) GetSliceMap

func (c *CCMap) GetSliceMap(key string) *MapSlice

get slice key

func (*CCMap) IsExist

func (c *CCMap) IsExist(key string) bool

key is exist

func (*CCMap) Keys

func (c *CCMap) Keys() []string

get all keys

func (*CCMap) MGet

func (c *CCMap) MGet(keys ...string) map[string]interface{}

multiple get by keys

func (*CCMap) MSet

func (c *CCMap) MSet(data map[string]interface{})

multiple set

func (*CCMap) Replace

func (c *CCMap) Replace(key string, value interface{})

replace if key exist, update value. if key not exist, insert value.

func (*CCMap) Set

func (c *CCMap) Set(key string, value interface{})

set key value

func (*CCMap) Update

func (c *CCMap) Update(key string, value interface{})

update by key if key exist, update value

type Config

type Config struct {
	// Need store in CCMAP
	NeedStoreInMap bool

	// cookie name
	CookieName string

	// cookie domain
	Domain string

	// If you want to delete the cookie when the browser closes, set it to -1.
	//
	//  0 means no expire, (24 years)
	// -1 means when browser closes
	// >0 is the time.Duration which the session cookies should expire.
	Expires time.Duration

	// gc life time(s)
	GCLifetime int64

	// session life time(s)
	SessionLifetime int64

	// set whether to pass this bar cookie only through HTTPS
	Secure bool

	// sessionId is in url query
	SessionIdInURLQuery bool

	// sessionName in url query
	SessionNameInUrlQuery string

	// sessionId is in http header
	SessionIdInHttpHeader bool

	// sessionName in http header
	SessionNameInHttpHeader string

	// SessionIdGeneratorFunc should returns a random session id.
	SessionIdGeneratorFunc func() string

	// Encode the cookie value if not nil.
	EncodeFunc func(cookieValue string) (string, error)

	// Decode the cookie value if not nil.
	DecodeFunc func(cookieValue string) (string, error)
}

func NewDefaultConfig

func NewDefaultConfig() *Config

new default config

func (*Config) Decode

func (c *Config) Decode(cookieValue string) string

decode cookie value

func (*Config) Encode

func (c *Config) Encode(cookieValue string) string

encode cookie value

func (*Config) SessionIdGenerator

func (c *Config) SessionIdGenerator() string

sessionId generator

type Cookie struct {
}

func NewCookie

func NewCookie() *Cookie

func (*Cookie) Delete

func (c *Cookie) Delete(ctx *fasthttp.RequestCtx, name string)

delete cookie by cookie name

func (*Cookie) Get

func (c *Cookie) Get(ctx *fasthttp.RequestCtx, name string) (value string)

get cookie by name

func (*Cookie) Set

func (c *Cookie) Set(ctx *fasthttp.RequestCtx, name string, value string, domain string, expires time.Duration, secure bool)

response set cookie

type MapSlice

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

Map slice

type Provider

type Provider interface {
	Init(int64, ProviderConfig) error
	NeedGC() bool
	GC()
	ReadStore(string) (SessionStore, error)
	Regenerate(string, string) (SessionStore, error)
	Destroy(string) error
	Count() int
}

type ProviderConfig

type ProviderConfig interface {
	Name() string
}

type Session

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

Session struct

func NewSession

func NewSession(cfg *Config) *Session

return new Session

func (*Session) ChangeCookieName added in v1.0.4

func (s *Session) ChangeCookieName(cookieName string)

ChangeCookieName => Update cookie name

func (*Session) ChangeNeedStoreInMap added in v1.0.4

func (s *Session) ChangeNeedStoreInMap(controlFlg bool)

ChangeNeedStoreInMap => Change store ccmap between : SessionStore <=> ctx

func (*Session) Destroy

func (s *Session) Destroy(ctx *fasthttp.RequestCtx)

destroy session in fasthttp ctx

func (*Session) GetSessionId

func (s *Session) GetSessionId(ctx *fasthttp.RequestCtx) string

get session id 1. get session id by reading from cookie 2. get session id from query 3. get session id from http headers

func (*Session) GetSessionStoreWithCtx added in v1.0.4

func (s *Session) GetSessionStoreWithCtx(ctx *fasthttp.RequestCtx) (sessionStore SessionStore, err error)

func (*Session) Regenerate

func (s *Session) Regenerate(ctx *fasthttp.RequestCtx) (sessionStore SessionStore, err error)

regenerate a session id for this SessionStore

func (*Session) RemoveSessionStoreWithCtx added in v1.0.4

func (s *Session) RemoveSessionStoreWithCtx(ctx *fasthttp.RequestCtx) bool

func (*Session) SetProvider

func (s *Session) SetProvider(providerName string, providerConfig ProviderConfig) error

set session provider and provider config

func (*Session) SetSessionStoreWithCtx added in v1.0.4

func (s *Session) SetSessionStoreWithCtx(ctx *fasthttp.RequestCtx, sessionStore SessionStore)

func (*Session) Start

func (s *Session) Start(ctx *fasthttp.RequestCtx) (sessionStore SessionStore, err error)

session start 1. get sessionId from fasthttp ctx 2. if sessionId is empty, generator sessionId and set response Set-Cookie 3. return session provider store

type SessionStore

type SessionStore interface {
	Save(*fasthttp.RequestCtx) error
	Get(key string) interface{}
	GetAll() map[string]interface{}
	Set(key string, value interface{})
	Delete(key string)
	Flush()
	GetSessionId() string
}

type Store

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

func (*Store) Delete

func (s *Store) Delete(key string)

delete data by key

func (*Store) Flush

func (s *Store) Flush()

flush all data

func (*Store) Get

func (s *Store) Get(key string) interface{}

get data by key

func (*Store) GetAll

func (s *Store) GetAll() map[string]interface{}

get all data

func (*Store) GetSessionId

func (s *Store) GetSessionId() string

get session id

func (*Store) Init

func (s *Store) Init(sessionId string, data map[string]interface{})

init store data and sessionId

func (*Store) Set

func (s *Store) Set(key string, value interface{})

set data

Directories

Path Synopsis
_examples
file command
memcache command
memory command
mysql command
redis command
sqlite3 command

Jump to

Keyboard shortcuts

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