Documentation
¶
Index ¶
- Constants
- func DefaultNewSessionId() string
- func Get[T interface{}](c *gin.Context) *T
- func New[T interface{}](c *gin.Context) *T
- func Options(c *gin.Context, options sessions.Options)
- func Save(c *gin.Context) error
- func Sessions[T interface{}](name string, optFns ...func(*Session)) gin.HandlerFunc
- type MockDynamoDBClient
- func (m *MockDynamoDBClient) DeleteItem(ctx context.Context, params *dynamodb.DeleteItemInput, ...) (*dynamodb.DeleteItemOutput, error)
- func (m *MockDynamoDBClient) GetItem(ctx context.Context, params *dynamodb.GetItemInput, ...) (*dynamodb.GetItemOutput, error)
- func (m *MockDynamoDBClient) PutItem(ctx context.Context, params *dynamodb.PutItemInput, ...) (*dynamodb.PutItemOutput, error)
- func (m *MockDynamoDBClient) UpdateItem(ctx context.Context, params *dynamodb.UpdateItemInput, ...) (*dynamodb.UpdateItemOutput, error)
- type Session
- func (s *Session) AddFlash(value interface{}, vars ...string)
- func (s *Session) Clear()
- func (s *Session) Delete(key interface{})
- func (s *Session) Flashes(vars ...string) []interface{}
- func (s *Session) Get(key interface{}) interface{}
- func (s *Session) ID() string
- func (s *Session) Options(options sessions.Options)
- func (s *Session) Save() error
- func (s *Session) Set(key interface{}, val interface{})
Constants ¶
const (
// DefaultKey is the gin context key for Session instance.
DefaultKey = "github.com/nguyengg/go-aws-commons/gin-sessions-dynamodb"
)
Variables ¶
This section is empty.
Functions ¶
func DefaultNewSessionId ¶
func DefaultNewSessionId() string
DefaultNewSessionId creates a new UUID and returns its raw-URL-encoded content.
func Get ¶
Get returns the pointer to the session struct attached to the request.
There are two ways to interact with the session middleware; this is the more type-safe way.
Usage:
type MySession struct { Id string `dynamodbav:"sessionId,hashkey" tableName:"session"` } r := gin.Default() r.Use(Sessions[MySession]("sid")) r.GET("/", func (c *gin.Context) { var s *MySession = Get[MySession](c) })
func New ¶
New always create a new session and return the pointer thereto.
Usage:
type MySession struct { Id string `dynamodbav:"sessionId,hashkey" tableName:"session"` } r := gin.Default() r.Use(Sessions[MySession]("sid")) r.GET("/", func (c *gin.Context) { var s *MySession = New[MySession](c) })
func Options ¶
Options can be used to modify the cookie options for the current session.
If you are not using Default and only use the type-safe Get and New, Options can be used instead of Session.Options.
func Save ¶
Save can be used to save the current session to DynamoDB.
If you are not using Default and only use the type-safe Get and New, Save can be used instead of Session.Save.
func Sessions ¶
func Sessions[T interface{}](name string, optFns ...func(*Session)) gin.HandlerFunc
Sessions creates a gin middleware for managing sessions of struct type T.
The name argument is the name of the cookie that stores the session Id. Type T must have these struct tags:
// Hash key is required, and its type must be a string since only string session Ids are supported. Field string `dynamodbav:"sessionId,hashkey" tableName:"my-table"`
See ddb.Table for more information on how the struct tags are parsed. If type T does not implement the required tags or the tags fail validation, the function will panic.
Types ¶
type MockDynamoDBClient ¶
func (*MockDynamoDBClient) DeleteItem ¶
func (m *MockDynamoDBClient) DeleteItem(ctx context.Context, params *dynamodb.DeleteItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.DeleteItemOutput, error)
func (*MockDynamoDBClient) GetItem ¶
func (m *MockDynamoDBClient) GetItem(ctx context.Context, params *dynamodb.GetItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.GetItemOutput, error)
func (*MockDynamoDBClient) PutItem ¶
func (m *MockDynamoDBClient) PutItem(ctx context.Context, params *dynamodb.PutItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.PutItemOutput, error)
func (*MockDynamoDBClient) UpdateItem ¶
func (m *MockDynamoDBClient) UpdateItem(ctx context.Context, params *dynamodb.UpdateItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.UpdateItemOutput, error)
type Session ¶
type Session struct { // Client is the DynamoDB client for saving session data. // // By default, `config.LoadDefaultConfig` will be used to provide an instance. Client ddb.ManagerAPIClient // ClientOptions is passed to every DynamoDB call. ClientOptions []func(*dynamodb.Options) // NewSessionId is used to create the Id for a new session. // // By default, DefaultNewSessionId is used. NewSessionId func() string // CookieOptions modify the cookie settings. CookieOptions sessions.Options // contains filtered or unexported fields }
Session implements gin sessions.Session in a type-safe way.
func Default ¶
Default returns the Session instance attached to the request.
There are two ways to interact with the session middleware; this is one of them by letting you interact with the Session wrapper.