diff --git a/docker-compose.yml b/docker-compose.yml index 03b5185..8674642 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -20,6 +20,8 @@ services: - PATREON_WEBHOOKS_SECRET={PATREON_WEBHOOKS_SECRET} - COOKIE_SECRET={COOKIE_SECRET} - GIN_MODE=release + - DYNAMO_FEEDS_TABLE_NAME=Prod_Feeds + - DYNAMO_PLEDGES_TABLE_NAME=Prod_Pledges volumes: - {PATH_TO_GOOGLE_CREDENTIALS_FILE_FOR_USE_OUTSIDE_GOOGLE_CLOUD}:/google-credentials.json ytdl: diff --git a/pkg/config/config.go b/pkg/config/config.go index dadf2e8..8f534d8 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -9,17 +9,19 @@ import ( const FileName = "podsync" type AppConfig struct { - YouTubeApiKey string `yaml:"youtubeApiKey"` - VimeoApiKey string `yaml:"vimeoApiKey"` - PatreonClientId string `yaml:"patreonClientId"` - PatreonSecret string `yaml:"patreonSecret"` - PatreonRedirectURL string `yaml:"patreonRedirectUrl"` - PatreonWebhooksSecret string `json:"patreonWebhooksSecret"` - PostgresConnectionURL string `yaml:"postgresConnectionUrl"` - RedisURL string `yaml:"redisUrl"` - CookieSecret string `yaml:"cookieSecret"` - AssetsPath string `yaml:"assetsPath"` - TemplatesPath string `yaml:"templatesPath"` + YouTubeApiKey string `yaml:"youtubeApiKey"` + VimeoApiKey string `yaml:"vimeoApiKey"` + PatreonClientId string `yaml:"patreonClientId"` + PatreonSecret string `yaml:"patreonSecret"` + PatreonRedirectURL string `yaml:"patreonRedirectUrl"` + PatreonWebhooksSecret string `json:"patreonWebhooksSecret"` + PostgresConnectionURL string `yaml:"postgresConnectionUrl"` + RedisURL string `yaml:"redisUrl"` + CookieSecret string `yaml:"cookieSecret"` + AssetsPath string `yaml:"assetsPath"` + TemplatesPath string `yaml:"templatesPath"` + DynamoFeedsTableName string `yaml:"dynamoFeedsTableName"` + DynamoPledgesTableName string `yaml:"dynamoPledgesTableName"` } func ReadConfiguration() (cfg *AppConfig, err error) { @@ -34,17 +36,19 @@ func ReadConfiguration() (cfg *AppConfig, err error) { viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) envmap := map[string]string{ - "youtubeApiKey": "YOUTUBE_API_KEY", - "vimeoApiKey": "VIMEO_API_KEY", - "patreonClientId": "PATREON_CLIENT_ID", - "patreonSecret": "PATREON_SECRET", - "patreonRedirectUrl": "PATREON_REDIRECT_URL", - "patreonWebhooksSecret": "PATREON_WEBHOOKS_SECRET", - "postgresConnectionUrl": "POSTGRES_CONNECTION_URL", - "redisUrl": "REDIS_CONNECTION_URL", - "cookieSecret": "COOKIE_SECRET", - "assetsPath": "ASSETS_PATH", - "templatesPath": "TEMPLATES_PATH", + "youtubeApiKey": "YOUTUBE_API_KEY", + "vimeoApiKey": "VIMEO_API_KEY", + "patreonClientId": "PATREON_CLIENT_ID", + "patreonSecret": "PATREON_SECRET", + "patreonRedirectUrl": "PATREON_REDIRECT_URL", + "patreonWebhooksSecret": "PATREON_WEBHOOKS_SECRET", + "postgresConnectionUrl": "POSTGRES_CONNECTION_URL", + "redisUrl": "REDIS_CONNECTION_URL", + "cookieSecret": "COOKIE_SECRET", + "assetsPath": "ASSETS_PATH", + "templatesPath": "TEMPLATES_PATH", + "dynamoFeedsTableName": "DYNAMO_FEEDS_TABLE_NAME", + "dynamoPledgesTableName": "DYNAMO_PLEDGES_TABLE_NAME", } for k, v := range envmap { diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index e80868c..4471b0c 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -20,6 +20,8 @@ patreonRedirectUrl: "7" assetsPath: "8" templatesPath: "9" patreonWebhooksSecret: "10" +dynamoFeedsTableName: "11" +dynamoPledgesTableName: "12" ` func TestReadYaml(t *testing.T) { @@ -42,6 +44,8 @@ func TestReadYaml(t *testing.T) { require.Equal(t, "8", cfg.AssetsPath) require.Equal(t, "9", cfg.TemplatesPath) require.Equal(t, "10", cfg.PatreonWebhooksSecret) + require.Equal(t, "11", cfg.DynamoFeedsTableName) + require.Equal(t, "12", cfg.DynamoPledgesTableName) } func TestReadEnv(t *testing.T) { @@ -58,6 +62,8 @@ func TestReadEnv(t *testing.T) { os.Setenv("ASSETS_PATH", "88") os.Setenv("TEMPLATES_PATH", "99") os.Setenv("PATREON_WEBHOOKS_SECRET", "1010") + os.Setenv("DYNAMO_FEEDS_TABLE_NAME", "1111") + os.Setenv("DYNAMO_PLEDGES_TABLE_NAME", "1212") cfg, err := ReadConfiguration() require.NoError(t, err) @@ -72,4 +78,6 @@ func TestReadEnv(t *testing.T) { require.Equal(t, "88", cfg.AssetsPath) require.Equal(t, "99", cfg.TemplatesPath) require.Equal(t, "1010", cfg.PatreonWebhooksSecret) + require.Equal(t, "1111", cfg.DynamoFeedsTableName) + require.Equal(t, "1212", cfg.DynamoPledgesTableName) } diff --git a/pkg/storage/dynamo.go b/pkg/storage/dynamo.go index ae87387..9eca683 100644 --- a/pkg/storage/dynamo.go +++ b/pkg/storage/dynamo.go @@ -29,8 +29,6 @@ const ( ) var ( - pledgesTableName = aws.String("Pledges") - feedsTableName = aws.String("Feeds") feedTimeToLiveField = aws.String("ExpirationTime") feedDowngradeIndexName = aws.String("UserID-HashID-Index") ) @@ -56,7 +54,9 @@ Feeds: TTL attr: ExpirationTime */ type Dynamo struct { - dynamo *dynamodb.DynamoDB + dynamo *dynamodb.DynamoDB + FeedsTableName *string + PledgesTableName *string } func NewDynamo(cfg ...*aws.Config) (Dynamo, error) { @@ -76,7 +76,11 @@ func NewDynamo(cfg ...*aws.Config) (Dynamo, error) { 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 { @@ -97,7 +101,7 @@ func (d Dynamo) SaveFeed(feed *model.Feed) error { } input := &dynamodb.PutItemInput{ - TableName: feedsTableName, + TableName: d.FeedsTableName, Item: item, ConditionExpression: aws.String("attribute_not_exists(HashID)"), } @@ -116,7 +120,7 @@ func (d Dynamo) GetFeed(hashID string) (*model.Feed, error) { logger.Debug("getting feed") getInput := &dynamodb.GetItemInput{ - TableName: feedsTableName, + TableName: d.FeedsTableName, Key: map[string]*dynamodb.AttributeValue{ "HashID": {S: aws.String(hashID)}, }, @@ -158,7 +162,7 @@ func (d Dynamo) GetFeed(hashID string) (*model.Feed, error) { } updateInput := &dynamodb.UpdateItemInput{ - TableName: feedsTableName, + TableName: d.FeedsTableName, Key: getInput.Key, UpdateExpression: updateExpression.Update(), } @@ -198,7 +202,7 @@ func (d Dynamo) GetMetadata(hashID string) (*model.Feed, error) { } input := &dynamodb.GetItemInput{ - TableName: feedsTableName, + TableName: d.FeedsTableName, Key: map[string]*dynamodb.AttributeValue{ "HashID": {S: aws.String(hashID)}, }, @@ -255,7 +259,7 @@ func (d Dynamo) Downgrade(userID string, featureLevel int) error { logger.Debug("querying hash ids") queryInput := &dynamodb.QueryInput{ - TableName: feedsTableName, + TableName: d.FeedsTableName, IndexName: feedDowngradeIndexName, KeyConditionExpression: keyConditionExpression.KeyCondition(), ExpressionAttributeNames: keyConditionExpression.Names(), @@ -304,7 +308,7 @@ func (d Dynamo) Downgrade(userID string, featureLevel int) error { for _, key := range keys { input := &dynamodb.UpdateItemInput{ - TableName: feedsTableName, + TableName: d.FeedsTableName, Key: key, ConditionExpression: updateExpression.Condition(), UpdateExpression: updateExpression.Update(), @@ -338,7 +342,7 @@ func (d Dynamo) Downgrade(userID string, featureLevel int) error { for _, key := range keys { input := &dynamodb.UpdateItemInput{ - TableName: feedsTableName, + TableName: d.FeedsTableName, Key: key, UpdateExpression: updateExpression.Update(), ExpressionAttributeNames: updateExpression.Names(), @@ -370,7 +374,7 @@ func (d Dynamo) AddPledge(pledge *model.Pledge) error { } input := &dynamodb.PutItemInput{ - TableName: pledgesTableName, + TableName: d.PledgesTableName, Item: item, ConditionExpression: aws.String("attribute_not_exists(PatronID)"), } @@ -405,7 +409,7 @@ func (d Dynamo) UpdatePledge(patronID string, pledge *model.Pledge) error { } input := &dynamodb.UpdateItemInput{ - TableName: pledgesTableName, + TableName: d.PledgesTableName, Key: map[string]*dynamodb.AttributeValue{ pledgesPrimaryKey: {N: aws.String(patronID)}, }, @@ -432,7 +436,7 @@ func (d Dynamo) DeletePledge(pledge *model.Pledge) error { logger.Infof("deleting pledge %s", pk) input := &dynamodb.DeleteItemInput{ - TableName: pledgesTableName, + TableName: d.PledgesTableName, Key: map[string]*dynamodb.AttributeValue{ pledgesPrimaryKey: {N: aws.String(pk)}, }, @@ -452,7 +456,7 @@ func (d Dynamo) GetPledge(patronID string) (*model.Pledge, error) { logger.Debug("getting pledge") input := &dynamodb.GetItemInput{ - TableName: pledgesTableName, + TableName: d.PledgesTableName, Key: map[string]*dynamodb.AttributeValue{ pledgesPrimaryKey: {N: aws.String(patronID)}, }, diff --git a/pkg/storage/dynamo_test.go b/pkg/storage/dynamo_test.go index f3d20bf..5814d1b 100644 --- a/pkg/storage/dynamo_test.go +++ b/pkg/storage/dynamo_test.go @@ -21,12 +21,12 @@ func createDynamo(t *testing.T) storage { }) require.NoError(t, err) - d.dynamo.DeleteTable(&dynamodb.DeleteTableInput{TableName: pledgesTableName}) - d.dynamo.DeleteTable(&dynamodb.DeleteTableInput{TableName: feedsTableName}) + d.dynamo.DeleteTable(&dynamodb.DeleteTableInput{TableName: d.PledgesTableName}) + d.dynamo.DeleteTable(&dynamodb.DeleteTableInput{TableName: d.FeedsTableName}) // Create Pledges table _, err = d.dynamo.CreateTable(&dynamodb.CreateTableInput{ - TableName: pledgesTableName, + TableName: d.PledgesTableName, AttributeDefinitions: []*dynamodb.AttributeDefinition{ { AttributeName: aws.String(pledgesPrimaryKey), @@ -49,7 +49,7 @@ func createDynamo(t *testing.T) storage { // Create Feeds table _, err = d.dynamo.CreateTable(&dynamodb.CreateTableInput{ - TableName: feedsTableName, + TableName: d.FeedsTableName, AttributeDefinitions: []*dynamodb.AttributeDefinition{ { AttributeName: aws.String(feedsPrimaryKey), @@ -100,14 +100,14 @@ func createDynamo(t *testing.T) storage { 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) - err = d.dynamo.WaitUntilTableExists(&dynamodb.DescribeTableInput{TableName: feedsTableName}) + err = d.dynamo.WaitUntilTableExists(&dynamodb.DescribeTableInput{TableName: d.FeedsTableName}) require.NoError(t, err) _, err = d.dynamo.UpdateTimeToLive(&dynamodb.UpdateTimeToLiveInput{ - TableName: feedsTableName, + TableName: d.FeedsTableName, TimeToLiveSpecification: &dynamodb.TimeToLiveSpecification{ AttributeName: feedTimeToLiveField, Enabled: aws.Bool(true),