dynamostore

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

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

Go to latest
Published: Jun 3, 2019 License: MIT Imports: 12 Imported by: 0

README

dynamostore

GoDoc Build Status

AWS dynamoDB store for gorilla sessions. Uses AWS Official Go Library

Installation

go get -u github.com/pratikju/dynamostore

Example

import (
	"github.com/pratikju/dynamostore"
)

// create dynamoDB store
store, err := dynamostore.NewDynamoStore(map[string]string{
	"table":    "mysession",
	"endpoint": "http://localhost:8000", // No need to set this in production
}, []byte("something-very-secret"))
if err != nil {
  // handle error
}

// Get a session.
// Get() always returns a session, even if empty.
session, err := store.Get(r, "session-name")
if err != nil {
  // handle error
}

// Set some session values.
session.Values["name"] = "alice"
session.Values["id"] = 43

// Save the session.
if err := session.Save(r, w); err != nil {
  // handle error
}

// Delete the session
session.Options.MaxAge = -1
if err := session.Save(r, w); err != nil {
  // handle error
}

License

MIT, see the LICENSE.

Documentation

Index

Constants

View Source
const (
	// DefaultDynamoDBTableName is used when no table name is configured explicitly.
	DefaultDynamoDBTableName = "session-backend"
	// DefaultDynamoDBReadCapacity is the default read capacity that is used when none is configured explicitly.
	DefaultDynamoDBReadCapacity = 5
	// DefaultDynamoDBWriteCapacity is the default write capacity that is used when none is configured explicitly.
	DefaultDynamoDBWriteCapacity = 5
	// DefaultDynamoDBRegion is used when no region is configured explicitly.
	DefaultDynamoDBRegion = "us-east-1"
	// DefaultMaxAge is the default max age for session when none is configures explicitly.
	DefaultMaxAge = 86400 * 30
	// DefaultTTLEnabled enables ttl by default for session object as recommended by AWS.
	DefaultTTLEnabled = true
)

Variables

This section is empty.

Functions

This section is empty.

Types

type DynamoStore

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

DynamoStore stores sessions in dynamoDB.

func NewDynamoStore

func NewDynamoStore(config map[string]interface{}, keyPairs ...[]byte) (*DynamoStore, error)

NewDynamoStore creates the dynamoDB store from given configuration config parameters expects the following keys:

1. table for dynamoDB table to store the session. (type: string)

2. read_capacity for read provisioned throughput for dynamoDB table. (type: int64)

3. write_capacity for write provisioned throughput for dynamoDB table. (type: int64)

4. region for aws region where the dynamoDB table will be created. (type: string)

5. endpoint for aws dynamoDB endpoint. (type: string)

6. max_age for maximum age of the session. (type: int64)

7. ttl_enabled for enabling ttl on the table. (type: bool)

If any of the keys is missing or wrong type is provided for the key, corresponding default value for the key will be used.

See https://github.com/gorilla/sessions/blob/master/store.go for detailed information on what keyPairs does.

func (*DynamoStore) Get

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

Get returns a session for the given name after adding it to the registry.

It returns a new session if the sessions doesn't exist. Access IsNew on the session to check if it is an existing session or a new one.

It returns a new session and an error if the session exists but could not be decoded.

func (*DynamoStore) MaxAge

func (s *DynamoStore) 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 (*DynamoStore) New

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

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

The difference between New() and Get() is that calling New() twice will decode the session data twice, while Get() registers and reuses the same

func (*DynamoStore) Save

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

Save adds a single session to the response.

type Session

type Session struct {
	// Identifier for session values
	ID string `json:"id"`
	// Encoded session values
	Data string `json:"data"`
	// Unix timestamp indicating when the session values were modified
	ModifiedAt int64 `json:"modified_at"`
	// TTL field for table
	TTL int64 `json:"ttl"`
}

Session object stored in dynamoDB

Jump to

Keyboard shortcuts

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