gosession

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2019 License: MIT Imports: 2 Imported by: 4

README

go-sessions

Go Report Card

A Go wrapper for gorilla sessions http://www.gorillatoolkit.org/pkg/sessions

Installation

$ go get github.com/gorilla/sessions
$ go get github.com/Ulbora/go-sessions

Usage

Initialize and use

    import usess github.com/Ulbora/go-sessions
    var s usess.Session
    var store *sessions.CookieStore
    func main() {
	    s.MaxAge = 5 * 60
	    s.Name = "user-session-test"
	    s.SessionKey = "554dfgdffdd11dfgf1ff1f"
        store = s.InitSessionStore()
    }

    func handleSomething(r http.ResponseWriter, w *http.Request) {	    
        session, err := store.Get(r, s.Name)
	    if err != nil {
		    http.Error(w, err.Error(), http.StatusInternalServerError)
	    }

        //get already set session value        
	    user := session.Values["username"]

        //sets new session values
        session.Values["someVal"] = "someValue"
        session.Values["someOtherVal"] = 55
        //saves new session values
        session.Save(r, w)

        //do something
    }

Important Note:

If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler or else you will leak memory! An easy way to do this is to wrap the top-level mux when calling http.ListenAndServe:

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GoSession

type GoSession struct {
	Path       string
	MaxAge     int
	HTTPOnly   bool
	Secure     bool
	Name       string
	SessionKey string
}

GoSession a GoSession

func (*GoSession) InitSessionStore

func (s *GoSession) InitSessionStore() *gs.CookieStore

InitSessionStore initialize session store

Jump to

Keyboard shortcuts

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