GoToSocial/vendor/github.com/quasoft/memstore
Tobi Smethurst 98263a7de6
Grand test fixup (#138)
* start fixing up tests

* fix up tests + automate with drone

* fiddle with linting

* messing about with drone.yml

* some more fiddling

* hmmm

* add cache

* add vendor directory

* verbose

* ci updates

* update some little things

* update sig
2021-08-12 21:03:24 +02:00
..
.gitignore Grand test fixup (#138) 2021-08-12 21:03:24 +02:00
.travis.yml Grand test fixup (#138) 2021-08-12 21:03:24 +02:00
LICENSE Grand test fixup (#138) 2021-08-12 21:03:24 +02:00
README.md Grand test fixup (#138) 2021-08-12 21:03:24 +02:00
cache.go Grand test fixup (#138) 2021-08-12 21:03:24 +02:00
memstore.go Grand test fixup (#138) 2021-08-12 21:03:24 +02:00

README.md

memstore

GoDoc Build Status Coverage Status Go Report Card

In-memory implementation of gorilla/sessions for use in tests and dev environments

How to install

go get github.com/quasoft/memstore

Documentation

Documentation, as usual, can be found at godoc.org.

The interface of gorilla/sessions is described at http://www.gorillatoolkit.org/pkg/sessions.

How to use

package main

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

	"github.com/quasoft/memstore"
)

func main() {
	// Create a memory store, providing authentication and
	// encryption key for securecookie
	store := memstore.NewMemStore(
		[]byte("authkey123"),
		[]byte("enckey12341234567890123456789012"),
	)

	http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
		// Get session by name.
		session, err := store.Get(r, "session1")
		if err != nil {
			log.Printf("Error retrieving session: %v", err)
		}

		// The name should be 'foobar' if home page was visited before that and 'Guest' otherwise.
		user, ok := session.Values["username"]
		if !ok {
			user = "Guest"
		}
		fmt.Fprintf(w, "Hello %s", user)
	})

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		// Get session by name.
		session, err := store.Get(r, "session1")
		if err != nil {
			log.Printf("Error retrieving session: %v", err)
		}

		// Add values to the session object
		session.Values["username"] = "foobar"
		session.Values["email"] = "spam@eggs.com"

		// Save values
		err = session.Save(r, w)
		if err != nil {
			log.Fatalf("Error saving session: %v", err)
		}
	})

	log.Printf("listening on http://%s/", "127.0.0.1:9090")
	log.Fatal(http.ListenAndServe("127.0.0.1:9090", nil))
}