dynamodb

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2020 License: MIT Imports: 3 Imported by: 0

README

gin-session-dynamodb

PkgGoDev Build Status

A session store backend for gin-contrib/sessions.

Usage

Import it in your code:

import "github.com/kjmkznr/gin-sessions-dynamodb"

Basic Examples

package main

import (
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/dynamodb"
	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
	dynamodbstore "github.com/kjmkznr/gin-sessions-dynamodb"
)

func main() {
	r := gin.Default()
	sess, err := session.NewSession(&aws.Config{
		Region: aws.String("ap-northeast-1"),
	})
	if err != nil {
		panic(err)
	}

	ddb := dynamodb.New(sess)
	store := dynamodbstore.NewStore(ddb, "SessionTable", []byte("secret"))
	r.Use(sessions.Sessions("mysession", store))

	r.GET("/incr", func(c *gin.Context) {
		sess := sessions.Default(c)
		var count int
		v := sess.Get("count")
		if v == nil {
			count = 0
		} else {
			count = v.(int)
			count++
		}
		sess.Set("count", count)
		err := sess.Save()
		if err != nil {
			println(err.Error())
		}
		c.JSON(200, gin.H{"count": count})
	})
	r.Run(":8000")
}

DynamoDB Table Schema

  • Hash Key: session_id (S)
  • Attributes
    • session_data (S)
    • session_expires_at (N)

Testing

$ docker run -p 8000:8000 -d amazon/dynamodb-local:latest
$ go test

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Store

type Store interface {
	sessions.Store
}

func NewStore

func NewStore(client dynamodbiface.DynamoDBAPI, tableName string, keyPairs ...[]byte) Store

client: DynamoDB client (*dynamodbiface.DynamoDBAPI) tableName: Table name used by the DynamoDB store keyPairs: Keys are defined in pairs to allow key rotation, but the common case is to set a single authentication key and optionally an encryption key.

The first key in a pair is used for authentication and the second for encryption. The encryption key can be set to nil or omitted in the last pair, but the authentication key is required in all pairs.

It is recommended to use an authentication key with 32 or 64 bytes. The encryption key, if set, must be either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256 modes.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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