dynastore

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

README

dynastore

AWS DynamoDB store for Gorilla Toolkit using AWS library.

This is a fork and overhaul of the original repo which hadn't been updated in a long time. I was able to simplify it quite a bit with the v2 API and remove all the serialization code. It also no longer produces output when it encounters errors, it just returns them.

Uses the official AWS library, github.com/aws/aws-sdk-go-v2/aws

Installation
go get github.com/ddouglas/dynastore@v0.1.0
Environment Variables

dynastore uses the common AWS environment variables:

  • AWS_DEFAULT_REGION or AWS_REGION
  • AWS_ACCESS_KEY_ID
  • AWS_SECRET_ACCESS_KEY

dynastore will also use AWS roles if they are available. In that case, only AWS_DEFAULT_REGION or AWS_REGION need be set.

Alternately, AWS settings can be specified using Options:

  • dynastore.AWSConfig(*aws.Config)
  • dynastore.DynamoDB(*dynamodb.DynamoDB)

Example

// Create Store
store, err := dynastore.New("session", dynastore.Path("/"), dynastore.HTTPOnly())
if err != nil {
  log.Fatalln(err)
}

// Get Session
session, err := store.Get(req, "session-key")
if err != nil {
  log.Fatalln(err)
}

// Add a Value
session.Values["hello"] = "world"

// Save Session
err := session.Save(req, w)
if err != nil {
  log.Fatalln(err)
}

// Delete Session
session.Options.MaxAge = -1
err := session.Save(req, w)
if err != nil {
  log.Fatalln(err)
}

Documentation

Overview

Copyright 2017 Matt Ho

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

View Source
const (
	DefaultTableName = "sessions"

	DefaultPrimaryKey = "id"

	// DefaultTTLField contains the default name of the ttl field
	DefaultTTLField = "ttl"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*Store)

Option provides options to creating a dynastore

func Domain

func Domain(v string) Option

Domain sets the default session option of the same name

func DynamoDB

func DynamoDB(ddb *dynamodb.Client) Option

DynamoDB allows a pre-configured dynamodb client to be supplied

func HTTPOnly

func HTTPOnly() Option

HTTPOnly sets the default session option HttpOnly; HTTP is all capped to satisfy golint

func MaxAge

func MaxAge(v int) Option

MaxAge sets the default session option of the same name

func Path

func Path(v string) Option

Path sets the default session option of the same name

func PrimaryKey

func PrimaryKey(key string) Option

func RefreshCookies

func RefreshCookies() Option

func Secure

func Secure() Option

Secure sets the default session option of the same name

func SessionOptions

func SessionOptions(options sessions.Options) Option

SessionOptions allows the default session options to be specified in a single command

func TableName

func TableName(tableName string) Option

TableName allows a custom table name to be specified

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store provides an implementation of the gorilla sessions.Store interface backed by DynamoDB

func New

func New(client *dynamodb.Client, opts ...Option) (*Store, error)

New instantiates a new Store that implements gorilla's sessions.Store interface

func (*Store) Delete

func (store *Store) Delete(ctx context.Context, id string) error

func (*Store) Get

func (store *Store) Get(req *http.Request, name string) (*sessions.Session, error)

Get should return a cached session.

func (*Store) Load

func (store *Store) Load(ctx context.Context, value string, session *sessions.Session) error

load loads a session data from the database. True is returned if there is a session data in the database.

func (*Store) New

func (store *Store) New(req *http.Request, name string) (*sessions.Session, error)

New should create and return a new session.

Note that New should never return a nil session, even in the case of an error if using the Registry infrastructure to cache the session.

func (*Store) Persist

func (store *Store) Persist(ctx context.Context, name string, session *sessions.Session) error

func (*Store) Save

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

Save should persist session to the underlying store implementation.

Jump to

Keyboard shortcuts

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