1
0
mirror of https://github.com/mxpv/podsync.git synced 2024-05-11 05:55:04 +00:00

Configure DynamoDB table names

This commit is contained in:
Maksym Pavlenko
2018-12-08 14:26:50 -08:00
parent 75d7c40fcb
commit e99222745f
5 changed files with 62 additions and 44 deletions

View File

@@ -20,6 +20,8 @@ services:
- PATREON_WEBHOOKS_SECRET={PATREON_WEBHOOKS_SECRET} - PATREON_WEBHOOKS_SECRET={PATREON_WEBHOOKS_SECRET}
- COOKIE_SECRET={COOKIE_SECRET} - COOKIE_SECRET={COOKIE_SECRET}
- GIN_MODE=release - GIN_MODE=release
- DYNAMO_FEEDS_TABLE_NAME=Prod_Feeds
- DYNAMO_PLEDGES_TABLE_NAME=Prod_Pledges
volumes: volumes:
- {PATH_TO_GOOGLE_CREDENTIALS_FILE_FOR_USE_OUTSIDE_GOOGLE_CLOUD}:/google-credentials.json - {PATH_TO_GOOGLE_CREDENTIALS_FILE_FOR_USE_OUTSIDE_GOOGLE_CLOUD}:/google-credentials.json
ytdl: ytdl:

View File

@@ -9,17 +9,19 @@ import (
const FileName = "podsync" const FileName = "podsync"
type AppConfig struct { type AppConfig struct {
YouTubeApiKey string `yaml:"youtubeApiKey"` YouTubeApiKey string `yaml:"youtubeApiKey"`
VimeoApiKey string `yaml:"vimeoApiKey"` VimeoApiKey string `yaml:"vimeoApiKey"`
PatreonClientId string `yaml:"patreonClientId"` PatreonClientId string `yaml:"patreonClientId"`
PatreonSecret string `yaml:"patreonSecret"` PatreonSecret string `yaml:"patreonSecret"`
PatreonRedirectURL string `yaml:"patreonRedirectUrl"` PatreonRedirectURL string `yaml:"patreonRedirectUrl"`
PatreonWebhooksSecret string `json:"patreonWebhooksSecret"` PatreonWebhooksSecret string `json:"patreonWebhooksSecret"`
PostgresConnectionURL string `yaml:"postgresConnectionUrl"` PostgresConnectionURL string `yaml:"postgresConnectionUrl"`
RedisURL string `yaml:"redisUrl"` RedisURL string `yaml:"redisUrl"`
CookieSecret string `yaml:"cookieSecret"` CookieSecret string `yaml:"cookieSecret"`
AssetsPath string `yaml:"assetsPath"` AssetsPath string `yaml:"assetsPath"`
TemplatesPath string `yaml:"templatesPath"` TemplatesPath string `yaml:"templatesPath"`
DynamoFeedsTableName string `yaml:"dynamoFeedsTableName"`
DynamoPledgesTableName string `yaml:"dynamoPledgesTableName"`
} }
func ReadConfiguration() (cfg *AppConfig, err error) { func ReadConfiguration() (cfg *AppConfig, err error) {
@@ -34,17 +36,19 @@ func ReadConfiguration() (cfg *AppConfig, err error) {
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
envmap := map[string]string{ envmap := map[string]string{
"youtubeApiKey": "YOUTUBE_API_KEY", "youtubeApiKey": "YOUTUBE_API_KEY",
"vimeoApiKey": "VIMEO_API_KEY", "vimeoApiKey": "VIMEO_API_KEY",
"patreonClientId": "PATREON_CLIENT_ID", "patreonClientId": "PATREON_CLIENT_ID",
"patreonSecret": "PATREON_SECRET", "patreonSecret": "PATREON_SECRET",
"patreonRedirectUrl": "PATREON_REDIRECT_URL", "patreonRedirectUrl": "PATREON_REDIRECT_URL",
"patreonWebhooksSecret": "PATREON_WEBHOOKS_SECRET", "patreonWebhooksSecret": "PATREON_WEBHOOKS_SECRET",
"postgresConnectionUrl": "POSTGRES_CONNECTION_URL", "postgresConnectionUrl": "POSTGRES_CONNECTION_URL",
"redisUrl": "REDIS_CONNECTION_URL", "redisUrl": "REDIS_CONNECTION_URL",
"cookieSecret": "COOKIE_SECRET", "cookieSecret": "COOKIE_SECRET",
"assetsPath": "ASSETS_PATH", "assetsPath": "ASSETS_PATH",
"templatesPath": "TEMPLATES_PATH", "templatesPath": "TEMPLATES_PATH",
"dynamoFeedsTableName": "DYNAMO_FEEDS_TABLE_NAME",
"dynamoPledgesTableName": "DYNAMO_PLEDGES_TABLE_NAME",
} }
for k, v := range envmap { for k, v := range envmap {

View File

@@ -20,6 +20,8 @@ patreonRedirectUrl: "7"
assetsPath: "8" assetsPath: "8"
templatesPath: "9" templatesPath: "9"
patreonWebhooksSecret: "10" patreonWebhooksSecret: "10"
dynamoFeedsTableName: "11"
dynamoPledgesTableName: "12"
` `
func TestReadYaml(t *testing.T) { func TestReadYaml(t *testing.T) {
@@ -42,6 +44,8 @@ func TestReadYaml(t *testing.T) {
require.Equal(t, "8", cfg.AssetsPath) require.Equal(t, "8", cfg.AssetsPath)
require.Equal(t, "9", cfg.TemplatesPath) require.Equal(t, "9", cfg.TemplatesPath)
require.Equal(t, "10", cfg.PatreonWebhooksSecret) require.Equal(t, "10", cfg.PatreonWebhooksSecret)
require.Equal(t, "11", cfg.DynamoFeedsTableName)
require.Equal(t, "12", cfg.DynamoPledgesTableName)
} }
func TestReadEnv(t *testing.T) { func TestReadEnv(t *testing.T) {
@@ -58,6 +62,8 @@ func TestReadEnv(t *testing.T) {
os.Setenv("ASSETS_PATH", "88") os.Setenv("ASSETS_PATH", "88")
os.Setenv("TEMPLATES_PATH", "99") os.Setenv("TEMPLATES_PATH", "99")
os.Setenv("PATREON_WEBHOOKS_SECRET", "1010") os.Setenv("PATREON_WEBHOOKS_SECRET", "1010")
os.Setenv("DYNAMO_FEEDS_TABLE_NAME", "1111")
os.Setenv("DYNAMO_PLEDGES_TABLE_NAME", "1212")
cfg, err := ReadConfiguration() cfg, err := ReadConfiguration()
require.NoError(t, err) require.NoError(t, err)
@@ -72,4 +78,6 @@ func TestReadEnv(t *testing.T) {
require.Equal(t, "88", cfg.AssetsPath) require.Equal(t, "88", cfg.AssetsPath)
require.Equal(t, "99", cfg.TemplatesPath) require.Equal(t, "99", cfg.TemplatesPath)
require.Equal(t, "1010", cfg.PatreonWebhooksSecret) require.Equal(t, "1010", cfg.PatreonWebhooksSecret)
require.Equal(t, "1111", cfg.DynamoFeedsTableName)
require.Equal(t, "1212", cfg.DynamoPledgesTableName)
} }

View File

@@ -29,8 +29,6 @@ const (
) )
var ( var (
pledgesTableName = aws.String("Pledges")
feedsTableName = aws.String("Feeds")
feedTimeToLiveField = aws.String("ExpirationTime") feedTimeToLiveField = aws.String("ExpirationTime")
feedDowngradeIndexName = aws.String("UserID-HashID-Index") feedDowngradeIndexName = aws.String("UserID-HashID-Index")
) )
@@ -56,7 +54,9 @@ Feeds:
TTL attr: ExpirationTime TTL attr: ExpirationTime
*/ */
type Dynamo struct { type Dynamo struct {
dynamo *dynamodb.DynamoDB dynamo *dynamodb.DynamoDB
FeedsTableName *string
PledgesTableName *string
} }
func NewDynamo(cfg ...*aws.Config) (Dynamo, error) { func NewDynamo(cfg ...*aws.Config) (Dynamo, error) {
@@ -76,7 +76,11 @@ func NewDynamo(cfg ...*aws.Config) (Dynamo, error) {
return Dynamo{}, err return Dynamo{}, err
} }
return Dynamo{dynamo: db}, nil return Dynamo{
dynamo: db,
FeedsTableName: aws.String("Feeds"),
PledgesTableName: aws.String("Pledges"),
}, nil
} }
func (d Dynamo) SaveFeed(feed *model.Feed) error { func (d Dynamo) SaveFeed(feed *model.Feed) error {
@@ -97,7 +101,7 @@ func (d Dynamo) SaveFeed(feed *model.Feed) error {
} }
input := &dynamodb.PutItemInput{ input := &dynamodb.PutItemInput{
TableName: feedsTableName, TableName: d.FeedsTableName,
Item: item, Item: item,
ConditionExpression: aws.String("attribute_not_exists(HashID)"), ConditionExpression: aws.String("attribute_not_exists(HashID)"),
} }
@@ -116,7 +120,7 @@ func (d Dynamo) GetFeed(hashID string) (*model.Feed, error) {
logger.Debug("getting feed") logger.Debug("getting feed")
getInput := &dynamodb.GetItemInput{ getInput := &dynamodb.GetItemInput{
TableName: feedsTableName, TableName: d.FeedsTableName,
Key: map[string]*dynamodb.AttributeValue{ Key: map[string]*dynamodb.AttributeValue{
"HashID": {S: aws.String(hashID)}, "HashID": {S: aws.String(hashID)},
}, },
@@ -158,7 +162,7 @@ func (d Dynamo) GetFeed(hashID string) (*model.Feed, error) {
} }
updateInput := &dynamodb.UpdateItemInput{ updateInput := &dynamodb.UpdateItemInput{
TableName: feedsTableName, TableName: d.FeedsTableName,
Key: getInput.Key, Key: getInput.Key,
UpdateExpression: updateExpression.Update(), UpdateExpression: updateExpression.Update(),
} }
@@ -198,7 +202,7 @@ func (d Dynamo) GetMetadata(hashID string) (*model.Feed, error) {
} }
input := &dynamodb.GetItemInput{ input := &dynamodb.GetItemInput{
TableName: feedsTableName, TableName: d.FeedsTableName,
Key: map[string]*dynamodb.AttributeValue{ Key: map[string]*dynamodb.AttributeValue{
"HashID": {S: aws.String(hashID)}, "HashID": {S: aws.String(hashID)},
}, },
@@ -255,7 +259,7 @@ func (d Dynamo) Downgrade(userID string, featureLevel int) error {
logger.Debug("querying hash ids") logger.Debug("querying hash ids")
queryInput := &dynamodb.QueryInput{ queryInput := &dynamodb.QueryInput{
TableName: feedsTableName, TableName: d.FeedsTableName,
IndexName: feedDowngradeIndexName, IndexName: feedDowngradeIndexName,
KeyConditionExpression: keyConditionExpression.KeyCondition(), KeyConditionExpression: keyConditionExpression.KeyCondition(),
ExpressionAttributeNames: keyConditionExpression.Names(), ExpressionAttributeNames: keyConditionExpression.Names(),
@@ -304,7 +308,7 @@ func (d Dynamo) Downgrade(userID string, featureLevel int) error {
for _, key := range keys { for _, key := range keys {
input := &dynamodb.UpdateItemInput{ input := &dynamodb.UpdateItemInput{
TableName: feedsTableName, TableName: d.FeedsTableName,
Key: key, Key: key,
ConditionExpression: updateExpression.Condition(), ConditionExpression: updateExpression.Condition(),
UpdateExpression: updateExpression.Update(), UpdateExpression: updateExpression.Update(),
@@ -338,7 +342,7 @@ func (d Dynamo) Downgrade(userID string, featureLevel int) error {
for _, key := range keys { for _, key := range keys {
input := &dynamodb.UpdateItemInput{ input := &dynamodb.UpdateItemInput{
TableName: feedsTableName, TableName: d.FeedsTableName,
Key: key, Key: key,
UpdateExpression: updateExpression.Update(), UpdateExpression: updateExpression.Update(),
ExpressionAttributeNames: updateExpression.Names(), ExpressionAttributeNames: updateExpression.Names(),
@@ -370,7 +374,7 @@ func (d Dynamo) AddPledge(pledge *model.Pledge) error {
} }
input := &dynamodb.PutItemInput{ input := &dynamodb.PutItemInput{
TableName: pledgesTableName, TableName: d.PledgesTableName,
Item: item, Item: item,
ConditionExpression: aws.String("attribute_not_exists(PatronID)"), ConditionExpression: aws.String("attribute_not_exists(PatronID)"),
} }
@@ -405,7 +409,7 @@ func (d Dynamo) UpdatePledge(patronID string, pledge *model.Pledge) error {
} }
input := &dynamodb.UpdateItemInput{ input := &dynamodb.UpdateItemInput{
TableName: pledgesTableName, TableName: d.PledgesTableName,
Key: map[string]*dynamodb.AttributeValue{ Key: map[string]*dynamodb.AttributeValue{
pledgesPrimaryKey: {N: aws.String(patronID)}, pledgesPrimaryKey: {N: aws.String(patronID)},
}, },
@@ -432,7 +436,7 @@ func (d Dynamo) DeletePledge(pledge *model.Pledge) error {
logger.Infof("deleting pledge %s", pk) logger.Infof("deleting pledge %s", pk)
input := &dynamodb.DeleteItemInput{ input := &dynamodb.DeleteItemInput{
TableName: pledgesTableName, TableName: d.PledgesTableName,
Key: map[string]*dynamodb.AttributeValue{ Key: map[string]*dynamodb.AttributeValue{
pledgesPrimaryKey: {N: aws.String(pk)}, pledgesPrimaryKey: {N: aws.String(pk)},
}, },
@@ -452,7 +456,7 @@ func (d Dynamo) GetPledge(patronID string) (*model.Pledge, error) {
logger.Debug("getting pledge") logger.Debug("getting pledge")
input := &dynamodb.GetItemInput{ input := &dynamodb.GetItemInput{
TableName: pledgesTableName, TableName: d.PledgesTableName,
Key: map[string]*dynamodb.AttributeValue{ Key: map[string]*dynamodb.AttributeValue{
pledgesPrimaryKey: {N: aws.String(patronID)}, pledgesPrimaryKey: {N: aws.String(patronID)},
}, },

View File

@@ -21,12 +21,12 @@ func createDynamo(t *testing.T) storage {
}) })
require.NoError(t, err) require.NoError(t, err)
d.dynamo.DeleteTable(&dynamodb.DeleteTableInput{TableName: pledgesTableName}) d.dynamo.DeleteTable(&dynamodb.DeleteTableInput{TableName: d.PledgesTableName})
d.dynamo.DeleteTable(&dynamodb.DeleteTableInput{TableName: feedsTableName}) d.dynamo.DeleteTable(&dynamodb.DeleteTableInput{TableName: d.FeedsTableName})
// Create Pledges table // Create Pledges table
_, err = d.dynamo.CreateTable(&dynamodb.CreateTableInput{ _, err = d.dynamo.CreateTable(&dynamodb.CreateTableInput{
TableName: pledgesTableName, TableName: d.PledgesTableName,
AttributeDefinitions: []*dynamodb.AttributeDefinition{ AttributeDefinitions: []*dynamodb.AttributeDefinition{
{ {
AttributeName: aws.String(pledgesPrimaryKey), AttributeName: aws.String(pledgesPrimaryKey),
@@ -49,7 +49,7 @@ func createDynamo(t *testing.T) storage {
// Create Feeds table // Create Feeds table
_, err = d.dynamo.CreateTable(&dynamodb.CreateTableInput{ _, err = d.dynamo.CreateTable(&dynamodb.CreateTableInput{
TableName: feedsTableName, TableName: d.FeedsTableName,
AttributeDefinitions: []*dynamodb.AttributeDefinition{ AttributeDefinitions: []*dynamodb.AttributeDefinition{
{ {
AttributeName: aws.String(feedsPrimaryKey), AttributeName: aws.String(feedsPrimaryKey),
@@ -100,14 +100,14 @@ func createDynamo(t *testing.T) storage {
require.NoError(t, err) require.NoError(t, err)
err = d.dynamo.WaitUntilTableExists(&dynamodb.DescribeTableInput{TableName: pledgesTableName}) err = d.dynamo.WaitUntilTableExists(&dynamodb.DescribeTableInput{TableName: d.PledgesTableName})
require.NoError(t, err) require.NoError(t, err)
err = d.dynamo.WaitUntilTableExists(&dynamodb.DescribeTableInput{TableName: feedsTableName}) err = d.dynamo.WaitUntilTableExists(&dynamodb.DescribeTableInput{TableName: d.FeedsTableName})
require.NoError(t, err) require.NoError(t, err)
_, err = d.dynamo.UpdateTimeToLive(&dynamodb.UpdateTimeToLiveInput{ _, err = d.dynamo.UpdateTimeToLive(&dynamodb.UpdateTimeToLiveInput{
TableName: feedsTableName, TableName: d.FeedsTableName,
TimeToLiveSpecification: &dynamodb.TimeToLiveSpecification{ TimeToLiveSpecification: &dynamodb.TimeToLiveSpecification{
AttributeName: feedTimeToLiveField, AttributeName: feedTimeToLiveField,
Enabled: aws.Bool(true), Enabled: aws.Bool(true),