surrealstore

package module
v0.0.0-...-2609d4d Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2025 License: MIT Imports: 9 Imported by: 0

README

surrealstore

A session store backend for gorilla/sessions using SurrealDB.

go get -u github.com/thecomputerm/surrealstore

Example

package examples

import (
	"log"
	"net/http"
	"time"

	"github.com/surrealdb/surrealdb.go"
	"github.com/thecomputerm/surrealstore"
)

// ExampleHandler is an example that displays the usage of PGStore.
func ExampleHandler(w http.ResponseWriter, r *http.Request) {
	db, err := surrealdb.New("ws://localhost:8000")
	if err != nil {
		log.Fatalln("Requires a real instance of surrealdb listening on localhost:8000.")
	}

	if _, err = db.SignIn(&surrealdb.Auth{
		Username: "root",
		Password: "root",
	}); err != nil {
		log.Fatalf("Failed to sign in to surrealdb: %v", err)
	}

	if err = db.Use("default", "auth"); err != nil {
		log.Fatalf("Failed to use correct namespace and db: %v", err)
	}

	// Fetch new store.
	store, err := surrealstore.NewSurrealStore(db, []byte("secret-key"))
	if err != nil {
		log.Fatalf("Failed to create a store: %v", err)
	}

	// Run a background goroutine to clean up expired sessions from the database.
	defer store.StopCleanup(store.Cleanup(time.Minute * 5))

	// Get a session.
	session, err := store.Get(r, "session-key")
	if err != nil {
		log.Fatalf("Error getting session: %v", err)
	}

	// Add a value.
	session.Values["foo"] = "bar"

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

	// Delete session.
	session.Options.MaxAge = -1
	if err = session.Save(r, w); err != nil {
		log.Fatalf("Error saving session: %v", err)
	}
}

To integrate it with Gin, create a store mimicking the postgres driver.

Thanks

I've primarily just snatched and modified the code from pgstore because it was the one gin-sessions was using.

Notes

  • Wrapping the time.Time object in a models.CustomDateTime struct because of this issue.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SurrealSession

type SurrealSession struct {
	ID         *models.RecordID      `json:"id,omitempty"`
	Data       string                `json:"data"`
	CreatedOn  models.CustomDateTime `json:"created_on"`
	ModifiedOn models.CustomDateTime `json:"modified_on"`
	ExpiresOn  models.CustomDateTime `json:"expires_on"`
}

type SurrealStore

type SurrealStore struct {
	Codecs  []securecookie.Codec
	Options *sessions.Options
	DB      *surrealdb.DB
}

func NewSurrealStore

func NewSurrealStore(db *surrealdb.DB, keyPairs ...[]byte) (*SurrealStore, error)

func (*SurrealStore) Cleanup

func (store *SurrealStore) Cleanup(interval time.Duration) (chan<- struct{}, <-chan struct{})

Cleanup runs a background goroutine every interval that deletes expired sessions from the database.

The design is based on https://github.com/yosssi/boltstore

func (*SurrealStore) Close

func (store *SurrealStore) Close()

func (*SurrealStore) Get

func (store *SurrealStore) Get(r *http.Request, name string) (*sessions.Session, error)

func (*SurrealStore) MaxAge

func (db *SurrealStore) MaxAge(age int)

MaxAge sets the maximum age for the store and the underlying cookie implementation. Individual sessions can be deleted by setting Options.MaxAge = -1 for that session.

func (*SurrealStore) MaxLength

func (db *SurrealStore) MaxLength(l int)

MaxLength restricts the maximum length of new sessions to l. If l is 0 there is no limit to the size of a session, use with caution. The default for a new PGStore is 4096.

func (*SurrealStore) New

func (store *SurrealStore) New(r *http.Request, name string) (*sessions.Session, error)

New returns a new session for the given name without adding it to the registry.

func (*SurrealStore) Save

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

Save saves the given session into the database and deletes cookies if needed

func (*SurrealStore) StopCleanup

func (store *SurrealStore) StopCleanup(quit chan<- struct{}, done <-chan struct{})

StopCleanup stops the background cleanup from running.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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