Skip to content

jozefsukovsky/gorilla-sessions-gopg

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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)
}

About

go-pg store for gorilla sessions

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages