dynamodb

package module
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2026 License: MIT Imports: 14 Imported by: 0

README

gin-session-dynamodb

PkgGoDev

A session store backend for gin-contrib/sessions using AWS DynamoDB.

Requirements

  • Go 1.21+
  • AWS SDK for Go v2

Usage

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

Basic Example

package main

import (
	"context"

	"github.com/aws/aws-sdk-go-v2/config"
	awsdynamodb "github.com/aws/aws-sdk-go-v2/service/dynamodb"
	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
	dynamodbstore "github.com/kjmkznr/gin-sessions-dynamodb/v2"
)

func main() {
	r := gin.Default()

	cfg, err := config.LoadDefaultConfig(context.Background(),
		config.WithRegion("ap-northeast-1"),
	)
	if err != nil {
		panic(err)
	}
	ddb := awsdynamodb.NewFromConfig(cfg)
	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)
		if err := sess.Save(); err != nil {
			println(err.Error())
		}
		c.JSON(200, gin.H{"count": count})
	})
	r.Run(":8000")
}

DynamoDB Table Schema

Attribute Type Role
SessionID String (S) Hash key
Data String (S) gob-encoded session values (base64)
ExpiresAt Number (N) Unix epoch seconds (DynamoDB TTL)

Testing

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

Documentation

Index

Constants

View Source
const (
	// SessionIDHashKeyName is the DynamoDB partition key attribute name for the session ID.
	SessionIDHashKeyName = "session_id"
	// SessionDataKeyName is the attribute name for the gob-encoded session data (stored as Binary).
	SessionDataKeyName = "session_data"
	// SessionExpiresName is the attribute name for the session expiration Unix time (stored as Number).
	SessionExpiresName = "session_expires_at"
)

These attribute names must match the values used by v1 so that existing tables and session items remain compatible after upgrading.

Variables

View Source
var DefaultSessionOptions = &gsessions.Options{
	Path:   "/",
	MaxAge: 86400 * 30,
}

Functions

This section is empty.

Types

type DynamoDBAPI

type DynamoDBAPI interface {
	GetItem(ctx context.Context, in *awsdynamodb.GetItemInput, opts ...func(*awsdynamodb.Options)) (*awsdynamodb.GetItemOutput, error)
	PutItem(ctx context.Context, in *awsdynamodb.PutItemInput, opts ...func(*awsdynamodb.Options)) (*awsdynamodb.PutItemOutput, error)
	DeleteItem(ctx context.Context, in *awsdynamodb.DeleteItemInput, opts ...func(*awsdynamodb.Options)) (*awsdynamodb.DeleteItemOutput, error)
}

DynamoDBAPI is the minimal interface satisfied by *dynamodb.Client from aws-sdk-go-v2.

type Store

type Store interface {
	ginsessions.Store
}

Store is the public interface returned by NewStore.

func NewStore

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

NewStore creates a new DynamoDB-backed session store. client must satisfy DynamoDBAPI (e.g. *dynamodb.Client from aws-sdk-go-v2). keyPairs follow the same convention as gorilla/securecookie: pairs of authentication key (required) and optional encryption key.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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