Giter Club home page Giter Club logo

fasthttpsession's Introduction

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 [email protected].

License

MIT

Thanks

Create By [email protected]

fasthttpsession's People

Contributors

lxy1226 avatar phachon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  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.