Documentation ¶
Overview ¶
Package storage provides low-level access to storage
Index ¶
- Constants
- func WithDynamoDBClient(client DynamoDBClient) func(*DynamoDBStorage)
- func WithDynamoDBConfig(cfg aws.Config) func(*DynamoDBStorage)
- func WithDynamoDBGSI1Name(gsi1Name string) func(*DynamoDBStorage)
- func WithDynamoDBRegion(region string) func(*DynamoDBStorage)
- func WithDynamoDBTableName(tableName string) func(*DynamoDBStorage)
- type DynamoDBClient
- type DynamoDBStorage
- func (d *DynamoDBStorage) Create(ctx context.Context, shortLink *models.ShortLink) error
- func (s *DynamoDBStorage) CreateTable(ctx context.Context) error
- func (s *DynamoDBStorage) EnsureTable(ctx context.Context) error
- func (d *DynamoDBStorage) GetByID(ctx context.Context, shortLinkID string) (*models.ShortLink, error)
- func (d *DynamoDBStorage) GetByURL(ctx context.Context, linkURL string) ([]*models.ShortLink, error)
- type ErrShortLinkAlreadyExists
- type MemoryStorage
- type Storage
Constants ¶
const ( DynamoDBDefaultTableName = "slink" DynamoDBDefaultGSI1Name = "GSI1" DynamoDBDefaultRegion = "us-east-1" )
Variables ¶
This section is empty.
Functions ¶
func WithDynamoDBClient ¶
func WithDynamoDBClient(client DynamoDBClient) func(*DynamoDBStorage)
func WithDynamoDBConfig ¶
func WithDynamoDBConfig(cfg aws.Config) func(*DynamoDBStorage)
func WithDynamoDBGSI1Name ¶
func WithDynamoDBGSI1Name(gsi1Name string) func(*DynamoDBStorage)
func WithDynamoDBRegion ¶
func WithDynamoDBRegion(region string) func(*DynamoDBStorage)
func WithDynamoDBTableName ¶
func WithDynamoDBTableName(tableName string) func(*DynamoDBStorage)
Types ¶
type DynamoDBClient ¶
type DynamoDBClient interface { PutItem(ctx context.Context, params *dynamodb.PutItemInput, options ...func(*dynamodb.Options)) (*dynamodb.PutItemOutput, error) GetItem(ctx context.Context, params *dynamodb.GetItemInput, options ...func(*dynamodb.Options)) (*dynamodb.GetItemOutput, error) Query(ctx context.Context, params *dynamodb.QueryInput, optFns ...func(*dynamodb.Options)) (*dynamodb.QueryOutput, error) DescribeTable(ctx context.Context, params *dynamodb.DescribeTableInput, optFns ...func(*dynamodb.Options)) (*dynamodb.DescribeTableOutput, error) CreateTable(ctx context.Context, params *dynamodb.CreateTableInput, optFns ...func(*dynamodb.Options)) (*dynamodb.CreateTableOutput, error) }
type DynamoDBStorage ¶
type DynamoDBStorage struct {
// contains filtered or unexported fields
}
DynamoDBStorage implements the Storage interface using Amazon DynamoDB as the backend.
The DynamoDB table can be created elsewhere as long as it has the same structure as used in `CreateTable`. `NewDynamoDBStorage` will create the table automatically if the table doesn’t exist (requires `dynamodb:CreateTable` IAM permission).
The suggested method to supply AWS credentials to the process is by using a dedicated IAM role. DynamoDBStorage will use STS by default when available. You can customise this by using `WithDynamoDBConfig` and supplying your own AWS Config.
The minimum required permissions are: - `dynamodb:PutItem` - `dynamodb:GetItem` - `dynamodb:Query` - `dynamodb:DescribeTable` - `dynamodb:CreateTable` (only if you want the table to be created automatically)
(or check `DynamoDBClient` interface if this list is out of date)
func NewDynamoDBStorage ¶
func NewDynamoDBStorage(ctx context.Context, options ...func(*DynamoDBStorage)) (*DynamoDBStorage, error)
NewDynamoDBStorage returns an initialised `*DynamoDBStorage`.
It checks if the DynamoDB table exists, if not it will create one first. This may fail if the assumed IAM role (or user) doesn't have `dynamodb:CreateTable` permission.
func (*DynamoDBStorage) CreateTable ¶
func (s *DynamoDBStorage) CreateTable(ctx context.Context) error
func (*DynamoDBStorage) EnsureTable ¶
func (s *DynamoDBStorage) EnsureTable(ctx context.Context) error
type ErrShortLinkAlreadyExists ¶
type ErrShortLinkAlreadyExists struct {
ShortLinkID string
}
func (*ErrShortLinkAlreadyExists) Error ¶
func (e *ErrShortLinkAlreadyExists) Error() string
type MemoryStorage ¶
type MemoryStorage struct {
// contains filtered or unexported fields
}
MemoryStorage implements the Storage interface using `sync.Map`s (in-memory hash maps) as the storage backend.
MemoryStorage is intended to be used only in development or testing, NOT in production.
func NewMemoryStorage ¶
func NewMemoryStorage() *MemoryStorage