gsgopg

package module
v0.0.0-...-cf997d2 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2019 License: BSD-3-Clause Imports: 8 Imported by: 0

README

gorilla/sessions go-pg store

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/go-pg/pg"
	"github.com/gorilla/sessions"
	gsgopg "github.com/jozefsukovsky/gorilla-sessions-gopg"
)

var db *pg.DB
var store *gsgopg.GoPgStore

func handler(w http.ResponseWriter, r *http.Request) {
	session, err := store.Get(r, "cookie-key")
	if err != nil {
		log.Fatalf(err.Error())
	}

	if session.Values["foo"] == nil {
		session.Values["foo"] = "bar"
	}

	session.Save(r, w)

	fmt.Fprintf(w, "%v\n", store)
	fmt.Fprintf(w, "Stored value: %s\n", session.Values["foo"])
}

func main() {
	db = pg.Connect(&pg.Options{
		User:     "dbuser",
		Password: "dbpassword",
		Database: "dbname",
	})
	defer db.Close()
	var err error
	store, err = gsgopg.NewGoPgStore(db, []byte("<SecretKey>"))
	store.Options = &sessions.Options{
		MaxAge:   86400,
		HttpOnly: true,
	}
	if err != nil {
		panic(err)
	}
	
	quit := make(chan struct{})
	go store.PeriodicCleanup(1*time.Minute, quit)
	
	http.HandleFunc("/", handler)
	http.ListenAndServe(":1234", nil)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GoPgStore

type GoPgStore struct {
	Codecs  []securecookie.Codec
	Options *sessions.Options
	// contains filtered or unexported fields
}

GoPgStore implements Session store

func NewGoPgStore

func NewGoPgStore(db *pg.DB, keyPairs ...[]byte) (*GoPgStore, error)

func (*GoPgStore) Cleanup

func (s *GoPgStore) Cleanup()

Cleanup deletes all expired sessions

func (*GoPgStore) Delete

func (s *GoPgStore) Delete(r *http.Request, w http.ResponseWriter, session *sessions.Session) error

func (*GoPgStore) Get

func (s *GoPgStore) Get(r *http.Request, name string) (*sessions.Session, error)

func (*GoPgStore) New

func (s *GoPgStore) New(r *http.Request, name string) (*sessions.Session, error)

func (*GoPgStore) PeriodicCleanup

func (s *GoPgStore) PeriodicCleanup(i time.Duration, quit <-chan struct{})

PeriodicCleanup will execute expired sessions deletion per interval

func (*GoPgStore) Save

func (s *GoPgStore) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error

type Session

type Session struct {
	Key    string    `sql:",type:varchar(52),pk"`
	Data   string    `sql:",notnull"`
	Expire time.Time `sql:",default:now()"`
	// contains filtered or unexported fields
}

Session schema

Jump to

Keyboard shortcuts

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